HotiKeys is a library that provides keyboard and mouse events by using low level Windows hooks.
HotiKeys has no dependancies other than Python 3.5+ itself.
Hotkey
is HotiKey's main class and takes the following arguments:
handler
: acallable
(function/method) that may take a single argument for the event argskey
(optional): anKey
or virtual key code (int
)modifiers
(optional): aniterable
of keysevents
(optional): aniterable
containingKeyState
,EventId
or anint
that represents the identifier of the event message
Hotkey
has the following class attributes for configuration;
keyboard
(bool, default: True): Hooks keyboard events if Truemouse
(bool, default: True): Hooks mouse events if Trueno_repeat
(bool, default: True): Blocks subsequent key down events when a key is being hold if Truepurge_delay
(float, default: 10.000): The time in seconds until pressed keys are removed (in case they're not correctly removed during key up events)threaded
(bool, default: True): Whether the hooks should be registered in new threads to prevent blocking the current thread.
(Note: keyboard
, mouse
and threaded
have to be set before the hooks are placed, which happens when the first instance of the class is made.)
(Note: these are class wide attributes and affect each instance of Hotkey
. You can subclass and override these attributes if you wish to have separate configurations.)
import time
from hotikeys import Hotkey, Key, KeyState, newhotkey
def handler(args):
print(Key[args.vkey], args.event.state)
def on_ctrl_shift_a():
print('Ctrl-Shift-A pressed!')
@newhotkey(Key.LButton)
def lmb_hotkey(args):
print('Click, click! ({x}, {y}'.format(x=args.x, y=args.y))
Hotkey(handler, events=(KeyState.Down, KeyState.Up))
Hotkey(on_ctrl_shift_a, Key.A, (Key.LControl, Key.LShift))
time.sleep(5) # Keep running for 5 seconds to prevent immediate exit
Another example can be found in tests/manual_test.py, which includes an example of a concurrent loop.
Currently M.I.A. Sadly enough the docs were eaten by a vicious bulldog on their journey here. Hopefully a replacement will find their way here... soon™.