Skip to content

Commit

Permalink
Re-order Windows key entries to fix #147
Browse files Browse the repository at this point in the history
There were two problems: first, Windows key preference was taking scan
codes as more important than vk's, when practice shows it should be the
other way around. And second, "key_to_scan_codes" was using `set(left +
right)` to return values for ambiguously sided keys, scrambling the
order.

Three test cases were important when debugging this issue:

keyboard.send('play/pause media') # requires high-order byte
keyboard.send('windows')
keyboard.send('left windows')
  • Loading branch information
boppreh committed Mar 30, 2018
1 parent eb6fdc8 commit 2544c0f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ def key_to_scan_codes(key, error_if_missing=True):
elif _is_list(key):
return sum((key_to_scan_codes(i) for i in key), ())
elif not _is_str(key):
raise ValueError('Unexpected key type: ' + repr(key))
raise ValueError('Unexpected key type ' + str(type(key)) + ', value (' + repr(key) + ')')

normalized = _normalize_name(key)
if normalized in sided_modifiers:
left_scan_codes = key_to_scan_codes('left ' + normalized, False)
right_scan_codes = key_to_scan_codes('right ' + normalized, False)
return tuple(set(left_scan_codes + right_scan_codes))
return left_scan_codes + tuple(c for c in right_scan_codes if c not in left_scan_codes)

try:
# Put items in ordered dict to remove duplicates.
Expand Down
6 changes: 5 additions & 1 deletion keyboard/_winkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,12 @@ def _setup_name_tables():

modifiers_preference = defaultdict(lambda: 10)
modifiers_preference.update({(): 0, ('shift',): 1, ('alt gr',): 2, ('ctrl',): 3, ('alt',): 4})
def order_key(line):
i, entry = line
scan_code, vk, extended, modifiers = entry
return modifiers_preference[modifiers], i, extended, vk, scan_code
for name, entries in list(from_name.items()):
from_name[name] = sorted(set(entries), key=lambda e: (modifiers_preference[e[1][-1]], e))
from_name[name] = sorted(set(entries), key=order_key)

# Called by keyboard/__init__.py
init = _setup_name_tables
Expand Down

0 comments on commit 2544c0f

Please sign in to comment.