Python basic keyboard operation
Article Directory
1. Keyboard input
pyautogui.keyDown()
: Simulate key press ;:
pyautogui.keyUp()
Simulate key release
pyautogui.press()
;: # is to call keyDown() & keyUp() to simulate a key press;: The
pyautogui.typewrite('this',0.5)
first parameter is the input content, and the second parameter is the interval time between each character;
pyautogui.typewrite(['T','h','i','s'])
:typewrite You can also pass in a single letter list;
For example:
pyautogui.keyDown('shift') # 按下shift
pyautogui.press('4') # 按下 4
pyautogui.keyUp('shift') # 释放 shift
Output: $;
pyautogui.typewrite('$$$$', 0.5)
Slow output: $$$
2. Keyboard special keys
Sometimes we need to enter some special keys, such as the left arrow, which are represented by corresponding keyboard strings, for example:
pyautogui.typewrite(['T','i','s','left','left','h',]) # 输出:This
Explanation: The left here is the arrow to the left; there are many other keyboard strings, please refer to the following table:
Keyboard string | Description |
---|---|
enter (or return or \n) | Carriage return |
esc | ESC |
shiftleft, shiftright | Left and right SHIFT keys |
altleft, altright | Left and right ALT keys |
ctrlleft, ctrlright | Left and right CTRL keys |
tab (\t) | TAB键 |
backspace, delete | BACKSPACE 、DELETE键 |
pageup, pagedown | PAGE UP 和 PAGE DOWN键 |
home, end | HOME and END keys |
up, down, left,right | Arrow keys |
f1, f2, f3 ... f12 | F1…….F12 key |
volumemute, volumedown,volumeup | The sound becomes louder and quieter (some keyboards do not) |
pause | PAUSE key, pause key |
capslock | CAPS LOCK key |
numlock | NUM LOCK key |
scrolllock | SCROLLLOCK key |
insert | INSERT key |
printscreen | PRINT SCREEN key |
winleft, winright | Win 键 (windows) |
command | command key (Mac OS X) |
option | option(Mac OS X) |
3. Shortcut keys
If we need to simulate the copy shortcut key ctrl+ c, if we use the previous method, the code is:
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
The order of pressing and releasing the shortcut keys is very important. At this time, we can use pyautogui.hotkey(). This function can accept multiple parameters, press them in the order in which they are passed in, and release them in the reverse order. The above shortcut key ctrl+ ccan change the code to:
pyautogui.hotkey('ctrl','c')
4. Prompt message box
- Prompt Box/Warning Box
import pyautogui
a = pyautogui.alert(text='This is an alert box.', title='Test')
print(a)
The output is as follows: Click OK, the return value is'OK'
- Select box
import pyautogui
a = pyautogui.confirm('选择一项', buttons=['A', 'B', 'C'])
print(a)
The output is as follows: Click the B option, the return value is'B'
- Password input
import pyautogui
a = pyautogui.password('Enter password (text will be hidden)')
print(a)
The output is as follows: input the password, it will be displayed as cipher text, click OK, the return value is the value just entered
- Normal input
import pyautogui
a = pyautogui.prompt('请输入一个数字:')
print(a)
The output is as follows: display as plain text, click OK, the return value is the value just entered;