WPF obtains the keyboard status (for example, WPF keys)

Source: Internet
Author: User

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 )));
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.