Simulate keyboard input using Python
Pywin installation module: http://sourceforge.net/projects/pywin32/files/
Import WIN32API
Import win32con
WIN32API. keybd_event (17,0,) # ctrl key bit code is 17
WIN32API. keybd_event (,) # The V key bit code is 86.
WIN32API. keybd_event (, win32con. keyeventf_keyup, 0) # Release the button
WIN32API. keybd_event (17,0, win32con. keyeventf_keyup, 0)
Attached key bit code table:
Letter and number keys-key function keys for the numeric keypad-other keys
Key/key/Key
A 65 0 96 F1 112 backspace 8
B 66 1 97 F2 113 tab 9
C 67 2 98 F3 114 clear 12
D 68 3 99 F4 115 enter 13
E 69 4 100 F5 116 shift 16
F 70 5 101 F6 117 control 17
G 71 6 102 F7 118 alt 18
H 72 7 103 F8 119 Caps Lock 20
I 73 8 104 F9 120 ESC 27
J 74 9 105 F10 121 spacebar 32
K 75*106 F11 122 page up 33
L 76 + 107 F12 123 page down 34
M 77 enter 108 -- end 35
N 78-109 -- home 36
O 79. 110 -- left arrow 37
P 80/111 -- Up Arrow 38
Q 81 -- right arrow 39
R 82 -- Down Arrow 40
S 83 -- insert 45
T 84 -- delete 46
U 85 -- help 47
V 86 -- Num Lock 144.
W 87
X 88
Y 89
Z 90
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
This function is in the library file user32.dll. We can find this file in the directory c: \ windows \ system32 (XP System), which is provided by the system. We use C # To directly call the API in this file as an example to illustrate how to perform mouse operations. First, we declare the reference in C #. If it is a from-based program, this declaration can be written in your from class.
[System. runtime. interopservices. dllimport ("USER32")]
Private Static extern int mouse_event (INT dwflags, int dx, int dy, int cbuttons, int dwextrainfo );
Parameter meaning
Dwflags long, one of the flags in the following table or their combination
DX, Dy long, specifies the absolute or relative position of the X and Y directions based on the mouseeventf_absolute mark.
Cbuttons long, not used
Dwextrainfo long, not used
Meaning of the dwflags constant
Const int mouseeventf_move = 0x0001; move the mouse
Const int mouseeventf_leftdown = 0x0002; simulate left mouse button Press
Const int mouseeventf_leftup = 0x0004; simulate left mouse button lifting
Const int mouseeventf_rightdown = 0x0008; simulate right-clicking
Const int mouseeventf_rightup = 0x0010; simulate right-clicking and raising
Const int mouseeventf_middledown = 0x0020; simulate the mouse key and press
Const int mouseeventf_middleup = 0x0040; simulate middle mouse button lifting
Const int mouseeventf_absolute = 0x8000; indicates whether absolute coordinates are used
In the program, we can directly call the mouse_event function.
Mouse_event (mouseeventf_absolute | mouseeventf_move, 500,500, 0, 0 );
1. Click the combination of the two events by clicking and releasing the left mouse button:
Mouse_event (mouseeventf_leftdown | mouseeventf_leftup, 0, 0, 0)
2. Simulate the right-click event:
Mouse_event (mouseeventf_rightdown | mouseeventf_rightup, 0, 0, 0)
3. Two consecutive left-click events constitute a double-click event:
Mouse_event (mouseeventf_leftdown | mouseeventf_leftup, 0, 0, 0)
Mouse_event (mouseeventf_leftdown | mouseeventf_leftup, 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 default point of the function is relative to the current position of the mouse. If dx, and Dy are expressed as 0, this function is considered to be the point where the current mouse is located. 5. directly set the absolute coordinates 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 );
X and Y are the abscissa and ordinate of the point you want to click.
Keybd_event () for keyboard Simulation ()
Keybd_event can trigger a key event, that is, a message wm_keydown or wm_keyup will be generated. Of course, the two messages can also be generated to simulate the buttons, but this function is not directly used for convenience. The keybd_event has four parameters. The first parameter is the virtual key value of the key. For example, the Enter key is vk_return and the tab key is vk_tab. The second parameter is the scan code. Generally, do not set it. Use 0 instead. The third parameter is the option flag. If it is keydown, set it to 0. If it is keyup, set it to "keyeventf_keyup". The fourth parameter is set to 0.