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 );