Detecting a key press

可紊 提交于 2019-12-10 22:36:30

问题


I am currently checking the keyboard state in my program:

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

And detecting key presses like this:

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

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.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/12990049/detecting-a-key-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!