Debian Codegraph @

docs/keyboard.rst

raw
1 .. default-role:: code
2
3 ==========================
4 Keyboard Control Functions
5 ==========================
6
7 The write() Function
8 ========================
9
10 The primary keyboard function is ``write()``. This function will type the characters in the string that is passed. To add a delay interval in between pressing each character key, pass an int or float for the ``interval`` keyword argument.
11
12 For example:
13
14 .. code:: python
15
16 >>> pyautogui.write('Hello world!') # prints out "Hello world!" instantly
17 >>> pyautogui.write('Hello world!', interval=0.25) # prints out "Hello world!" with a quarter second delay after each character
18
19 You can only press single-character keys with ``write()``, so you can't press the Shift or F1 keys, for example.
20
21 The press(), keyDown(), and keyUp() Functions
22 =============================================
23
24 To press these keys, call the ``press()`` function and pass it a string from the ``pyautogui.KEYBOARD_KEYS`` such as ``enter``, ``esc``, ``f1``. See `KEYBOARD_KEYS`_.
25
26 For example:
27
28 .. code:: python
29
30 >>> pyautogui.press('enter') # press the Enter key
31 >>> pyautogui.press('f1') # press the F1 key
32 >>> pyautogui.press('left') # press the left arrow key
33
34 The ``press()`` function is really just a wrapper for the ``keyDown()`` and ``keyUp()`` functions, which simulate pressing a key down and then releasing it up. These functions can be called by themselves. For example, to press the left arrow key three times while holding down the Shift key, call the following:
35
36 .. code:: python
37
38 >>> pyautogui.keyDown('shift') # hold down the shift key
39 >>> pyautogui.press('left') # press the left arrow key
40 >>> pyautogui.press('left') # press the left arrow key
41 >>> pyautogui.press('left') # press the left arrow key
42 >>> pyautogui.keyUp('shift') # release the shift key
43
44 To press multiple keys similar to what ``write()`` does, pass a list of strings to ``press()``. For example:
45
46 .. code:: python
47
48 >>> pyautogui.press(['left', 'left', 'left'])
49
50 Or you can set how many presses `left`:
51
52 .. code:: python
53
54 >>> pyautogui.press('left', presses=3)
55
56 To add a delay interval in between each press, pass an int or float for the ``interval`` keyword argument.
57
58 The hold() Context Manager
59 ==========================
60
61 To make holding a key convenient, the ``hold()`` function can be used as a context manager and passed a string from the ``pyautogui.KEYBOARD_KEYS`` such as ``shift``, ``ctrl``, ``alt``, and this key will be held for the duration of the ``with`` context block. See `KEYBOARD_KEYS`_.
62
63 .. code:: python
64
65 >>> with pyautogui.hold('shift'):
66 pyautogui.press(['left', 'left', 'left'])
67
68 . . .is equivalent to this code:
69
70 .. code:: python
71
72 >>> pyautogui.keyDown('shift') # hold down the shift key
73 >>> pyautogui.press('left') # press the left arrow key
74 >>> pyautogui.press('left') # press the left arrow key
75 >>> pyautogui.press('left') # press the left arrow key
76 >>> pyautogui.keyUp('shift') # release the shift key
77
78 The hotkey() Function
79 =====================
80
81 To make pressing hotkeys or keyboard shortcuts convenient, the ``hotkey()`` can be passed several key strings which will be pressed down in order, and then released in reverse order. This code:
82
83 .. code:: python
84
85 >>> pyautogui.hotkey('ctrl', 'shift', 'esc')
86
87 . . .is equivalent to this code:
88
89 .. code:: python
90
91 >>> pyautogui.keyDown('ctrl')
92 >>> pyautogui.keyDown('shift')
93 >>> pyautogui.keyDown('esc')
94 >>> pyautogui.keyUp('esc')
95 >>> pyautogui.keyUp('shift')
96 >>> pyautogui.keyUp('ctrl')
97
98 To add a delay interval in between each press, pass an int or float for the ``interval`` keyword argument.
99
100 KEYBOARD_KEYS
101 =============
102
103 The following are the valid strings to pass to the ``press()``, ``keyDown()``, ``keyUp()``, and ``hotkey()`` functions:
104
105 .. code:: python
106
107 ['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(',
108 ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
109 '8', '9', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`',
110 'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
111 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
112 'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace',
113 'browserback', 'browserfavorites', 'browserforward', 'browserhome',
114 'browserrefresh', 'browsersearch', 'browserstop', 'capslock', 'clear',
115 'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal', 'del', 'delete',
116 'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10',
117 'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20',
118 'f21', 'f22', 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
119 'final', 'fn', 'hanguel', 'hangul', 'hanja', 'help', 'home', 'insert', 'junja',
120 'kana', 'kanji', 'launchapp1', 'launchapp2', 'launchmail',
121 'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',
122 'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6',
123 'num7', 'num8', 'num9', 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn',
124 'pgup', 'playpause', 'prevtrack', 'print', 'printscreen', 'prntscrn',
125 'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select', 'separator',
126 'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab',
127 'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen',
128 'command', 'option', 'optionleft', 'optionright']
129