Ahk 2
Ahk 2
Release 0.8.1
1 Quickstart 3
1.1 Installing AHK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 API 5
2.1 Autohotkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.6 Registery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.7 Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.8 The Script Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.9 Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.10 Utils . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.11 Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 ahk 29
4 Installation 31
5 Usage 33
6 Examples 35
6.1 Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.2 Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.3 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.3.1 Getting windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.3.2 Working with windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.4 Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.5 Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.6 GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.7 non-blocking modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.8 Add directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.9 Run arbitrary AutoHotkey scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.9.1 Communicating data from ahk to Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.10 Experimental features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6.10.1 Hotkeys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6.10.2 ActionChain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6.10.3 find_window/find_windows methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.11 Errors and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.12 Non-Python dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
i
7 Contributing 43
8 Similar projects 45
Index 51
ii
ahk Documentation, Release 0.8.1
GitHub
CONTENTS: 1
ahk Documentation, Release 0.8.1
2 CONTENTS:
CHAPTER
ONE
QUICKSTART
ahk = AHK(executable_path='C:\\Path\\To\\AutoHotkey.exe')
3
ahk Documentation, Release 0.8.1
4 Chapter 1. Quickstart
CHAPTER
TWO
API
This part of the documentation is intended for developers looking to contribute to this project or discover more about
the programming interface. A lot of this is auto-generated documentation from the docstrings.
2.1 Autohotkey
ahk.window.WindowMixin
ahk.mouse.MouseMixin
ahk.keyboard.KeyboardMixin
ahk.screen.ScreenMixin
ahk.sound.SoundMixin
ahk.registery.RegisteryMixin
ahk.gui.GUIMixin
5
ahk Documentation, Release 0.8.1
2.2 Directives
__init__(milliseconds=0, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.Directive(**kwargs)
Simple directive class They are designed to be hashable and comparable with string equivalent of AHK directive.
Directives that don’t require arguments do not need to be instantiated.
__init__(**kwargs)
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.DirectiveMeta
Overrides __str__ so directives with no arguments can be used without instantiation Overrides __hash__ to
make objects ‘unique’ based upon a hash of the str representation
class ahk.directives.ErrorStdOut(**kwargs)
class ahk.directives.HotKeyInterval(milliseconds=0, **kwargs)
class ahk.directives.HotKeyModifierTimeout(milliseconds=0, **kwargs)
class ahk.directives.Include(include_name, **kwargs)
__init__(include_name, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.IncludeAgain(include_name, **kwargs)
class ahk.directives.InputLevel(level, **kwargs)
__init__(level, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.InstallKeybdHook(**kwargs)
class ahk.directives.InstallMouseHook(**kwargs)
class ahk.directives.KeyHistory(limit=40, **kwargs)
__init__(limit=40, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.MaxHotkeysPerInterval(value, **kwargs)
__init__(value, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.MaxMem(megabytes, **kwargs)
__init__(megabytes, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
6 Chapter 2. API
ahk Documentation, Release 0.8.1
class ahk.directives.MaxThreads
__init__()
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.MaxThreadsBuffer
__init__()
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.MaxThreadsPerHotkey
__init__()
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.MenuMaskKey
__init__()
Initialize self. See help(type(self)) for accurate signature.
class ahk.directives.NoEnv(**kwargs)
class ahk.directives.NoTrayIcon(**kwargs)
class ahk.directives.Persistent(**kwargs)
class ahk.directives.SingleInstance(**kwargs)
class ahk.directives.UseHook(**kwargs)
class ahk.directives.Warn(**kwargs)
class ahk.directives.WinActivateForce(**kwargs)
2.3 Keyboard
2.3. Keyboard 7
ahk Documentation, Release 0.8.1
hotkey(*args, **kwargs)
Convenience function for creating Hotkey instance using current engine.
Parameters
• hotkey – The hotkey to use (AutoHotkey syntax)
• script – The script to execute when the hotkey is activated (AutoHotkey code as a
string)
Returns an Hotkey instance
key_down(key, blocking=True)
Press down a key (without releasing it)
Parameters
• key –
• blocking –
Returns
key_press(key, release=True, blocking=True)
Press and (optionally) release a single key
Parameters
• key –
• release –
Returns
key_release(key, blocking=True)
Release a key that is currently in pressed down state
Parameters key –
Returns
key_state(key_name, mode=None)
Check the state of a key.
https://autohotkey.com/docs/commands/GetKeyState.htm
Parameters
• key_name – the name of the key (or virtual key code)
• mode – see AHK docs
Return type bool
Returns True if pressed down, else False
key_up(key, blocking=True)
Alias for :meth:~`KeyboardMixin.key_release`
key_wait(key_name, timeout=None, logical_state=False, released=False)
Wait for key to be pressed or released (default is pressed; specify released=True to wait for key
release).
https://autohotkey.com/docs/commands/KeyWait.htm
Parameters
• key_name – The name of the key
8 Chapter 2. API
ahk Documentation, Release 0.8.1
• timeout (Optional[int]) – how long (in seconds) to wait for the key. If not specified,
waits indefinitely
• logical_state – Check the logical state of the key, which is the state that the OS and
the active window believe the key to be in (not necessarily the same as the physical state).
This option is ignored for joystick buttons.
• released – Set to True to wait for the key to be released rather than pressed
Returns None
Raises TimeoutError – if the key was not pressed (or released, if specified) within timeout
send(s, raw=False, delay=None, blocking=True)
https://autohotkey.com/docs/commands/Send.htm
Parameters
• s–
• raw –
• delay –
• blocking – if True, waits until script finishes, else returns immediately.
Returns
send_event(s, delay=None)
https://autohotkey.com/docs/commands/Send.htm
Parameters
• s–
• delay –
Returns
send_input(s, blocking=True)
https://autohotkey.com/docs/commands/Send.htm
Parameters
• s–
• blocking –
Returns
send_play(s)
https://autohotkey.com/docs/commands/Send.htm
Parameters s –
Returns
send_raw(s, delay=None)
https://autohotkey.com/docs/commands/Send.htm
Parameters
• s–
• delay –
Returns
2.3. Keyboard 9
ahk Documentation, Release 0.8.1
set_capslock_state(state=None)
https://www.autohotkey.com/docs/commands/SetNumScrollCapsLockState.htm
Sets capslock state
Parameters state (str) – the desired state (“on” or “off”). Can also be True/False. If omitted,
toggles capslock state.
Returns
type(s, blocking=True)
Sends keystrokes using send_input, also escaping the string for use in AHK.
Parameters
• s – the string to type
• blocking – if True, waits until script finishes, else returns immediately.
2.4 Keys
The ahk.keys module contains some useful constants and classes for working with keys.
class ahk.keys.KEYS
KEYS constants REF: https://autohotkey.com/docs/KeyList.htm
ALT = KeyModifier(key_name='Alt')
Alt = KeyModifier(key_name='Alt')
BACKSPACE = Key(key_name='Backspace')
Backspace = Key(key_name='Backspace')
CAPS_LOCK = Key(key_name='CapsLock')
CONTROL = KeyModifier(key_name='Control')
CTRL = KeyModifier(key_name='Control')
CapsLock = Key(key_name='CapsLock')
Control = KeyModifier(key_name='Control')
Ctrl = KeyModifier(key_name='Control')
DEL = Key(key_name='Delete')
DELETE = Key(key_name='Delete')
DOWN = Key(key_name='Down')
Del = Key(key_name='Delete')
Delete = Key(key_name='Delete')
Down = Key(key_name='Down')
ENTER = Key(key_name='Enter')
ESCAPE = Key(key_name='Escape')
Enter = Key(key_name='Enter')
F1 = Key(key_name='F1')
F10 = Key(key_name='F10')
10 Chapter 2. API
ahk Documentation, Release 0.8.1
F11 = Key(key_name='F11')
F12 = Key(key_name='F12')
F13 = Key(key_name='F13')
F14 = Key(key_name='F14')
F15 = Key(key_name='F15')
F16 = Key(key_name='F16')
F17 = Key(key_name='F17')
F18 = Key(key_name='F18')
F19 = Key(key_name='F19')
F2 = Key(key_name='F2')
F20 = Key(key_name='F20')
F21 = Key(key_name='F21')
F22 = Key(key_name='F22')
F23 = Key(key_name='F23')
F24 = Key(key_name='F24')
F3 = Key(key_name='F3')
F4 = Key(key_name='F4')
F5 = Key(key_name='F5')
F6 = Key(key_name='F6')
F7 = Key(key_name='F7')
F8 = Key(key_name='F8')
F9 = Key(key_name='F9')
JOY1 = Key(key_name='Joy1')
JOY10 = Key(key_name='Joy10')
JOY11 = Key(key_name='Joy11')
JOY12 = Key(key_name='Joy12')
JOY13 = Key(key_name='Joy13')
JOY14 = Key(key_name='Joy14')
JOY15 = Key(key_name='Joy15')
JOY16 = Key(key_name='Joy16')
JOY17 = Key(key_name='Joy17')
JOY18 = Key(key_name='Joy18')
JOY19 = Key(key_name='Joy19')
JOY2 = Key(key_name='Joy2')
JOY20 = Key(key_name='Joy20')
JOY21 = Key(key_name='Joy21')
2.4. Keys 11
ahk Documentation, Release 0.8.1
JOY22 = Key(key_name='Joy22')
JOY23 = Key(key_name='Joy23')
JOY24 = Key(key_name='Joy24')
JOY25 = Key(key_name='Joy25')
JOY26 = Key(key_name='Joy26')
JOY27 = Key(key_name='Joy27')
JOY28 = Key(key_name='Joy28')
JOY29 = Key(key_name='Joy29')
JOY3 = Key(key_name='Joy3')
JOY30 = Key(key_name='Joy30')
JOY31 = Key(key_name='Joy31')
JOY32 = Key(key_name='Joy32')
JOY4 = Key(key_name='Joy4')
JOY5 = Key(key_name='Joy5')
JOY6 = Key(key_name='Joy6')
JOY7 = Key(key_name='Joy7')
JOY8 = Key(key_name='Joy8')
JOY9 = Key(key_name='Joy9')
Joy1 = Key(key_name='Joy1')
Joy10 = Key(key_name='Joy10')
Joy11 = Key(key_name='Joy11')
Joy12 = Key(key_name='Joy12')
Joy13 = Key(key_name='Joy13')
Joy14 = Key(key_name='Joy14')
Joy15 = Key(key_name='Joy15')
Joy16 = Key(key_name='Joy16')
Joy17 = Key(key_name='Joy17')
Joy18 = Key(key_name='Joy18')
Joy19 = Key(key_name='Joy19')
Joy2 = Key(key_name='Joy2')
Joy20 = Key(key_name='Joy20')
Joy21 = Key(key_name='Joy21')
Joy22 = Key(key_name='Joy22')
Joy23 = Key(key_name='Joy23')
Joy24 = Key(key_name='Joy24')
Joy25 = Key(key_name='Joy25')
12 Chapter 2. API
ahk Documentation, Release 0.8.1
Joy26 = Key(key_name='Joy26')
Joy27 = Key(key_name='Joy27')
Joy28 = Key(key_name='Joy28')
Joy29 = Key(key_name='Joy29')
Joy3 = Key(key_name='Joy3')
Joy30 = Key(key_name='Joy30')
Joy31 = Key(key_name='Joy31')
Joy32 = Key(key_name='Joy32')
Joy4 = Key(key_name='Joy4')
Joy5 = Key(key_name='Joy5')
Joy6 = Key(key_name='Joy6')
Joy7 = Key(key_name='Joy7')
Joy8 = Key(key_name='Joy8')
Joy9 = Key(key_name='Joy9')
LAlt = KeyModifier(key_name='LAlt')
LControl = KeyModifier(key_name='LControl')
LCtrl = KeyModifier(key_name='LControl')
LEFT = Key(key_name='Left')
LEFT_ALT = KeyModifier(key_name='LAlt')
LEFT_CONTROL = KeyModifier(key_name='LControl')
LEFT_SHIFT = KeyModifier(key_name='LShift')
LEFT_WIN = KeyModifier(key_name='LWin')
LShift = KeyModifier(key_name='LShift')
LWin = KeyModifier(key_name='LWin')
Left = Key(key_name='Left')
NUMPAD0 = Key(key_name='Numpad0')
NUMPAD1 = Key(key_name='Numpad1')
NUMPAD2 = Key(key_name='Numpad2')
NUMPAD3 = Key(key_name='Numpad3')
NUMPAD4 = Key(key_name='Numpad4')
NUMPAD5 = Key(key_name='Numpad5')
NUMPAD6 = Key(key_name='Numpad6')
NUMPAD7 = Key(key_name='Numpad7')
NUMPAD8 = Key(key_name='Numpad8')
NUMPAD9 = Key(key_name='Numpad9')
NUMPAD_ADD = Key(key_name='NumpadAdd')
2.4. Keys 13
ahk Documentation, Release 0.8.1
NUMPAD_DEL = Key(key_name='NumpadDel')
NUMPAD_DIV = Key(key_name='NumpadDiv')
NUMPAD_DOT = Key(key_name='NumpadDot')
NUMPAD_ENTER = Key(key_name='NumpadEnter')
NUMPAD_MULT = Key(key_name='NumpadMult')
NUMPAD_SUB = Key(key_name='NumpadSub')
NUM_LOCK = Key(key_name='NumLock')
NumLock = Key(key_name='NumLock')
Numpad0 = Key(key_name='Numpad0')
Numpad1 = Key(key_name='Numpad1')
Numpad2 = Key(key_name='Numpad2')
Numpad3 = Key(key_name='Numpad3')
Numpad4 = Key(key_name='Numpad4')
Numpad5 = Key(key_name='Numpad5')
Numpad6 = Key(key_name='Numpad6')
Numpad7 = Key(key_name='Numpad7')
Numpad8 = Key(key_name='Numpad8')
Numpad9 = Key(key_name='Numpad9')
NumpadAdd = Key(key_name='NumpadAdd')
NumpadDel = Key(key_name='NumpadDel')
NumpadDiv = Key(key_name='NumpadDiv')
NumpadDot = Key(key_name='NumpadDot')
NumpadEnter = Key(key_name='NumpadEnter')
NumpadMult = Key(key_name='NumpadMult')
NumpadSub = Key(key_name='NumpadSub')
RAlt = KeyModifier(key_name='RAlt')
RControl = KeyModifier(key_name='RControl')
RCtrl = KeyModifier(key_name='RControl')
RIGHT = Key(key_name='Right')
RIGHT_ALT = KeyModifier(key_name='RAlt')
RIGHT_CONTROL = KeyModifier(key_name='RControl')
RIGHT_SHIFT = KeyModifier(key_name='RShift')
RIGHT_WIN = KeyModifier(key_name='RWin')
RShift = KeyModifier(key_name='RShift')
RWin = KeyModifier(key_name='RWin')
Right = Key(key_name='Right')
14 Chapter 2. API
ahk Documentation, Release 0.8.1
SCROLL_LOCK = Key(key_name='ScrollLock')
SHIFT = KeyModifier(key_name='Shift')
SPACE = Key(key_name='Space')
ScrollLock = Key(key_name='ScrollLock')
Shift = KeyModifier(key_name='Shift')
TAB = Key(key_name='Tab')
Tab = Key(key_name='Tab')
UP = Key(key_name='Up')
Up = Key(key_name='Up')
WIN = KeyModifier(key_name='Win')
Win = KeyModifier(key_name='Win')
2.5 Mouse
2.5. Mouse 15
ahk Documentation, Release 0.8.1
• kwargs –
Returns
mouse_drag(x, y=None, *, from_position=None, speed=None, button=1, relative=None, block-
ing=True, mode=None)
Click and drag the mouse
Parameters
• x–
• y–
• from_position – (x,y) tuple of an optional starting position. Current position is used
if omitted
• speed –
• button – The button the click and drag; defaults to left mouse button
• relative – click and drag to a relative position rather than an absolute position
• blocking –
• mode –
Returns
mouse_move(*args, **kwargs)
REF: https://www.autohotkey.com/docs/commands/MouseMove.htm
Parameters
• x – the x coordinate to move to. If omitted, current position is used
• y – the y coordinate to move to. If omitted, current position is used
• speed – 0 (fastest) to 100 (slowest). Can be a callable or string AHK expression
• relative – Move the mouse realtive to current position rather than absolute x,y coordi-
nates
• mode –
• blocking –
Returns
property mouse_position
property mouse_speed
mouse_wheel(direction, *args, **kwargs)
Convenience function for ‘clicking’ the mouse wheel in a given direction.
Parameters
• direction – the string ‘up’ or ‘down’
• args – args passed to click
• kwargs – keyword args passed to click
Returns
right_click(*args, **kwargs)
Convenience function clicking right mouse button. Equivalent to click with button='R'
Parameters
16 Chapter 2. API
ahk Documentation, Release 0.8.1
• args –
• kwargs –
Returns
wheel_down(*args, **kwargs)
Convenience function for click with wheel down button
Parameters
• args –
• kwargs –
Returns
wheel_up(*args, **kwargs)
Convenience function for click with wheel up button
Parameters
• args –
• kwargs –
Returns
ahk.mouse.resolve_button(button)
Resolve a string of a button name to a canonical name used for AHK script
Parameters button (str) –
Returns
2.6 Registery
delete(*args, **kwargs)
read(*args, **kwargs)
reg_delete(key_name, value_name='')
Delete registery
Reference: https://www.autohotkey.com/docs/commands/RegDelete.htm
Arguments: key_name {str} – RegEdit
Keyword Arguments: value_name {str} – TODO (default: {“”})
2.6. Registery 17
ahk Documentation, Release 0.8.1
reg_read(key_name, value_name='')
Read registery
Reference: https://www.autohotkey.com/docs/commands/RegRead.htm
Arguments: key_name {str} – RegEdit
Keyword Arguments: value_name {str} – TODO (default: {“”})
Returns: str – Registery value
reg_set_view(reg_view)
Set registery view
Reference: https://www.autohotkey.com/docs/commands/SetRegView.htm
Arguments: reg_view {str} – Registery view
set_view(*args, **kwargs)
write(*args, **kwargs)
2.7 Screen
18 Chapter 2. API
ahk Documentation, Release 0.8.1
2.7. Screen 19
ahk Documentation, Release 0.8.1
20 Chapter 2. API
ahk Documentation, Release 0.8.1
2.9 Sound
get_volume(device_number=1)
REF: https://autohotkey.com/docs/commands/SoundGetWaveVolume.htm
Parameters device_number –
Returns
set_volume(value, device_number=1)
REF: https://autohotkey.com/docs/commands/SoundSetWaveVolume.htm
Parameters
• value – percent volume to set volume to
• device_number –
Returns
sound_beep(frequency=523, duration=150)
REF: https://autohotkey.com/docs/commands/SoundBeep.htm
2.9. Sound 21
ahk Documentation, Release 0.8.1
Parameters
• frequency – number between 37 and 32767
• duration – how long in milliseconds to play the beep
Returns None
sound_get(device_number=1, component_type='MASTER', control_type='VOLUME')
REF: https://autohotkey.com/docs/commands/SoundGet.htm
Parameters
• device_number –
• component_type –
• control_type –
Returns
sound_play(filename, blocking=True)
REF: https://autohotkey.com/docs/commands/SoundPlay.htm
Parameters
• filename –
• blocking –
• wait –
Returns
sound_set(value, device_number=1, component_type='MASTER', control_type='VOLUME')
REF: https://autohotkey.com/docs/commands/SoundSet.htm
Parameters
• value –
• device_number –
• component_type –
• control_type –
Returns
2.10 Utils
ahk.utils.escape_sequence_replace(s)
Replace Python escape sequences with AHK equivalent escape sequences Additionally escapes some other
characters for AHK escape sequences. Intended for use with AHK Send command functions.
Note: This DOES NOT provide ANY assurances against accidental or malicious injection. Does NOT escape
quotes.
ahk.utils.make_logger(name)
22 Chapter 2. API
ahk Documentation, Release 0.8.1
2.11 Window
class ahk.window.Control
__init__()
Initialize self. See help(type(self)) for accurate signature.
click()
REF: https://www.autohotkey.com/docs/commands/ControlClick.htm
Returns
focus()
REF: https://www.autohotkey.com/docs/commands/ControlFocus.htm
Returns
get(key)
REF: https://www.autohotkey.com/docs/commands/ControlGet.htm
Parameters key –
Returns
has_focus()
move()
REF: https://www.autohotkey.com/docs/commands/ControlMove.htm
Returns
property position
REF: https://www.autohotkey.com/docs/commands/ControlGetPos.htm
Returns
send(raw=False)
REF: https://www.autohotkey.com/docs/commands/ControlSend.htm
Parameters raw –
Returns
property text
REF: https://www.autohotkey.com/docs/commands/ControlGetText.htm
Returns
class ahk.window.Window(engine, ahk_id, encoding=None)
MAXIMIZED = '1'
MINIMIZED = '-1'
NON_MIN_NON_MAX = '0'
__init__(engine, ahk_id, encoding=None)
Initialize self. See help(type(self)) for accurate signature.
activate()
Activate the window.
See also: WinActivate
2.11. Window 23
ahk Documentation, Release 0.8.1
Returns
activate_bottom()
Calls WinActivateBottom on the window
Returns
property active
property always_on_top
Return type bool
property class_name
click(x, y, blocking=False)
Click at an x/y location on the screen. Uses ControlClick https://autohotkey.com/docs/commands/
ControlClick.htm
close(seconds_to_wait='')
Closes the Window. See also: WinClose
Parameters seconds_to_wait –
Returns
disable()
Distable the window
Returns
enable()
Enable the window
Returns
property exist
classmethod from_mouse_position(engine, **kwargs)
classmethod from_pid(engine, pid, **kwargs)
get(subcommand)
property height
hide()
Hides the window. See also: WinHide
Returns
kill(seconds_to_wait='')
maximize()
maximize the window
Returns
property maximized
minimize()
minimize the window
Returns
property minimized
24 Chapter 2. API
ahk Documentation, Release 0.8.1
2.11. Window 25
ahk Documentation, Release 0.8.1
Parameters seconds_to_wait –
Returns
wait_not_active(seconds_to_wait='')
Parameters seconds_to_wait –
Returns
property width
class ahk.window.WindowMixin(*args, **kwargs)
__init__(*args, **kwargs)
This class is typically not used directly. AHK components inherit from this class and the arguments for
this class should usually be passed in to AHK.
Parameters
• executable_path – the path to the AHK executable. If not provided explicitly in this
argument, the path to the AHK executable is resolved in the following order:
– The AHK_PATH environment variable, if present
– DEFAULT_EXECUTABLE_PATH if the file exists
If environment variable not present, tries to look for ‘AutoHotkey.exe’ or ‘Auto-
HotkeyA32.exe’ with shutil.which
• directives – a set of directives to apply to all generated AHK scripts
Raises ExecutableNotFound – if AHK executable cannot be found or the specified file
does not exist
property active_window
find_window(func=None, **kwargs)
Like find_windows but only returns the first found window
Parameters
• func –
• kwargs –
Returns a Window object or None if no matching window is found
find_window_by_class(*args, **kwargs)
Parameters
• args –
• kwargs –
Returns a Window object or None if no matching window is found
find_window_by_text(*args, **kwargs)
Parameters
• args –
• kwargs –
Returns a Window object or None if no matching window is found
26 Chapter 2. API
ahk Documentation, Release 0.8.1
find_window_by_title(*args, **kwargs)
Like find_windows_by_title but only returns the first result.
Returns a Window object or None if no matching window is found
find_windows(func=None, **kwargs)
Find all matching windows
Parameters
• func – a callable to filter windows
• exact (bool) – if False (the default) partial matches are found. If True, only exact
matches are returned
• kwargs – keywords of attributes of the window (has no effect if func is provided)
Returns a generator containing any matching Window objects.
find_windows_by_class(class_name, exact=False)
Parameters
• class_name –
• exact –
Returns a generator containing any matching Window objects.
find_windows_by_text(text, exact=False)
Parameters
• text –
• exact –
Returns a generator containing any matching Window objects.
find_windows_by_title(title, exact=False)
Equivalent to find_windows(title=title)`
Note that title is a bytes object
Parameters
• title (bytes) –
• exact –
Returns
win_get(title='', text='', exclude_title='', exclude_text='', encoding=None)
win_set(subcommand, *args, blocking=True)
windows()
Returns a list of windows
Returns
exception ahk.window.WindowNotFoundError
2.11. Window 27
ahk Documentation, Release 0.8.1
28 Chapter 2. API
CHAPTER
THREE
AHK
29
ahk Documentation, Release 0.8.1
30 Chapter 3. ahk
CHAPTER
FOUR
INSTALLATION
31
ahk Documentation, Release 0.8.1
32 Chapter 4. Installation
CHAPTER
FIVE
USAGE
ahk = AHK()
33
ahk Documentation, Release 0.8.1
34 Chapter 5. Usage
CHAPTER
SIX
EXAMPLES
Non-exhaustive examples of some of the functions available with this package. Full documentation coming soon!
6.1 Mouse
ahk = AHK()
6.2 Keyboard
ahk = AHK()
35
ahk Documentation, Release 0.8.1
6.3 Windows
ahk = AHK()
ahk = AHK()
win.send('hello') # Send keys directly to the window (does not need focus!)
win.move(x=200, y=300, width=500, height=800)
36 Chapter 6. Examples
ahk Documentation, Release 0.8.1
6.4 Screen
ahk = AHK()
6.5 Sound
ahk = AHK()
6.4. Screen 37
ahk Documentation, Release 0.8.1
6.6 GUI
ahk = AHK()
ahk.show_tooltip("hello4", second=2, x=10, y=10) #
˓→ToolTip
For some functions, you can also opt for a non-blocking interface, so you can do other stuff while AHK scripts run.
import time
ahk = AHK()
ahk.mouse_position = (200, 200) # Moves the mouse instantly to the start position
start = time.time()
ahk.mouse_move(x=100, y=100, speed=30, blocking=False)
while True: # report mouse position while it moves
t = round(time.time() - start, 4)
position = ahk.mouse_position
print(t, position)
if position == (100, 100):
break
38 Chapter 6. Examples
ahk Documentation, Release 0.8.1
You can add directives that will be added to all generated scripts. For example, to prevent the AHK trayicon from
appearing, you can add the NoTrayIcon directive.
ahk = AHK(directives=[NoTrayIcon])
By default, some directives are automatically added to ensure functionality and are merged with any user-provided
directives.
ahk = AHK()
If you’re writing your own ahk scripts to use with this library, you can use FileAppend with the * parameter to get
data from your ahk script into Python.
Suppose you have a script like so
#Persistent
data := "Hello Data!"
FileAppend, %data%, * ; send data var to stdout
ExitApp
result = ahk.run_script(my_script)
print(result) # Hello Data!
If your autohotkey returns something that can’t be decoded, add the keyword argument decode=False in which
case you’ll get back a CompletedProcess object where stdout (and stderr) will be bytes and you can handle it
however you choose.
Experimental features are things that are minimally functional, (even more) likely to have breaking changes, even for
minor releases.
Github issues are provided for convenience to collect feedback on these features.
6.10.1 Hotkeys
GH-9
Hotkeys now have a primitive implementation. You give it a hotkey (a string the same as in an ahk script, without the
::) and the body of an AHK script to execute as a response to the hotkey.
ahk = AHK()
At this point, the hotkey is active. If you press + , the script Run Notepad will execute.
There is no need to add return to the provided script, as it is provided by the template.
To stop the hotkey call the stop() method.
hotkey.stop()
6.10.2 ActionChain
GH-25
ActionChains let you define a set of actions to be performed in order at a later time.
They work just like the AHK class, except the actions are deferred until the perform method is called.
An additional method sleep is provided to allow for waiting between actions.
ac = ActionChain()
# An Action Chain doesn't perform the actions until perform() is called on the chain
Just like anywhere else, scripts running simultaneously may conflict with one another, so using blocking interfaces is
generally recommended. Currently, there is limited support for interacting with windows in actionchains, you may
want to use win_set)
40 Chapter 6. Examples
ahk Documentation, Release 0.8.1
GH-26
Right now, these are implemented by iterating over all window handles and filtering with Python. They may be
optimized in the future.
AHK.find_windows returns a generator filtering results based on attributes provided as keyword arguments. AHK.
find_window is similar, but returns the first matching window instead of all matching windows.
There are couple convenience functions, but not sure if these will stay around or maybe we’ll add more, depending on
feedback.
• find_windows_by_title
• find_window_by_title
• find_windows_by_text
• find_window_by_text
You can enable debug logging, which will output script text before execution, and some other potentially useful
debugging information.
import logging
logging.basicConfig(level=logging.DEBUG)
ahk = AHK(executable_path='C:\\path\\to\\AutoHotkey.exe')
42 Chapter 6. Examples
CHAPTER
SEVEN
CONTRIBUTING
43
ahk Documentation, Release 0.8.1
44 Chapter 7. Contributing
CHAPTER
EIGHT
SIMILAR PROJECTS
These are some similar projects that are commonly used for automation with Python.
• Pyautogui - Al Sweigart’s creation for cross-platform automation
• Pywinauto - Automation on Windows platforms with Python.
• keyboard - Pure Python cross-platform keyboard hooks/control and hotkeys!
• mouse - From the creators of keyboard, Pure Python mouse control!
• pynput - Keyboard and mouse control
45
ahk Documentation, Release 0.8.1
NINE
• genindex
• modindex
• search
47
ahk Documentation, Release 0.8.1
a
ahk.autohotkey, 5
ahk.directives, 6
ahk.keyboard, 7
ahk.keys, 10
ahk.mouse, 15
ahk.registery, 17
ahk.screen, 18
ahk.script, 20
ahk.sound, 21
ahk.utils, 22
ahk.window, 23
49
ahk Documentation, Release 0.8.1
51
ahk Documentation, Release 0.8.1
52 Index
ahk Documentation, Release 0.8.1
K N
key_down() (ahk.keyboard.KeyboardMixin method), 8 NoEnv (class in ahk.directives), 7
key_press() (ahk.keyboard.KeyboardMixin method), non_max_non_min() (ahk.window.Window prop-
8 erty), 25
key_release() (ahk.keyboard.KeyboardMixin NON_MIN_NON_MAX (ahk.window.Window attribute),
method), 8 23
key_state() (ahk.keyboard.KeyboardMixin method), NoTrayIcon (class in ahk.directives), 7
8 NUM_LOCK (ahk.keys.KEYS attribute), 14
key_up() (ahk.keyboard.KeyboardMixin method), 8 NumLock (ahk.keys.KEYS attribute), 14
key_wait() (ahk.keyboard.KeyboardMixin method), 8 NUMPAD0 (ahk.keys.KEYS attribute), 13
KeyboardMixin (class in ahk.keyboard), 7 Numpad0 (ahk.keys.KEYS attribute), 14
KeyHistory (class in ahk.directives), 6 NUMPAD1 (ahk.keys.KEYS attribute), 13
KEYS (class in ahk.keys), 10 Numpad1 (ahk.keys.KEYS attribute), 14
Index 53
ahk Documentation, Release 0.8.1
54 Index
ahk Documentation, Release 0.8.1
T
TAB (ahk.keys.KEYS attribute), 15
Tab (ahk.keys.KEYS attribute), 15
text() (ahk.window.Control property), 23
text() (ahk.window.Window property), 25
title() (ahk.window.Window property), 25
to_bottom() (ahk.window.Window method), 25
to_top() (ahk.window.Window method), 25
transparent() (ahk.window.Window property), 25
type() (ahk.keyboard.KeyboardMixin method), 10
U
UP (ahk.keys.KEYS attribute), 15
Up (ahk.keys.KEYS attribute), 15
UseHook (class in ahk.directives), 7
W
wait() (ahk.window.Window method), 25
wait_active() (ahk.window.Window method), 25
wait_close() (ahk.window.Window method), 25
wait_not_active() (ahk.window.Window method),
26
Warn (class in ahk.directives), 7
wheel_down() (ahk.mouse.MouseMixin method), 17
wheel_up() (ahk.mouse.MouseMixin method), 17
width() (ahk.window.Window property), 26
WIN (ahk.keys.KEYS attribute), 15
Win (ahk.keys.KEYS attribute), 15
win_get() (ahk.window.WindowMixin method), 27
win_set() (ahk.window.WindowMixin method), 27
WinActivateForce (class in ahk.directives), 7
Window (class in ahk.window), 23
WindowMixin (class in ahk.window), 26
WindowNotFoundError, 27
windows() (ahk.window.WindowMixin method), 27
write() (ahk.registery.RegisteryMixin method), 18
Index 55