What do I have to use to be able to get “all keys” that are pressed on the keyboard at the moment? Since Form.KeyPress += new EventHandler() doesn’t do much at all when it’s filled of controls. It doesn’t call it no matter what I do, neither KeyDown, KeyUp or anything else… and yeah, I know how to use them.
So, if there’s any function in the system that can check for pressed keys, that returns an array of the keys used or something – I would be grateful to be pointed in the right direction.
It sounds like you want to query the state of all keys in the keyboard. The best function for that is the Win32 API call
GetKeyboardStateI don’t believe there is a good managed equivalent of that function. The PInvoke code for it is fairly straight forward
This will populate the
byte[]with the up / down state of every virtual key in the system. If the high bit is set then the virtual key is currently pressed. Mapping aKeyto a virtual key code is done by only considering thebyteportion of the numericKeyvalue.The above works for most of the
Keyvalues but you need to be careful with modifier keys. The valuesKeys.Alt,Keys.ControlandKeys.Shiftwon’t work here because they’re technically modifiers and not key values. To use modifiers you need to use the actual associated key valuesKeys.ControlKey,Keys.LShiftKey, etc … (really anything that ends with Key)So checking if a particular key is set can be done as follows