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