Autokey (not AutoHotkey)
Created: 2018-11-07 09:59:51 -0800 Modified: 2018-11-14 09:08:12 -0800
This is a program like AutoHotkey on Windows.
At least on Ubuntu with KDE, ctrl+shift+U lets you type in the hex code for a Unicode character, e.g. 1f433 for a 🐳.
You can set a hotkey to an entire folder of scripts in the program. When you press that hotkey, it will give you a context menu where you can pick a specific hotkey. This menu is keyboard accessible.
Scripting (API documentation, script examples)
Section titled Scripting (API documentation, script examples)- If you want to see the output of something quickly, you can use:
dialog.info_dialog(‘Test’, ‘Hello world %s’ % some_string_value)
- For key names like <left> or <alt>
- Autokey’s Python scripts get installed here: /home/adam/.local/lib/python3.6/site-packages/autokey
- If you need a delay, do “from time import sleep” and then “sleep(1)“.
- Using the interface to make many scripts can be somewhat tedious. Each script gets saved as two files to ~/.config/autokey/data in some subfolder: a hidden JSON file with metadata on how to trigger the shortcut, and a script or text file for the action.
- If you need breakpoints to drop into a Python shell in the middle of a script, use “breakpoint()” if you’re on Python 3.7+ or “import pdb; pdb.set_trace()” if you’re on a lower version.
Reusing Python scripts
Section titled Reusing Python scriptsIf you want to reuse Python scripts so that you don’t have a lot of duplication (reference), you can make a folder like ~/.config/autokey/python_scripts and then tell the application to use it via Edit → Preferences → Script Engine → choose a folder.
The problem is that your shared script won’t have access to “keyboard”, “window”, etc. without doing some relatively extreme workarounds. I.e. you’d have to make iomediator, a configmanager, etc. like what’s shown here. But then, you still have to make Keyboard, Window, etc. like you see here. It is not simple to set this up.
To work around that, I got a lot of help from Twitch chat and ended up with this:
My whale script:
insert_unicode.py:
By using “locals()”, we can access keyboard, window, etc. from the insert_unicode.py script.
Specific scripts
Section titled Specific scriptsPut the active window in a particular location on the screen
Section titled Put the active window in a particular location on the screenThere’s only a problem when you’ve quick-tiled a window on KDE; there’s no way to undo that state, so the window is completely unaffected. I didn’t try to figure this out any further because of two reasons:
- Apparently KDE can somehow manage this via Window Rules (right-click title bar, more actions, window manager settings) or kwinscripts. I never figured out either of these. https://userbase.kde.org/KWin_Rules_Examples
- The workaround is just to press super+up before running the Autokey script. Also, this only affects certain windows like Chrome to begin with.
Troubleshooting
Section titled TroubleshootingTyping unicode characters like arrows or emojis (reference)
Section titled Typing unicode characters like arrows or emojis (reference)First of all, you can just install like this:
sudo add-apt-repository ppa:sporkwitch/autokey
sudo apt update
sudo apt install autokey-gtk
Or alternatively, to install the Qt5 based GUI:
Section titled Or alternatively, to install the Qt5 based GUI:sudo apt remove autokey-qt
No need to ever install dbus or something and try to install via PIP (which is a huge pain). Note that if you needed to go that route for whatever reason, you could try these commands:
install python-dbus-dev
sudo apt-get install libdbus-1-dev libdbus-glib-1-dev
Add a script that does something like this:
Can’t send global keys to a particular program
Section titled Can’t send global keys to a particular programIn KDE, I had super+up configured to maximize a window. I apparently can’t send that to an application via keyboard.send_keys().
There’s a workaround involving “uinput” that you can use via a module in Python (reference)
I also had some luck with this:
Getting the output of a script
Section titled Getting the output of a scriptJust launch via autokey-gtk —verbose
Keep in mind that this will probably print every single character/keystroke that you type, so you should be careful about showing passwords.
Can’t quit the application from the notification tray
Section titled Can’t quit the application from the notification tray[10:25] macobo123: pkill -f autokey?