Skip to content

How can i call function with ahk hotkey? #159

Answered by spyoungtech
Eikosa asked this question in Q&A
Discussion options

You must be logged in to vote

Python callbacks are now supported in v1!

from ahk import AHK

def my_callback():
    print('Hello callback!')

ahk = AHK()
# when WIN + n is pressed, fire `my_callback`
ahk.add_hotkey('#n', callback=my_callback)
ahk.start_hotkeys()  # start the hotkey process thread
ahk.block_forever()  # not strictly needed in all scripts -- stops the script from exiting; sleep forever

Hint: for adding arguments to the callback function, use lambda or functools.partial, or similar.

from functools import partial

def my_callback(msg):
    print(msg)
# ...
ahk.add_hotkey('#n', callback=partial(my_callback, "hello ahk"))
# OR
ahk.add_hotkey('#n', callback=lambda: my_callback("hello ahk"))
# OR
def wrapper(…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@spyoungtech
Comment options

Answer selected by spyoungtech
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #152 on June 23, 2022 00:37.