惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
H
Help Net Security
月光博客
月光博客
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
GbyAI
GbyAI
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 小峰

反思下 [.net] 将 datatable 存储到数据库 - 小峰 要上班了 表达式模版学习笔记 模版元编程学习笔记 模版默认参数小探 boost serialization 试用 套接字 io 模型 小结 精简版 rtti class 能不能这么写 boost/format试用 类成员变量私有化 设计模式小结 接口版本演化引发的思考二 c# 程序最小化到系统托盘 [xna]帧数 皆是#惹的祸 代码片段 单元测试
[xna]键盘
小峰 · 2006-10-18 · via 博客园 - 小峰

   这几天看了看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