For Keyboard Events (previewkeydown, keydown, previewkeyup, keyup), it is easier to obtain keyboard information such as key combinations.
1. The keyeventargs object contains a keystates attribute, which reflects the key attribute of the trigger event.
2. The keyboarddevice attribute provides the same information for all keys on the key disk. Naturally, it also provides an instance of the keyboarddevice class. Its attributes include the current element with focus, and the modifiers that are pressed when an event occurs, including shift, Ctrl, and ALT, besides, bit logic is used to check their status.
Private void textbox_keydown_1 (Object sender, keyeventargs E)
{
If (E. keyboarddevice. modifiers & modifierkeys. Control) = modifierkeys. Control)
{
MessageBox. Show ("you pressed the control key ");
}
}
For keys with the switch function, you can use the iskeytoggled () method in the keyboard class for detection. For example:
Private void textbox_keydown_1 (Object sender, keyeventargs E)
{
If (keyboard. iskeydown (key. numlock ))
{
MessageBox. Show (string. Format ("you pressed the numlock key, the current status is: {0}", keyboard. iskeytoggled (key. numlock )));
}
}
Of course, the keyboarddevice method also provides the iskeydown method, iskeyup method, iskeytoggled method, and getkeystates method, for example:
If (E. keyboarddevice. iskeydown (key. numlock ))
{
MessageBox. Show (string. Format ("you pressed the numlock key, the current status is: {0}", E. keyboarddevice. iskeytoggled (key. numlock )));
}