Last active
April 25, 2019 16:43
-
-
Save SimonWoodburyForget/fe0a90bbf897d43dec52ba28b781708b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
KEYBOARDCONTROL_NOT_FOUND_FOR_OS = \ | |
"No keyboard control module was found for os %s" % sys.platform | |
if sys.platform.startswith('linux'): | |
from plover.oslayer import xkeyboardcontrol as keyboardcontrol | |
elif sys.platform.startswith('win32'): | |
from plover.oslayer import winkeyboardcontrol as keyboardcontrol | |
elif sys.platform.startswith('darwin'): | |
from plover.oslayer import osxkeyboardcontrol as keyboardcontrol | |
else: | |
raise Exception(KEYBOARDCONTROL_NOT_FOUND_FOR_OS) | |
class KeyboardCapture(keyboardcontrol.KeyboardCapture): | |
"""Listen to keyboard events.""" | |
# Supported keys. | |
SUPPORTED_KEYS_LAYOUT = ''' | |
Escape F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 | |
` 1 2 3 4 5 6 7 8 9 0 - = \\ BackSpace Insert Home Page_Up | |
Tab q w e r t y u i o p [ ] Delete End Page_Down | |
a s d f g h j k l ; ' Return | |
z x c v b n m , . / Up | |
space Left Down Right | |
''' | |
SUPPORTED_KEYS = tuple(SUPPORTED_KEYS_LAYOUT.split()) | |
class KeyboardEmulation(keyboardcontrol.KeyboardEmulation): | |
"""Emulate printable key presses and backspaces.""" | |
pass | |
if __name__ == '__main__': | |
import time | |
kc = KeyboardCapture() | |
ke = KeyboardEmulation() | |
pressed = set() | |
released = set() | |
def on_key(key, action): | |
global pressed | |
global released | |
if 'pressed' == action: | |
pressed.add(key) | |
elif key in pressed: | |
if len(pressed) < 4: | |
# with a Dvorak keyboard, I had to do something like this and | |
# remap KEY_TO_KEYCODE to the Dvorak layout, which gave me a | |
# QWERTY layout, which meant I needed to remap from QWERTY back | |
# into keycodes. | |
# | |
# ke._send_keycode(plover.oslayer.xkeyboardcontrol.KEY_TO_KEYCODE_QWERTY[key]) | |
# ke._display.sync() | |
ke.send_string(" kkkk " + key) | |
pressed.remove(key) | |
else: | |
released.add(key) | |
if released == pressed: | |
ke.send_string(" oooo " + "".join(pressed)) | |
pressed = set() | |
released = set() | |
kc.key_down = lambda k: on_key(k, 'pressed') | |
kc.key_up = lambda k: on_key(k, 'released') | |
kc.suppress_keyboard(KeyboardCapture.SUPPORTED_KEYS) | |
kc.start() | |
print('Press CTRL-c to quit.') | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
kc.cancel() | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment