Source

ttgui / generate_keymap.py

Full commit
# -*- coding: utf-8 -*-

def escape_modifier( digit ):
    mode = ord(digit) - ord("1")
    return "shift "*(mode&1) + "meta "*((mode&2)/2) + "ctrl "*((mode&4)/4)


keys = [('[A','up'),('[B','down'),('[C','right'),('[D','left'),
    ('[E','5'),('[F','end'),('[G','5'),('[H','home'),
    ('[1~','home'),('[2~','insert'),('[3~','delete'),('[4~','end'),
    ('[5~','page up'),('[6~','page down'),
    ('[7~','home'),('[8~','end'),
    
    ('[[A','f1'),('[[B','f2'),('[[C','f3'),('[[D','f4'),('[[E','f5'),
    
    ('[11~','f1'),('[12~','f2'),('[13~','f3'),('[14~','f4'),
    ('[15~','f5'),('[17~','f6'),('[18~','f7'),('[19~','f8'),
    ('[20~','f9'),('[21~','f10'),('[23~','f11'),('[24~','f12'),
    ('[25~','f13'),('[26~','f14'),('[28~','f15'),('[29~','f16'),
    ('[31~','f17'),('[32~','f18'),('[33~','f19'),('[34~','f20'),
    
    ('OA','up'),('OB','down'),('OC','right'),('OD','left'),
    ('OH','home'),('OF','end'),
    ('OP','f1'),('OQ','f2'),('OR','f3'),('OS','f4'),
    ('Oo','/'),('Oj','*'),('Om','-'),('Ok','+'),
    
    ('[Z','shift tab')
    ] + [
    # modified cursor keys + home, end, 5 -- [#X and [1;#X forms
    (prefix+digit+letter, escape_modifier(digit) + key)
       for prefix in "[","[1;"
         for digit in "12345678"
           for letter,key in zip("ABCDEFGH",
              ('up','down','right','left','5','end','5','home'))
    ] + [ 
    # modified F1-F4 keys -- O#X form
    ("O"+digit+letter, escape_modifier(digit) + key)
      for digit in "12345678"
        for letter,key in zip("PQRS",('f1','f2','f3','f4'))
    ] + [ 
    # modified F1-F13 keys -- [XX;#~ form
    ("["+str(num)+";"+digit+"~", escape_modifier(digit) + key)
    for digit in "12345678"
    for num,key in zip(
    (11,12,13,14,15,17,18,19,20,21,23,24,25,26,28,29,31,32,33,34),
    ('f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','f11',
    'f12','f13','f14','f15','f16','f17','f18','f19','f20'))
    ]

	
if __name__ == "__main__":
    f = open("keymap.py", "w")
    print >>f, "# -*- coding: utf-8 -*-"
    print >>f, "keymap = {"
    for chars, name in keys:
        print >>f, "    %r: %r," % (chars, name)
    print >>f, "}"
    f.close()