Skip to content

Commit

Permalink
Merge pull request #538 from morinted/mac_keycode
Browse files Browse the repository at this point in the history
Fix Mac OS X keyboard layout
  • Loading branch information
benoit-pierre authored Jun 13, 2016
2 parents 2282f03 + 62807a9 commit c240d71
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 325 deletions.
20 changes: 20 additions & 0 deletions plover/misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2016 Open Steno Project
# See LICENSE.txt for details.


def popcount_8(v):
"""Population count for an 8 bit integer"""
assert 0 <= v <= 255
v -= ((v >> 1) & 0x55555555)
v = (v & 0x33333333) + ((v >> 2) & 0x33333333)
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24


def characters(string):
"""Python 2.7 has narrow Unicode on Mac OS X and Windows"""

encoded = string.encode('utf-32-be') # 4 bytes per character
for char_start in xrange(0, len(encoded), 4):
end = char_start + 4
character = encoded[char_start:end].decode('utf-32-be')
yield character
277 changes: 0 additions & 277 deletions plover/oslayer/mac_keycode.py

This file was deleted.

44 changes: 23 additions & 21 deletions plover/oslayer/osxkeyboardcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
import threading
import Queue
from time import sleep
import collections

from plover.oslayer import mac_keycode
from plover.misc import characters
from plover.oslayer.osxkeyboardlayout import KeyboardLayout
from plover.key_combo import add_modifiers_aliases, parse_key_combo, KEYNAME_TO_CHAR
import plover.log

Expand Down Expand Up @@ -77,7 +76,7 @@
'caps_lock': 57,

'return': 36, 'tab': 48, 'backspace': BACK_SPACE, 'delete': 117,
'escape': 53,
'escape': 53, 'clear': 71,

'up': 126, 'down': 125, 'left': 123, 'right': 124, 'page_up': 116,
'page_down': 121, 'home': 115, 'end': 119,
Expand All @@ -96,6 +95,13 @@
KEYNAME_TO_KEYCODE[name.lower()] = code + NX_KEY_OFFSET
add_modifiers_aliases(KEYNAME_TO_KEYCODE)

DEADKEY_SYMBOLS = {
'dead_acute': '´',
'dead_circumflex': '^',
'dead_diaeresis': '¨',
'dead_grave': '`',
'dead_tilde': '~',
}

def down(seq):
return [(x, True) for x in seq]
Expand Down Expand Up @@ -287,24 +293,12 @@ def _event_handler(self):
handler(key)


# "Narrow python" unicode objects store characters in UTF-16 so we
# can't iterate over characters in the standard way. This workaround
# let's us iterate over full characters in the string.
def characters(s):
encoded = s.encode('utf-32-be')
for i in xrange(len(encoded) / 4):
start = i * 4
end = start + 4
character = encoded[start:end].decode('utf-32-be')
yield character


class KeyboardEmulation(object):

RAW_PRESS, STRING_PRESS = range(2)

def __init__(self):
pass
self._layout = KeyboardLayout()

@staticmethod
def send_backspaces(number_of_backspaces):
Expand Down Expand Up @@ -351,7 +345,7 @@ def apply_raw():

last_modifier = None
for c in characters(s):
for keycode, modifier in mac_keycode.KeyCodeForChar(c):
for keycode, modifier in self._layout.char_to_key_sequence(c):
if keycode is not None:
if modifier is not last_modifier:
# Flush on modifier change.
Expand Down Expand Up @@ -404,11 +398,19 @@ def send_key_combination(self, combo_string):
"""
def name_to_code(name):
# Static key codes
code = KEYNAME_TO_KEYCODE.get(name)
if code is not None:
return code
char = KEYNAME_TO_CHAR.get(name, name)
code, mods = mac_keycode.KeyCodeForChar(char)[0]
pass
# Dead keys
elif name.startswith('dead_'):
code, mod = self._layout.deadkey_symbol_to_key_sequence(
DEADKEY_SYMBOLS.get(name)
)[0]
# Normal keys
else:
char = KEYNAME_TO_CHAR.get(name, name)
code, mods = self._layout.char_to_key_sequence(char)[0]
return code
# Parse and validate combo.
key_events = parse_key_combo(combo_string, name_to_code)
Expand Down
Loading

0 comments on commit c240d71

Please sign in to comment.