Simulate keyboard and mouse operations in. net

Source: Internet
Author: User

Zhou yinhui

In fact, the sendkeys class provides a very useful method. Unfortunately, it is unavailable in WPF. It is said that the message loop method of WPF is changed to dispatcher, so the system is called directly. windows. forms. sendkeys. the send () method returns an error. but it doesn't matter. At least sendinput of use32 is available. Through P/invoke, we can simulate keyboard or mouse operations.

The definition is as follows:

 
[Dllimport("User32.dll", Setlasterror =True)]Internal static extern intSendinput (IntNinputs,RefInputMi,IntCbsize );
 
The input structure indicates a keyboard or mouse operation:
[ Structlayout ( Layoutkind . Sequential)]Internal struct  Input { Internal int Type; Internal  Inputunion Union ;};[ Structlayout ( Layoutkind . Explicit)] Internal struct  Inputunion {[ Fieldoffset (0)] Internal  Mouseinput Mouseinput ;[ Fieldoffset (0)]Internal  Keybdinput Keyboardinput ;};[ Structlayout ( Layoutkind . Sequential)] Internal struct  Mouseinput { Internal int DX; Internal int Dy; Internal int Mousedata; Internal int Dwflags; Internal int Time; Internal  Intptr Dwextrainfo ;};[Structlayout ( Layoutkind . Sequential)] Internal struct  Keybdinput { Internal Short Wvk; Internal Short Wscan; Internal int Dwflags; Internal int Time; Internal  Intptr Dwextrainfo ;};[ Flags ] Internal Enum  Sendmouseinputflags {Move = 0x0001, leftdown = 0x0002, leftup = 0x0004, rightdown = 0x0008, rightup = 0x0010, middledown = 0x0020, middleup = 0x0040, xdown = 0x0080, xup = 0x0100, wheel = 0x0800, absolute = 0x8000 ,};

Definitions of these methods or structures can be found on the http://www.pinvoke.net/index.aspx site (but not guaranteed to be reliable)

The followingCodeThe file is well packaged and can be downloaded for reference:

Simulation.zip

How to use it?

It's easy to press a key, for example, press Enter:

 
Keyboard. Press (Key. Enter );Keyboard. Release (Key. Enter );

Enter a key combination, for example, ALT + F4.
Keyboard. Press (Key. Leftalt );Keyboard. Press (Key. F4 );Keyboard. Release (Key. Leftalt );Keyboard. Release (Key. F4 );
 
Enter a text clip:
Keyboard. Type ("Notepad");
 
The mouse is similar to this, for example:
Mouse. MoveTo (NewSystem. Drawing.Point(X, y ));Mouse. Click (Mousebutton. Right );

 

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.