Skip to content


To set up a macro, add a macro to the keymap and define its function. By default, Fn is configured as macro number 0:

from keyboard import *

keyboard = Keyboard()
keyboard.keymap = (
        ESC,   1,   2,   3,   4,   5,   6,   7,   8,   9,   0, '-', '=', BACKSPACE,
        TAB,   Q,   W,   E,   R,   T,   Y,   U,   I,   O,   P, '[', ']', '|',
        CAPS,  A,   S,  D,   F,   G,   H,   J,   K,   L, ';', '"',    ENTER,
        LSHIFT,Z,   X,   C,   V,   B,   N,   M, ',', '.', '/',         RSHIFT,
        LCTRL, LGUI, LALT,          SPACE,        RALT, MENU,  MACRO(0), RCTRL

def macro_handler(dev, n, is_down):
    if is_down:
        dev.send_text('You pressed macro #{}\n'.format(n))
        dev.send_text('You released macro #{}\n'.format(n))

keyboard.macro_handler = macro_handler


Use a macro to launch the Calculator application on Windows

Replace the function macro_handler with the following code to launch the Calculator with a single keystroke:

def macro_handler(dev, n, is_down):
    if is_down and n == 0:
        dev.send(GUI, R)

Automatic typing

Use the following code to read a file in the keyboard's USB storage and type its content automatically:

def macro_handler(dev, n, is_down):
    if is_down and n == 0:
        with open('', 'r') as f:
            for line in f:

Repeated key strokes

Use a macro to trigger a repeated sequence of key presses until a new key is pressed:

def macro_handler(dev, n, is_down):
    if n == 0 and not is_down:
        t = time.time()
        dt = 0.01                   # seconds
        while dev.scan() < 2:
            if time.time() > t:
                dev.send_text('hello, world\n')
                t += dt