Skip to content

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.

  • 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.

If 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:

from insert_unicode import paste_character
paste_character(locals(), '🐳')

insert_unicode.py:

import sys
from importlib import reload
import json
reload(sys)
from subprocess import Popen, PIPE
def paste_character(autokeyLocals, symbol):
c = Popen(['xclip', '-selection', 'clipboard'], stdin=PIPE)
c.communicate(symbol.encode('utf-8'))
autokeyLocals['keyboard'].send_keys('<ctrl>+v')

By using “locals()”, we can access keyboard, window, etc. from the insert_unicode.py script.

Put the active window in a particular location on the screen

Section titled Put the active window in a particular location on the screen
t = window.get_active_title();
window.set_property(t, 'remove', 'maximized_vert', matchClass=False)
window.set_property(t, 'remove', 'maximized_horz', matchClass=False)
window.set_property(t, 'remove', 'fullscreen', matchClass=False)
window.resize_move(t, 1680, 0, 1370, 1050, matchClass=False)

There’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:

  1. 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
  2. The workaround is just to press super+up before running the Autokey script. Also, this only affects certain windows like Chrome to begin with.

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:

import sys
from importlib import reload
reload(sys)
from subprocess import Popen, PIPE
def paste_character(symbol):
c = Popen(['xclip', '-selection', 'clipboard'], stdin=PIPE)
c.communicate(symbol.encode('utf-8'))
keyboard.send_keys('<ctrl>+v')
paste_character('🐳')

Can’t send global keys to a particular program

Section titled Can’t send global keys to a particular program

In 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:

keyboard.press_key("<super>")
keyboard.press_key("<up>")
keyboard.release_key("<up>")
keyboard.release_key("<super>")

Just 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?