Skip to content

Instantly share code, notes, and snippets.

@hugke729
Created May 28, 2019 21:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugke729/184542a1c5144326afc12bcc78cd57cf to your computer and use it in GitHub Desktop.
Save hugke729/184542a1c5144326afc12bcc78cd57cf to your computer and use it in GitHub Desktop.
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.

@camilovietnam
Copy link

Brilliant! Worked perfectly, the only thing I had to do was install xclip. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment