WPF Analog UI Keyboard entry

Source: Internet
Author: User

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

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.