问题
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