Lesson 49:

Controlling the Keyboard with Python

Python can be used to control the keyboard and mouse, which allows us to automate any program that uses these as inputs.

Graphical User Interface (GUI) Automation is particularly useful for repetative clicking or keyboard entry. The program's own module will probably deliver better programmatic performance, but GUI automation is more broadly applicable.

We will be using the pyautogui module. Lesson 48 details how to install this package.


In [2]:
import pyautogui

This lesson will control all the keyboard controlling functions in the module.

The typewrite() function will type text into a given textbox. It may be useful to use the mouse operators to navigate and click into a field before running.


In [20]:
# Writes to the cell right below (70 pixels down)
pyautogui.moveRel(0,70)
pyautogui.click()
pyautogui.typewrite('Hello world!')
Hello world!

Again, to simulate more human interaction, we can an interval parameter like duration before.


In [21]:
# Writes to the cell right below (70 pixels down)
pyautogui.moveRel(0,70)
pyautogui.click()
pyautogui.typewrite('Hello world!', interval=0.2)
Hello world!

For more complex characters, we can pass a list of complex characters, like the arrow keys, shift, etc.


In [25]:
# Writes to the cell right below (70 pXixels down)
pyautogui.moveRel(0,70)
pyautogui.click()
pyautogui.typewrite(['a','b','left','left','X','Y'], interval=1)

In [ ]:
XYab

A list of keys are available in the KEYBOARD_KEYS


In [26]:
pyautogui.KEYBOARD_KEYS


Out[26]:
['\t',
 '\n',
 '\r',
 ' ',
 '!',
 '"',
 '#',
 '$',
 '%',
 '&',
 "'",
 '(',
 ')',
 '*',
 '+',
 ',',
 '-',
 '.',
 '/',
 '0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9',
 ':',
 ';',
 '<',
 '=',
 '>',
 '?',
 '@',
 '[',
 '\\',
 ']',
 '^',
 '_',
 '`',
 'a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g',
 'h',
 'i',
 'j',
 'k',
 'l',
 'm',
 'n',
 'o',
 'p',
 'q',
 'r',
 's',
 't',
 'u',
 'v',
 'w',
 'x',
 'y',
 'z',
 '{',
 '|',
 '}',
 '~',
 'accept',
 'add',
 'alt',
 'altleft',
 'altright',
 'apps',
 'backspace',
 'browserback',
 'browserfavorites',
 'browserforward',
 'browserhome',
 'browserrefresh',
 'browsersearch',
 'browserstop',
 'capslock',
 'clear',
 'convert',
 'ctrl',
 'ctrlleft',
 'ctrlright',
 'decimal',
 'del',
 'delete',
 'divide',
 'down',
 'end',
 'enter',
 'esc',
 'escape',
 'execute',
 'f1',
 'f10',
 'f11',
 'f12',
 'f13',
 'f14',
 'f15',
 'f16',
 'f17',
 'f18',
 'f19',
 'f2',
 'f20',
 'f21',
 'f22',
 'f23',
 'f24',
 'f3',
 'f4',
 'f5',
 'f6',
 'f7',
 'f8',
 'f9',
 'final',
 'fn',
 'hanguel',
 'hangul',
 'hanja',
 'help',
 'home',
 'insert',
 'junja',
 'kana',
 'kanji',
 'launchapp1',
 'launchapp2',
 'launchmail',
 'launchmediaselect',
 'left',
 'modechange',
 'multiply',
 'nexttrack',
 'nonconvert',
 'num0',
 'num1',
 'num2',
 'num3',
 'num4',
 'num5',
 'num6',
 'num7',
 'num8',
 'num9',
 'numlock',
 'pagedown',
 'pageup',
 'pause',
 'pgdn',
 'pgup',
 'playpause',
 'prevtrack',
 'print',
 'printscreen',
 'prntscrn',
 'prtsc',
 'prtscr',
 'return',
 'right',
 'scrolllock',
 'select',
 'separator',
 'shift',
 'shiftleft',
 'shiftright',
 'sleep',
 'stop',
 'subtract',
 'tab',
 'up',
 'volumedown',
 'volumemute',
 'volumeup',
 'win',
 'winleft',
 'winright',
 'yen',
 'command',
 'option',
 'optionleft',
 'optionright']

These are case-sensitive, but often map to the same function anyway.


In [28]:
pyautogui.typewrite('F1') 
pyautogui.typewrite('f1')

We can also pass variables in hotkey mode, i.e. pressed together.


In [33]:
# Simulates ctrl + alt + delete
pyautogui.hotkey('ctrl','alt','delete')

Recap

  • Controlling the mouse and keyboard is called GUI automation.
  • The pyautogui module has many functions to control the mouse and keyboard.
  • The pyautogui.typewrite() function pases virtual keypresses from a string to a highlighted textbox.
  • It can take a string, a list of strings, and also takes an interval parameter for speed.
  • Passing a list of strings to typewrite() lets you use complex keyboard keys, like 'shift' or 'f1'.
  • These keyboard key strings are available in the pyautogui.KEYBOARD_KEYS list.
  • pyautogui.press() will press a single key.
  • pyautogui.hotkey() can be used for combined keys and keyboard shortcuts, like Ctrl+Alt+Delete.