WinForm applications can be developed using the SendKeys class to facilitate the simulation of keyboard entry operations. So how do you implement keyboard emulation for a control in WPF? This article will use the same effect as the WPF SendKeys implementation and WinForm.
First add WpfSendKeys.dll to the project references, add two textbox and a button in the XAML with the following code.
<window x:class= "Wpkeyboard.mainwindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation " xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml " title=" MainWindow "loaded=" window_loaded " Keydown= "Window_keydown" x:name= "Mainwin" height= "x" width= "525" > <Grid> <textbox x: Name= "tb1" fontsize= "margin=" 73,12,84,218 "/> <textbox x:name=" TB2 "fontsize=" margin= " 73,119,84,117 "/> <button x:name=" btn "content=" Auto Click and change Font Color " click=" Btn_click " margin= "73,229,84,36"/> </Grid></Window>
Open the C # program and add the System.Windows.Input.Test namespace. Defines the Sendtouithread method used to send keyboard directives to a WPF control. In window_loaded, the "Hello world!" character is written to the textbox by calling the Sendtouithread method, and the characters are copied, and finally the button is automatically clicked to complete the change of font color. Where the string copy works by judging the contents of the analog keyboard input as "Control+c", the string is copied into the second textbox.
Using system;using system.threading;using system.windows;using system.windows.input;using System.Windows.Input.Test ; using system.windows.media;using system.windows.threading;namespace wpkeyboard{//<summary>//Interaction Logic for MainWindow.xaml//</summary> public partial class Mainwindow:window {public mainwind ow () {InitializeComponent (); } private void Sendtouithread (UIElement element, string text) {element. Dispatcher.begininvoke (new Action () = {Sendkeys.send (element, text);}), DISPATCHERPR Iority. Input); The private void Btn_click (object sender, RoutedEventArgs e) {tb1. Foreground = new SolidColorBrush (colors.red); TB2. Foreground = new SolidColorBrush (colors.blue); private void Window_Loaded (object sender, RoutedEventArgs e) {ThreadPool.QueueUserWorkItem (_ = > {Thread.Sleep (3000); Sendtouithread (TB1, "Hello"); Thread.Sleep (1000); Sendtouithread (TB1, "W"); Thread.Sleep (1000); Sendtouithread (TB1, "O"); Thread.Sleep (1000); Sendtouithread (TB1, "R"); Thread.Sleep (1000); Sendtouithread (TB1, "ld!"); Thread.Sleep (1000); Sendtouithread (Mainwin, "^c"); Thread.Sleep (1000); Sendtouithread (btn, "{ENTER}"); }); private void Window_keydown (object sender, KeyEventArgs e) {if (E.keyboarddevice.modifiers.has Flag (modifierkeys.control) && E.key = = key.c) {tb2. Text = tb1. Text; } } }}
Dynamic Demo
The program does not need to click any button after running, the program automatically completes the operation of the diagram.
Source code Download
Wpkeyboard.zip
Http://www.cnblogs.com/gnielee/archive/2010/08/10/wpf-keyboard-auto-input.html
WPF Analog UI Keyboard entry