Saturday, January 1, 2011

heart.py: String macros in Linux with Python

Here's something I made after discovering the Python module virtkey: a way to bind macros that type out strings for you to keyboard shortcuts in Ubuntu (and most likely other Linux platforms):


import virtkey
import time
import sys
time.sleep(0.5)
v = virtkey.virtkey()
if len(sys.argv) > 1:
    str = sys.argv[1]
    strords = [ord(c) for c in list(str)]
else:
    strords = [9829] #heart!
for i in strords:
    v.press_unicode(i)
    v.release_unicode(i)


The fourth line (time.sleep(0.5)) is a necessary hack: When you bind the keyboard shortcut, you will likely bind it to Windows + a letter key or Ctrl + Alt + a letter key. The delay gives you time to release those held mod keys before virtkey starts typing the argument, or if there is no argument, a heart (♥). It would be run as:

python ~/heart.py "hello, world!"

And one could keybind it with the Keyboard Shortcuts dialog in Ubuntu, or a similar tool in other Linux distros. The original script can be downloaded here. It's also in my code scraps on Google Code

The idea is, for developers or other people who have strings that they use a lot and would like to keep on a permanent clipboard, to bind oft-used strings to Windows + 1, Windows + 2, ... , Windows + 0, for quicker access. This has probably been done with multiple-clipboard applications, but this was still fun to make.

No comments:

Post a Comment

Followers