Implement caps-lock modifying extra keys

This commit is contained in:
Travis Burtrum 2014-12-30 15:32:45 -05:00
parent 45ad416611
commit cad4d650a1
3 changed files with 38 additions and 3 deletions

View File

@ -93,10 +93,12 @@ else:
# parse keymap # parse keymap
if args.keymap: if args.keymap:
from uinputmapper.keymapper import parse_keymap from uinputmapper.keymapper import parse_keymap
runtime_keymaps, active_keymap_index, revert_keymap_index, active_keymap, revert_default_code, switch_layout_codes, switch_layout_mode, num_codes_to_index = parse_keymap(args, rev_event_keys, event_keys) runtime_keymaps, active_keymap_index, revert_keymap_index, active_keymap, revert_default_code, switch_layout_codes, switch_layout_mode, num_codes_to_index, caps_lock_no_effect = parse_keymap(args, rev_event_keys, event_keys)
shift_down = -1 shift_down = -1
caps_locked = False # todo: get/set this a sane way?
rshift = event_keys[1]['KEY_RIGHTSHIFT'] rshift = event_keys[1]['KEY_RIGHTSHIFT']
lshift = event_keys[1]['KEY_LEFTSHIFT'] lshift = event_keys[1]['KEY_LEFTSHIFT']
capslck = event_keys[1]['KEY_CAPSLOCK']
if args.direct: if args.direct:
# setup a new input device # setup a new input device
@ -146,13 +148,20 @@ while True:
#if ev.type == 4: # MSC_SCAN, works for some keyboards (serio) but not others (usb), not needed if we grab the input #if ev.type == 4: # MSC_SCAN, works for some keyboards (serio) but not others (usb), not needed if we grab the input
# ev.value = active_keymap[ev.value] # ev.value = active_keymap[ev.value]
if ev.type == 1: # EV_KEY if ev.type == 1: # EV_KEY
new_code, invert_shift = active_keymap[ev.code].get_code(ev.code, shift_down != -1) new_code, invert_shift = active_keymap[ev.code].get_code(ev.code, caps_locked != (shift_down != -1))
# we want some events to happen when a key is first pressed or lifted # we want some events to happen when a key is first pressed or lifted
if ev.value == 1: # pressed if ev.value == 1: # pressed
if ev.code == lshift or ev.code == rshift: # shift if ev.code == lshift or ev.code == rshift: # shift
shift_down = ev.code shift_down = ev.code
elif ev.code == capslck: # caps locked
caps_locked = not caps_locked
if caps_locked and caps_lock_no_effect[new_code]:
invert_shift = not invert_shift
if invert_shift: # swap shift behavior if invert_shift: # swap shift behavior
# if (invert_shift and not(caps_locked and caps_lock_no_effect[new_code])) or (not(invert_shift) and (caps_locked and caps_lock_no_effect[new_code])): # not as clean as above?
pre_event = input_event(ev.time, 1, shift_down, 0) pre_event = input_event(ev.time, 1, shift_down, 0)
if shift_down == -1: if shift_down == -1:
pre_event.code = lshift pre_event.code = lshift
@ -175,6 +184,8 @@ while True:
if ev.code == lshift or ev.code == rshift: # shift if ev.code == lshift or ev.code == rshift: # shift
shift_down = -1 shift_down = -1
if caps_locked and caps_lock_no_effect[new_code]:
invert_shift = not invert_shift
if invert_shift: # swap shift behavior if invert_shift: # swap shift behavior
post_event = input_event(ev.time, 1, shift_down, 1) post_event = input_event(ev.time, 1, shift_down, 1)
if shift_down == -1: if shift_down == -1:

View File

@ -12,6 +12,15 @@ revert_keymap_index = 0
# in this case, 2 means modified Progammer Dvorak # in this case, 2 means modified Progammer Dvorak
default_keymap_index = 2 default_keymap_index = 2
# these are keys that caps_lock doesn't modify by default, but that you would like it to, affects all keymaps
caps_lock_modify = """
GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSPC, PSLS,PAST,PMNS,
LBRC,RBRC,BSLS, P7, P8, P9,
SCLN,QUOT, P4, P5, P6, PPLS,
COMM,DOT, SLSH, P1, P2, P3,
P0, PDOT
"""
# these are the keymaps available, you can add as many as you want or re-order them, just be aware the mapping is # these are the keymaps available, you can add as many as you want or re-order them, just be aware the mapping is
# always done from the first one to all subsequent ones, so you probably want to leave QWERTY or similar up top # always done from the first one to all subsequent ones, so you probably want to leave QWERTY or similar up top
keymaps = [ keymaps = [

View File

@ -40,6 +40,7 @@ def parse_keymap(args, rev_event_keys, event_keys):
revert_default_key = keymap_config['revert_default_key'] revert_default_key = keymap_config['revert_default_key']
revert_keymap_index = keymap_config['revert_keymap_index'] revert_keymap_index = keymap_config['revert_keymap_index']
default_keymap_index = keymap_config['default_keymap_index'] default_keymap_index = keymap_config['default_keymap_index']
caps_lock_modify = keymap_config['caps_lock_modify']
keymaps = keymap_config['keymaps'] keymaps = keymap_config['keymaps']
#print keymap_config #print keymap_config
#exit(0) #exit(0)
@ -169,4 +170,18 @@ def parse_keymap(args, rev_event_keys, event_keys):
#pprint.pprint(num_codes_to_index) #pprint.pprint(num_codes_to_index)
#exit(0) #exit(0)
return runtime_keymaps, active_keymap_index, revert_keymap_index, active_keymap, revert_default_code, switch_layout_codes, switch_layout_mode, num_codes_to_index # again quick array index based lookup
caps_lock_no_effect = []
for x in keycnt_range:
caps_lock_no_effect.append(False)
for key in caps_lock_modify.split(','):
key = key.strip() # remove whitespace
new_key = 'KEY_'+short_to_long.get(key, key)
if args.dump and new_key not in event_keys[1]: # todo: probably should exit with some helpful error here?
print "'%s':''," % key
continue
caps_lock_no_effect[event_keys[1][new_key]] = True
#pprint.pprint(caps_lock_no_effect)
#exit(0)
return runtime_keymaps, active_keymap_index, revert_keymap_index, active_keymap, revert_default_code, switch_layout_codes, switch_layout_mode, num_codes_to_index, caps_lock_no_effect