WPF TextBox Control gets the hotkey and turns to Win32 keys

Source: Internet
Author: User
Tags modifiers

The key object used in WPF differs from the keys in WinForm, where the key enumeration object differs from the physical key in that it has a function key and cannot be cast as a type. When using the win API to register a hot key, you need to convert it to a Win32 key value, you can use Keyinterop.virtualkeyfromkey (), and the keys can save the combo keys, key is just a single button. There is a Modifiers in the keys, from which you can see that the 0~15 bit is used to store the function keys. The difference between the two graphs can be visually identified by comparing them.


Example code:

Using System.windows.input;namespace demo.  Controls{class HotKeyTextBox:BeiLiNu.Ui.Controls.WPF.Controls.XTextBox {private System.Windows.Forms.Keys        Pressedkeys = System.Windows.Forms.Keys.None; protected override void Onpreviewkeydown (System.Windows.Input.KeyEventArgs e) {int keyValue = Keyintero            P.virtualkeyfromkey (E.key); if ((Keyboard.modifiers & modifierkeys.control) = = Modifierkeys.control) {keyValue + = (int)            System.Windows.Forms.Keys.Control; } if ((Keyboard.modifiers & modifierkeys.alt) = = Modifierkeys.alt) {KeyValue + = (int)            System.Windows.Forms.Keys.Alt;  } if ((Keyboard.modifiers & modifierkeys.shift) = = Modifierkeys.shift) {KeyValue            + = (int) System.Windows.Forms.Keys.Shift; } this.            Keys = (System.Windows.Forms.Keys) keyValue;      E.handled = true;             Public System.Windows.Forms.Keys Keys {get {return pressedkeys;}                set {Pressedkeys = value;            SetText (value); }} private void SetText (System.Windows.Forms.Keys Keys) {this. Text = keys.        ToString (); }    }}


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.