1. Analog keyboard input (SendKeys)
Function: Send one or more button messages to the active window as if you were typing on the keyboard.
Syntax: SendKeys.Send(string keys);SendKeys.SendWait(string keys);
Description:
(1) Each button is represented by one or more characters. In order to specify a single keyboard character, you must press the key of the character itself. For example, to represent the letter A, you can use “A” as the string. In order to represent multiple characters, you must add another character directly after the character. For example, to represent A, B, and C, use “ABC” as the string.
(2) For SendKeys, the plus sign (+), caret (^), percent sign (%), overscore (~), and parentheses ( ) all have special meaning. To specify any of the above characters, place it in braces ({}). For example, to specify a positive sign, it can be represented by {+}. Square brackets ([ ]) are not of special significance to SendKeys, but they must be placed in braces. In other applications, the square brackets have special meaning, which may be important in the presence of dynamic data exchange (DDE). To specify braces, use {{} and {}}.
(3) In order to specify those characters that are not displayed when the button is pressed, such as ENTER or TAB and those that represent actions instead of characters, use the following code:
button | Code |
BACKSPACE | {BACKSPACE}, {BS}, or {BKSP} |
BREAK | {BREAK} |
CAPS LOCK | {CAPSLOCK} |
DEL or DELETE | {DELETE} or {DEL} |
DOWN ARROW | {DOWN} |
END | {END} |
ENTER | {ENTER} or ~ |
ESC | {ESC} |
HELP | {HELP} |
HOME | {HOME} |
INS or INSERT | {INSERT} or {INS} |
LEFT ARROW | {LEFT} |
NUM LOCK | {NUMLOCK} |
PAGE DOWN | {PGDN} |
PAGE UP | {PGUP} |
PRINT SCREEN | {PRTSC} |
RIGHT ARROW | {RIGHT} |
SCROLL LOCK | {SCROLLLOCK} |
TAB | {TAB} |
UP ARROW | {UP} |
F1 | {F1} |
F2 | {F2} |
F3 | {F3} |
F4 | {F4} |
F5 | {F5} |
F6 | {F6} |
F7 | {F7} |
F8 | {F8} |
F9 | {F9} |
F10 | {F10} |
F11 | {F1} |
F12 | {F12} |
F13 | {F13} |
F14 | {F14} |
F15 | {F15} |
F16 | {F16} |
(4) In order to specify those key combinations combined with keys such as SHIFT, CTRL, and ALT, one or more codes can be placed in front of these key codes, which are listed below:
button | Code |
Shift | + |
Ctrl | ^ |
Alt | % |
To show that you can press any combination of SHIFT, CTRL, and ALT at the same time when pressing other buttons, put the codes of those buttons in parentheses. For example, to say that pressing the Shift key while pressing E and C, use “+(EC)”. To illustrate that pressing the SHIFT button while pressing E, then pressing C instead of SHIFT uses “+EC”.
To specify a repeat key, use the form {key number}. A space must be placed between key and number. For example, {LEFT 42} means that the LEFT ARROW key is pressed 42 times; {h 10} means that the H key is pressed 10 times.
Note: You cannot use SendKeys to send key messages to an application that is not designed to run on Microsoft Windows. Sendkeys also cannot send the PRINT SCREEN button {PRTSC} to any application.
(5) Input Chinese characters with SendKeys.Send (“Chinese characters”);
Original blog address:http://www.cnblogs.com/sydeveloper/archive/2013/02/25/2932571.html
2. Simulate mouse movement and click
The mouse_event function we need to use is located in the user32.dll library file, so we need to declare the reference first.
1 [DllImport("user32",CharSet=CharSet.Unicode)]
2 private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
3 //Moving the mouse
4 const int MOUSEEVENTF_MOVE = 0x0001;
5 //Simulate left mouse button press
6 const int MOUSEEVENTF_LEFTDOWN = 0x0002;
7 //Simulate left mouse button up
8 const int MOUSEEVENTF_LEFTUP = 0x0004;
9 //Simulate mouse right click
10 const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
11 //Simulate the right mouse button up
12 const int MOUSEEVENTF_RIGHTUP = 0x0010;
13 //Analog mouse button press
14 const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
15 //Analog mouse middle button lift
16 const int MOUSEEVENTF_MIDDLEUP = 0x0040;
17 //Indicate whether absolute coordinates are used
18 const int MOUSEEVENTF_ABSOLUTE = 0x8000;
dwFlags:A set of flag bits that specifies a variety of situations for clicking buttons and mouse actions. Each of the parameters in this parameter can be any reasonable combination of the following values:
- MOUSEEVENTF_ABSOLUTE: Indicates that the parameter dX, dy contains normalized absolute coordinates. If this bit is not set, the parameter contains relative data: the location of the change relative to the last position. This flag can be set or not, regardless of the type of mouse or the type of mouse-like device connected to the system. For information on relative mouse actions, see the Remarks section below.
- MOUSEEVENTF_MOVE: Indicates that a move has occurred.
- MOUSEEVENTF_LEFTDOWN: Indicates that the left mouse button is pressed.
- MOUSEEVENTF_LEFTUP: Indicates that the left mouse button is released.
- MOUSEEVENTF_RIGHTDOWN: Indicates that the right mouse button is pressed.
- MOUSEEVENTF_RIGHTUP: Indicates that the right mouse button is released.
- MOUSEEVENTF_MIDDLEDOWN: Indicates that the middle mouse button is pressed.
- MOUSEEVENTF_MIDDLEUP: Indicates that the middle mouse button is released.
- MOUSEEVENTF_WHEEL: If there is a round in the mouse in Windows NT, the mouse wheel is moved. The number of moves is given by dwData.
dx:Specifying the absolute position of the mouse along the x-axis or the number of moves since the last mouse event was generated depends on the setting of MOUSEEVENTF_ABSOLUTE. The absolute data given is taken as the actual X coordinate of the mouse; the relative data given is given as the number of moving mickeys. A mickey indicates the number of mouse movements, indicating that the mouse has moved.
dy:Specifying the absolute position of the mouse along the y-axis or the number of moves since the last mouse event was generated depends on the setting of MOUSEEVENTF_ABSOLUTE. The absolute data given is taken as the actual y coordinate of the mouse, and the relative data given is given as the number of moving mickeys.
dwData:If dwFlags is MOUSEEVENTF_WHEEL, dwData specifies the number of mouse wheel movements. A positive value indicates that the mouse wheel is rotating forward, ie away from the user; a negative value indicates that the mouse wheel is turning backwards, ie towards the user. A wheel is defined as WHEEL_DELTA, which is 120.
If dwFlags is not MOUSEEVENTF_WHEEL, then dWData should be zero.
dwExtralnfo:Specifies an additional 32-bit value associated with the mouse event. The application calls the function GetMessageExtraInfo to get this additional information.
return value:no.
In the program we can call the mouse_event function directly. mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0);
1. Here is the combination of the left mouse button press and release two events: one mouse click: mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
2. Simulate the right mouse click event: mouse_event (MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 )
3. Two consecutive left mouse click events constitute a mouse double click event: mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 ) mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
4. Use absolute coordinates (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0)
It should be noted that if MOUSEEVENTF_ABSOLUTE is not used, the function defaults to the point relative to the current position of the mouse. If dx, and dy, it is represented by 0, 0, this function is considered to be the point where the current mouse is located.
5. Set the absolute coordinates directly and click mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0 ); where X, Y are the abscissa and ordinate of the point you want to click, respectively
6, keyboard simulation with Keybd_event ()
Keybd_event can trigger a button event, which means that a WM_KEYDOWN or WM_KEYUP message is generated. Of course, you can also use these two messages to simulate the button, but it is not convenient to use this function directly. Keybd_event has four parameters. The first one is the virtual key value of the button. For example, the enter key is vk_return and the tab key is vk_tab. The second parameter is the scan code. Generally, it is not necessary to set it. The third parameter is the option flag. If it is keydown, it can be set to 0. If it is keyup, it is set to “KEYEVENTF_KEYUP”. The fourth parameter is also set to 0.
Reprinted to the blog (slightly revised):http://www.cnblogs.com/blackice/p/3418414.html
For other properties of DllImport, see MSDN (Fields):https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.dllimportattribute.aspx
3. Set the cursor position
Declare reference, import method
1 [DllImport("User32.dll")]
2 public extern static bool GetCursorPos(ref Point pot);
3
4 [DllImport("User32.dll")]
5 public extern static void SetCursorPos(int x, int y);
Call method to set the cursor position
1 SetCursorPos(int.Parse(pointX), (int.Parse(pointY));
4. Control the display of the application
Declare reference, import method
1 [DllImport("User32.dll")]
2 public static extern bool SetForegroundWindow(IntPtr hWnd);
3
4 [DllImport("User32.dll")]
5 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
Call the method to activate and display the opening web page (IE) window to the desktop
1 static void Main(string[] args)
2 {
3 Process[] localByName = Process.GetProcessesByName("iexplore");
4 foreach (Process proc in localByName)
5 {
6 //internet explorer uses a hosting model - one iexplore.exe instance hosts the internet explorer frame,
7 //The other iexplore.exe instances just display the contents of tabs (the page title - MainWindowTitle is not an empty string)
8 if (proc.MainWindowTitle != string.Empty)
9 {
10 IntPtr handle = proc.MainWindowHandle;
11 SetForegroundWindow(handle);
12 ShowWindow(handle, 3);
13 }
14 }
15 }
The effect of this method is equivalent to:
Keyboard Events Simulation using keybd_event() function
Introduction
Simulation of a keyboard input is a well known concept for those who are all familiar with Visual Basic. SendKeys()
in Visual Basic does all the things, if you want to do anything without keyboard. But what is in SendKeys()
function? What does it do? Can we do such a thing with Visual C++? This article is the answer. I think this will be useful for beginners who are all just trying to do something different with VC++. Let us get into the steps…
Function Syntax
Copy Codevoid keybd_event(BYTE bVirtualKey, BYTE bScanCode,
DWORD dwFlags, DWORD dwExtraInfo);
bVirtualKey //Virtual Keycode of keys. E.g., VK_RETURN, VK_TAB…
bScanCode //Scan Code value of keys. E.g., 0xb8 for “Left Alt” key.
dwFlags //Flag that is set for key state. E.g., KEYEVENTF_KEYUP.
dwExtraInfo //32-bit extra information about keystroke.
Function Details:
bVirtualKey
Virtual keycode that has to be send as key input. The following are the available predefined virtual key codes:
VK_NUMPAD7 | 0x67 | VK_BACK | 0x08 |
VK_NUMPAD8 | 0x68 | VK_TAB | 0x09 |
VK_NUMPAD9 | 0x69 | VK_RETURN | 0x0D |
VK_MULTIPLY | 0x6A | VK_SHIFT | 0x10 |
VK_ADD | 0x6B | VK_CONTROL | 0x11 |
VK_SEPARATOR | 0x6C | VK_MENU | 0x12 |
VK_SUBTRACT | 0x6D | VK_PAUSE | 0x13 |
VK_DECIMAL | 0x6E | VK_CAPITAL | 0x14 |
VK_DIVIDE | 0x6F | VK_ESCAPE | 0x1B |
VK_F1 | 0x70 | VK_SPACE | 0x20 |
VK_F2 | 0x71 | VK_END | 0x23 |
VK_F3 | 0x72 | VK_HOME | 0x24 |
VK_F4 | 0x73 | VK_LEFT | 0x25 |
VK_F5 | 0x74 | VK_UP | 0x26 |
VK_F6 | 0x75 | VK_RIGHT | 0x27 |
VK_F7 | 0x76 | VK_DOWN | 0x28 |
VK_F8 | 0x77 | VK_PRINT | 0x2A |
VK_F9 | 0x78 | VK_SNAPSHOT | 0x2C |
VK_F10 | 0x79 | VK_INSERT | 0x2D |
VK_F11 | 0x7A | VK_DELETE | 0x2E |
VK_F12 | 0x7B | VK_LWIN | 0x5B |
VK_NUMLOCK | 0x90 | VK_RWIN | 0x5C |
VK_SCROLL | 0x91 | VK_NUMPAD0 | 0x60 |
VK_LSHIFT | 0xA0 | VK_NUMPAD1 | 0x61 |
VK_RSHIFT | 0xA1 | VK_NUMPAD2 | 0x62 |
VK_LCONTROL | 0xA2 | VK_NUMPAD3 | 0x63 |
VK_RCONTROL | 0xA3 | VK_NUMPAD4 | 0x64 |
VK_LMENU | 0xA4 | VK_NUMPAD5 | 0x65 |
VK_RMENU | 0xA5 | VK_NUMPAD6 | 0x66 |
- Character key can be converted into virtual key using
VkKeyScan(TCHAR ch)
function. bScanCode
Scan code is the hardware key code for the key (make and break codes). The following are the available scan codes (break code will be used in this parameter).dwFlags
A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags.ValueMeaningKEYEVENTF_EXTENDEDKEY
If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).KEYEVENTF_KEYUP
If specified, the key is being released. If not specified, the key is being depressed.dwExtraInfo
32-bit extra information along with the keyboard input.
Example Code
Copy Code// Simulating a Alt+Tab keystroke
keybd_event(VK_MENU,0xb8,0 , 0); //Alt Press
keybd_event(VK_TAB,0x8f,0 , 0); // Tab Press
keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0); // Tab Release
keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0); // Alt Release
// Simulating a Ctrl+A keystroke
keybd_event(VK_CONTROL,0x9d,0 , 0); // Ctrl Press
keybd_event(VkKeyScan(‘A’),0x9e,0 , 0); // ‘A’ Press
keybd_event(VkKeyScan(‘A’),0x9e, KEYEVENTF_KEYUP,0); // ‘A’ Release
keybd_event(VK_CONTROL,0x9d,KEYEVENTF_KEYUP,0); // Ctrl Release
Conclusion
This article may not be that much detailed. None of the articles can satisfy one’s expectations. But, each article should be a seed for your technical growth. Thus, I believe that this would be a seed. Thank you all.https://83d95145cda2fc05646af72d9148b2a9.safeframe.googlesyndication.com/safeframe/1-0-37/html/container.html
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
thanks Naren
public static class VirtualMouse {
// import the necessary API function so .NET can
// marshall parameters appropriately [DllImport("user32.dll")] static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
// constants for the mouse_input() API function private const int MOUSEEVENTF_MOVE = 0x0001; private const int MOUSEEVENTF_LEFTDOWN = 0x0002; private const int MOUSEEVENTF_LEFTUP = 0x0004; private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; private const int MOUSEEVENTF_RIGHTUP = 0x0010; private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; private const int MOUSEEVENTF_MIDDLEUP = 0x0040; private const int MOUSEEVENTF_ABSOLUTE = 0x8000; // simulates movement of the mouse. parameters specify changes
// in relative position. positive values indicate movement // right or down
public static void Move(int xDelta, int yDelta)
{ mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
}
// simulates movement of the mouse. parameters specify an // absolute location, with the top left corner being the
// origin
public static void MoveTo(int x, int y)
{ mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0); }
// simulates a click-and-release action of the left mouse
// button at its current position public static void LeftClick()
{ mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); }
}
public static class VirtualMouse
{
// import the necessary API function so .NET can
// marshall parameters appropriately
[DllImport(“user32.dll”)]
static extern void
mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
// constants for the mouse_input() API function
private const int MOUSEEVENTF_MOVE = 0x0001;
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const int MOUSEEVENTF_RIGHTUP = 0x0010;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
// simulates movement of the mouse. parameters specify changes
// in relative position. positive values indicate movement
// right or down
public static void Move(int xDelta, int yDelta)
{
mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
}
// simulates movement of the mouse. parameters specify an
// absolute location, with the top left corner being the
// origin
public static void MoveTo(int x, int y)
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
}
// simulates a click-and-release action of the left mouse
// button at its current position
public static void LeftClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
}
}
If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner of the display surface, (65535,65535) maps onto the lower-right corner.
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(200, 200);
//Yes, it places the mouse pointer in the coordinates (200, 200) pixels of the screen //[Tested on LinqPad].
You can use that to convert the input in pixels to the desired value, like this:
var inputXinPixels = 200;
var inputYinPixels = 200;
var screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
//var outputX = inputXinPixels * 65535 / screenBounds.Width;
//var outputY = inputYinPixels * 65535 / screenBounds.Height;
var outputX = (inputXinPixels * 65536 / screenBounds.Width) + 1;
var outputY = (inputYinPixels * 65536 / screenBounds.Height) + 1;
MoveTo(outputX, outputY);
var inputXinPixels = 200;
var inputYinPixels = 200;
var screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
//var outputX = inputXinPixels * 65535 / screenBounds.Width;
//var outputY = inputYinPixels * 65535 / screenBounds.Height;
var outputX = (inputXinPixels * 65536 / screenBounds.Width) + 1;
var outputY = (inputYinPixels * 65536 / screenBounds.Height) + 1;
MoveTo(outputX, outputY);