检测按键 [英] Detecting a key press

查看:139
本文介绍了检测按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在检查程序中的键盘状态:

I am currently checking the keyboard state in my program:

SlimDX.DirectInput.KeyboardState keyboardState = keyboard.GetCurrentState();

并检测像这样的按键:

 if(keyboardState.IsPressed(SlimDX.DirectInput.Key.Q))...

我不知道如何检测任何按键,而不是特定的按键.翻阅SlimDX API文档后,我不能指望它.毫无疑问,我可能会错过一些非常简单的东西.

I can't figure out how to detect ANY key press, rather than specific keys. I can't put my finger on it after trawling through the SlimDX API documentation. I'm probably missing something really simple no doubt.

推荐答案

KeyboardState类具有PressedKeys只读属性,其中包含按下键的IList<Key>.

KeyboardState class has a PressedKeys read-only property which contains an IList<Key> of pressed keys.

您可以检查此列表是否为空,以了解没有按下任何键(kbd.PressedKeys.Count == 0).

You can check if this list is empty to know that nothing is pressed (kbd.PressedKeys.Count == 0).

如果要响应键盘事件并避免轮询模式(对按键是否进行显式检查),则应查看SlimDX.RawInput命名空间并使用按键事件处理,类似于WinForms.

If you want to respond to keyboard events and avoid the polling mode (explicit checks for keypresses) you should look to SlimDX.RawInput namespace and use the key press event handling, similar to WinForms.

这篇关于检测按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆