Skip to content

Instantly share code, notes, and snippets.

@mickael9
Last active March 29, 2017 15:24
Show Gist options
  • Save mickael9/567ce26b028ce89cafcaf4250464c526 to your computer and use it in GitHub Desktop.
Save mickael9/567ce26b028ce89cafcaf4250464c526 to your computer and use it in GitHub Desktop.
import sys
import re
# key code -> USB HID scancode
usb_map = [
0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 87, 88, 99, 70, 119, 110, 102, 104, 111, 107, 109, 106,
105, 108, 103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
72, 73, 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190,
191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135, 136, 113,
115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94, 95, 0, 0, 0,
122, 123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115, 114, 113,
150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140
]
if sys.stdin.isatty():
print('Usage: loadkeys -m <keymap> | %s' % sys.argv[0], file=sys.stderr)
source = sys.stdin.read()
maps = {name: [int(val, 16) for val in vals.split(',') if val.strip()] for name, vals in re.findall(
r'unsigned short ((?:plain|shift)_map)\[NR_KEYS] = \{([^}]+)};',
source,
re.MULTILINE
)}
KT_LATIN = 0
KT_LETTER = 11
KT_SPEC = 2
K_ENTER = (KT_SPEC << 8) | 1
chr_to_usb = {}
for map_name, map_vals in maps.items():
for n, key in enumerate(map_vals):
try:
scan = usb_map.index(n)
except ValueError:
continue
key &= 0xfff
typ = key >> 8
if key == K_ENTER:
char = '\n'
elif typ in (KT_LATIN, KT_LETTER):
char = chr(key & 0xff)
if char == '\x1b':
continue
else:
continue
if map_name == 'shift_map':
scan |= 0x80
if char not in chr_to_usb:
chr_to_usb[char] = scan
for k in sorted(chr_to_usb):
shift = ''
if chr_to_usb[k] & 0x80:
shift = ' | SHIFT'
print('keyMap[%#.2x] = %#.2x%s; /* %r */' % (
ord(k), chr_to_usb[k] & ~0x80, shift, k))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment