question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not sure what interface I want hotkeys to take.

keyboard is a project that offers cross-platform hotkeys and keyboard actions.

Not sure if to model after keyboard or do something else.

Here’s a quick mockup I have in mind

myhotkey = Hotkey('CTRL+T', some_script)
myhotkey.start()  #  start the process with the hotkey trigger (e.g. listening in the background)
myhotkey.stop()  #  stop the script

Where some_script would be an ahk script to put under the hotkey declaration.

Another possible idea would be to have hotkeys have the ability to perform callbacks for Python functions, similar to keyboard, but I can’t think of a great way to accomplish this at the moment.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:30 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
spyoungtechcommented, Jan 11, 2021

@Maracko there are some improvements on the way that may help your use case. But let me try to answer your questions:

proper way to close the ahk executable on exit cause it sometimes is left active even after my python script stops?

This library uses finalizer hooks to ensure the AHK process (like those started for hotkeys) is closed. However, as mentioned in the official docs using os._exit will not allow cleanup handlers to run. As such, the hotkey process may be left running.

So, in short, if your python script exits normally (even with an exception), there shouldn’t be anything you need to do to ensure the ahk executable terminates.

There are, however, cases where cleanup might not run, such as when a Python fatal internal error is detected, or when os._exit() is called.

In your example, changing os._exit(1) to sys.exit(1) should ensure the AHK process is closed.

there a way to start an event loop so the app doesn’t close immediately after execution?

I’m 100% sure what you mean here.

For Hotkeys, the main Python script must continue to run for the hotkey to continue running. If your app consists only of the AHK hotkey, then you can do something like this:

ahk = AHK()
hotkey = ahk.hotkey("#n", "Run Notepad")
hotkey.start() # call .start() to start the AHK process listening to the hotkey
while True: # keep the main thread alive so hotkey will stay running
    try:
        time.sleep(0.5)
    except KeyboardInterrupt:
        hotkey.stop()  # technically you don't have to call stop. 
                       # The process will stop on its own when the script ends
        break

There are a few improvements on the way that may help your situation:

  1. as mentioned earlier Python callbacks for hotkeys are going to implemented in the near future. So something like if ahk.key_state("Control") and ahk.key_state("Shift") and ahk.key_state("1") could reasonably be re-written as a hotkey.

  2. There is a daemon feature coming in #111 that will offer greatly improved performance. Currently, each AHK command runs in its own process. The daemon feature will allow AHK commands to be executed in a single long-running AHK process, which should improve efficiency by an order of magnitude, especially for scripts that call methods in rapid succession.

Lastly, I may add a wait_forever method to hotkeys… but it will likely just be something simple, like the while loop I wrote above.

Hope that answers your question. Let me know if you still have other questions or always feel free to open another issue.

1reaction
gtusrcommented, Jul 13, 2019

Since we already have the python keyboard project for creating powerful hotkeys from scratch, maybe a useful goal for this project is to incorporate hotkey functionality in a way that makes it as easy as possible to port existing autohotkey hotkeys to this framework.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keyboard shortcuts in Windows - Microsoft Support
Learn how to navigate Windows using keyboard shortcuts. Explore a full list of taskbar, command prompt, and general Windows shortcuts.
Read more >
Special keys, Keyboard shortcuts, Function keys and Hotkeys
Hotkeys are combinations of keys that when pressed at the same time let you quickly access/open menus using only your keyboard. A hotkey...
Read more >
Hotkey Features Integration (Version 9) for Windows 11, 10 ...
This software enables F1-F12 special keys and buttons on your computer.
Read more >
Hotkeys (mouse, joystick and keyboard shortcuts) - AutoHotkey
Learn details about hotkeys in general, modifier symbols, context-sensitive hotkeys, custom combinations, mouse wheel hotkeys, function hotkeys, etc.
Read more >
Keyboard Shortcuts - Millersville University
To enable your Function keys, check the option not to display this message again. Standard Toolbar Buttons. Button. Keyboard. Shortcut Description. Button.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found