Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An alternative way to input unicode characters with Autokey

An alternative way to input unicode characters with Autokey

The post Add any symbol without leaving the keyboard explains how to input unicode characters easily on Linux. However, the instructions given may not always work or you may be stuck with Autokey version 2, which doesn't play nice with unicode. What follows is a brief description of a work-around.

First, create a file called MyCopy.py containing the following

import sys
reload(sys)
sys.setdefaultencoding('utf8')

from subprocess import Popen, PIPE

def paste_char(symbol):
    c = Popen(['xclip', '-selection', 'clipboard'], stdin=PIPE)
    c.communicate(symbol.encode('utf-8'))
    p = Popen(['autokey-run', '-s', 'Paste'])

This script (1) ensures the correct encoding is being used so that unicode works correctly, (2) copies a given symbol to the clipboard, and (3) pastes it. The script uses xclip, which is probably installed by default, but may need to be installed.

In Autokey, go to Edit > Preferences > Script Engine. Ensure the User Module Folder points to the directory containing MyCopy.py

Then, create a script called 'Paste'. As implied above, this is called by name by MyCopy.py. The Paste script simply executes a paste command, which is typically Ctrl+V, but for some applications, it may differ. For me, Paste looks like

window_title = window.get_active_title()
if 'Sublime Text (UNREGISTERED)' in window_title:
    keyboard.send_keys('<super>+v')
elif 'IPython' == window.get_active_title():
    keyboard.send_keys('<ctrl>+<shift>+v')
elif 'Terminal' == window.get_active_title():
    keyboard.send_keys('<ctrl>+<shift>+v')
elif 'Matlab console' == window.get_active_title():
    keyboard.send_keys('<ctrl>+<shift>+v')
else:
    keyboard.send_keys('<ctrl>+v')

It should be self-explanatory how to adjust this to your case.

We're now ready to set up a script to paste a unicode character. Let's use Δ as an example.

Click File > New > Script. Name it Delta. Then paste the following two lines

from MyCopy import paste_char
paste_char('Δ')

Finally, set up an abbreviation that calls the Delta script. I personally prefer '/Delta', but you can use whatever you like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.