WPF captures keyboard input events

Source: Internet
Author: User

One requirement that was recently modified requires capturing the Text of the keyboard input, including various punctuation matches.

The first thing to think about is a keyboard event such as Previewkeydown or Previewkeyup.

But the object of these two events is not enough, you have to rely on the judge Key to determine what the input, and then according to the KeyEventArgs to write dead a corresponding key Text to obtain data.

such as the combination of keys to get the multiplication sign key need to use Shift + 8 (the numeric keypad that the multiplication sign key is key.multiply, and need to get in the main keyboard bit to use the key combination)

 1  private  void  Window_previewkeydown (object   sender, KeyEventArgs e)  2  { 3  if  (e.keystates = = Keyboard.getkeystates (KEY.D8) && Amp    Keyboard.modifiers == Modifierkeys.shift)  4  { 5   var  input =  " *  "  ;  6   " 7 } 

This approach is not friendly and need to use other punctuation when the additional if conditions, and the issue of key conflicts, as above you enter the multiplication sign key after you may add a "8", due to key.d8 reasons

Then took a little time to find a foreign blog, and then found the article (citation 1), in fact, the article in the Solution Blog Park can also find a few, but not detailed a pen.

This is the code I wrote according to the article.

Xaml:

1 <Windowx:class= "Dome.mainwindow"2 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"4 Previewtextinput= "Window_previewtextinput"5 Height= "The " 6 Width= "All"7 MinWidth= "All">8 </Window>

C#:

1 Private void Window_previewtextinput (object  sender, TextCompositionEventArgs e)2{3     var input = e.text; 4 }

Reference

Http://stackoverflow.com/questions/2924928/wpf-previewkeydown-event-and-underscore-char

Summarize

About the difference between Previewkeydown and KeyDown there are a lot of related articles in the park, I will also write a later, after all, I also met this pit ...

WPF captures keyboard input events

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.