[xna]键盘
这几天看了看xna。这个东东可能在不远的未来占有举足轻重的地位。有证见gameres上的讨
论:
http://bbs.gameres.com/showthread.asp?threadid=25707
http://bbs.gameres.com/showthread.asp?threadid=62072
xna的简单介绍:
XNA Game Studio Express is a set of tools based on Microsoft Visual C# Express 2005 that allow students and hobbyists to build games that target both Microsoft® Windows® and Xbox 360™. XNA Game Studio Express also includes the XNA Framework, a set of managed libraries, based on the .NET 2.0 Framework, tuned for game development. This documentation collection contains technology overviews, tutorials, and reference material related to XNA Game Studio Express.
(见微软帮助文档:ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MS.XNAFX.1033/XNA/XNA_Overview.htm)
多的不说了。既然是学就不能白学,写个键盘辅助类做“到此一游”的佐证。
代码如下:
//------------------------------------------------------------------
using System;
using System.IO;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
//------------------------------------------------------------------
namespace Microsoft.Xna.Framework.Input
{
/// <summary>
/// 键盘辅助类
/// </summary>
public static class KeyboardHelper
{
private static KeyboardState lastFrameKeyboardState;
private static List<Keys> pressedKeys;
private static List<Keys> releaseKeys;
private static bool allowKeyPressedRepeat;

/// <summary>
/// 获取上帧键盘状态
/// </summary>
public static KeyboardState LastFrameKeyboardState
{
get { return lastFrameKeyboardState; }
}
/// <summary>
/// 获取按下键集合
/// </summary>
public static List<Keys> PressedKeys
{
get { return pressedKeys; }
}
/// <summary>
/// 获取释放键集合
/// </summary>
public static List<Keys> ReleaseKeys
{
get { return releaseKeys; }
}
/// <summary>
/// 是否允许按下键重复
/// </summary>
public static bool AllowKeyPressedRepeat
{
get { return allowKeyPressedRepeat; }
set { allowKeyPressedRepeat = value; }
}

public static void Initalize() { }
static KeyboardHelper()
{
lastFrameKeyboardState = Keyboard.GetState();
pressedKeys = new List<Keys>();
releaseKeys = new List<Keys>();
}
/// <summary>
/// 检测指定键是否在集合中
/// </summary>
/// <param name="key"></param>
/// <param name="collection"></param>
/// <returns></returns>
public static bool IsKeyInCollection(Keys key, Keys[] collection)
{
foreach (Keys child in collection)
{
if (child == key)
{
return true;
}
}
return false;
}
/// <summary>
/// 更新按键状态
/// </summary>
public static void Update()
{
//清理
pressedKeys.Clear();
releaseKeys.Clear();

//获取键盘状态
KeyboardState thisFrameKeyboardState = Keyboard.GetState();

//检测老键是否在新键中存在
foreach (Keys key in lastFrameKeyboardState.GetPressedKeys())
{
if (IsKeyInCollection(key, thisFrameKeyboardState.GetPressedKeys()))
{
//交集部分处理
if (allowKeyPressedRepeat)
{
pressedKeys.Add(key);
}
}
else
{
releaseKeys.Add(key);
}
}

//检测新键是否在老键中
foreach (Keys key in thisFrameKeyboardState.GetPressedKeys())
{
if (!IsKeyInCollection(key, lastFrameKeyboardState.GetPressedKeys()))
{
pressedKeys.Add(key);
}
}

//记录新键
lastFrameKeyboardState = thisFrameKeyboardState;
}
/// <summary>
/// 检测指定键是否按下
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsKeyPressed(Keys key)
{
return pressedKeys.Contains(key);
}
/// <summary>
/// 检测指定键是否释放
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsKeyReleased(Keys key)
{
return releaseKeys.Contains(key);
}
}
}
//------------------------------------------------------------------
参考msdn帮助文档:ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MS.XNAFX.1033/XNA/XNA_Overview.htm
论:
http://bbs.gameres.com/showthread.asp?threadid=25707
http://bbs.gameres.com/showthread.asp?threadid=62072
xna的简单介绍:
XNA Game Studio Express is a set of tools based on Microsoft Visual C# Express 2005 that allow students and hobbyists to build games that target both Microsoft® Windows® and Xbox 360™. XNA Game Studio Express also includes the XNA Framework, a set of managed libraries, based on the .NET 2.0 Framework, tuned for game development. This documentation collection contains technology overviews, tutorials, and reference material related to XNA Game Studio Express.
(见微软帮助文档:ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MS.XNAFX.1033/XNA/XNA_Overview.htm)
多的不说了。既然是学就不能白学,写个键盘辅助类做“到此一游”的佐证。
代码如下:
//------------------------------------------------------------------
using System;
using System.IO;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
//------------------------------------------------------------------
namespace Microsoft.Xna.Framework.Input
{
/// <summary>
/// 键盘辅助类
/// </summary>
public static class KeyboardHelper
{
private static KeyboardState lastFrameKeyboardState;
private static List<Keys> pressedKeys;
private static List<Keys> releaseKeys;
private static bool allowKeyPressedRepeat;
/// <summary>
/// 获取上帧键盘状态
/// </summary>
public static KeyboardState LastFrameKeyboardState
{
get { return lastFrameKeyboardState; }
}
/// <summary>
/// 获取按下键集合
/// </summary>
public static List<Keys> PressedKeys
{
get { return pressedKeys; }
}
/// <summary>
/// 获取释放键集合
/// </summary>
public static List<Keys> ReleaseKeys
{
get { return releaseKeys; }
}
/// <summary>
/// 是否允许按下键重复
/// </summary>
public static bool AllowKeyPressedRepeat
{
get { return allowKeyPressedRepeat; }
set { allowKeyPressedRepeat = value; }
}
public static void Initalize() { }
static KeyboardHelper()
{
lastFrameKeyboardState = Keyboard.GetState();
pressedKeys = new List<Keys>();
releaseKeys = new List<Keys>();
}
/// <summary>
/// 检测指定键是否在集合中
/// </summary>
/// <param name="key"></param>
/// <param name="collection"></param>
/// <returns></returns>
public static bool IsKeyInCollection(Keys key, Keys[] collection)
{
foreach (Keys child in collection)
{
if (child == key)
{
return true;
}
}
return false;
}
/// <summary>
/// 更新按键状态
/// </summary>
public static void Update()
{
//清理
pressedKeys.Clear();
releaseKeys.Clear();
//获取键盘状态
KeyboardState thisFrameKeyboardState = Keyboard.GetState();
//检测老键是否在新键中存在
foreach (Keys key in lastFrameKeyboardState.GetPressedKeys())
{
if (IsKeyInCollection(key, thisFrameKeyboardState.GetPressedKeys()))
{
//交集部分处理
if (allowKeyPressedRepeat)
{
pressedKeys.Add(key);
}
}
else
{
releaseKeys.Add(key);
}
}
//检测新键是否在老键中
foreach (Keys key in thisFrameKeyboardState.GetPressedKeys())
{
if (!IsKeyInCollection(key, lastFrameKeyboardState.GetPressedKeys()))
{
pressedKeys.Add(key);
}
}
//记录新键
lastFrameKeyboardState = thisFrameKeyboardState;
}
/// <summary>
/// 检测指定键是否按下
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsKeyPressed(Keys key)
{
return pressedKeys.Contains(key);
}
/// <summary>
/// 检测指定键是否释放
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsKeyReleased(Keys key)
{
return releaseKeys.Contains(key);
}
}
}
//------------------------------------------------------------------参考msdn帮助文档:ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MS.XNAFX.1033/XNA/XNA_Overview.htm


浙公网安备 33010602011771号