@@ -49,52 +49,93 @@ def new_figure_manager_given_figure(num, figure):
4949 return manager
5050
5151
52- _KEY_LUT = {'shift+À' : '~' ,
53- 'À' : '`' ,
54- '\x1b ' : 'esc' ,
55- '\x08 ' : 'backspace' ,
56- 'shift+Ü' : '|' ,
57- 'Ü' : '\\ ' ,
58- 'shift+Ý' : '}' ,
59- 'Ý' : ']' ,
60- 'shift+Û' : '{' ,
61- 'Û' : '[' ,
62- 'shift+º' : ':' ,
63- 'º' : ';' ,
64- 'shift+Þ' : '"' ,
65- 'Þ' : "'" ,
66- 'shift+¼' : '<' ,
67- '¼' : ',' ,
68- 'shift+¾' : '>' ,
69- '¾' : '.' ,
70- 'shift+¿' : '?' ,
71- '¿' : '/' ,
72- 'shift+»' : '+' ,
73- '»' : '=' ,
74- 'shift+½' : '_' ,
75- '½' : '-' ,
76- '\x7f ' : 'del' ,
77- '\t ' : 'tab' }
78-
79-
80- def _get_key (key ):
81- """Handle key codes with unicode characters"""
82- ind = key .index ('u+' )
83- char = chr (int (key [ind + 2 :], 16 ))
84- if re .match ('\d' , char ):
52+ # http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
53+ _SHIFT_LUT = {59 : ':' ,
54+ 61 : '+' ,
55+ 173 : '_' ,
56+ 186 : ':' ,
57+ 187 : '+' ,
58+ 188 : '<' ,
59+ 189 : '_' ,
60+ 190 : '>' ,
61+ 191 : '?' ,
62+ 192 : '~' ,
63+ 219 : '{' ,
64+ 220 : '|' ,
65+ 221 : '}' ,
66+ 222 : '"' }
67+
68+ _LUT = {8 : 'backspace' ,
69+ 9 : 'tab' ,
70+ 13 : 'enter' ,
71+ 16 : 'shift' ,
72+ 17 : 'control' ,
73+ 18 : 'alt' ,
74+ 19 : 'pause' ,
75+ 20 : 'caps' ,
76+ 27 : 'escape' ,
77+ 32 : ' ' ,
78+ 33 : 'pageup' ,
79+ 34 : 'pagedown' ,
80+ 35 : 'end' ,
81+ 36 : 'home' ,
82+ 37 : 'left' ,
83+ 38 : 'up' ,
84+ 39 : 'right' ,
85+ 40 : 'down' ,
86+ 45 : 'insert' ,
87+ 46 : 'delete' ,
88+ 91 : 'super' ,
89+ 92 : 'super' ,
90+ 93 : 'select' ,
91+ 106 : '*' ,
92+ 107 : '+' ,
93+ 109 : '-' ,
94+ 110 : '.' ,
95+ 111 : '/' ,
96+ 144 : 'num_lock' ,
97+ 145 : 'scroll_lock' ,
98+ 186 : ':' ,
99+ 187 : '=' ,
100+ 188 : ',' ,
101+ 189 : '-' ,
102+ 190 : '.' ,
103+ 191 : '/' ,
104+ 192 : '`' ,
105+ 219 : '[' ,
106+ 220 : '\\ ' ,
107+ 221 : ']' ,
108+ 222 : "'" }
109+
110+
111+ def _handle_key (key ):
112+ """Handle key codes"""
113+ code = int (key [key .index ('k' ) + 1 :])
114+ value = chr (code )
115+ # letter keys
116+ if code >= 65 and code <= 90 :
85117 if 'shift+' in key :
86- num = int (key [- 1 ])
87118 key = key .replace ('shift+' , '' )
88- char = ')!@#$%^&*(' [num ]
89- elif 'shift+' in key :
90- if 'shift+' + char in _KEY_LUT :
91- key = key .replace ('shift+' , '' )
92- char = _KEY_LUT ['shift+' + char ]
93119 else :
94- char = _KEY_LUT [char ]
95- else :
96- char = _KEY_LUT .get (char , char )
97- key = key [:key .index ('u+' )] + char
120+ value = value .lower ()
121+ # number keys
122+ elif code >= 48 and code <= 57 :
123+ if 'shift+' in key :
124+ value = ')!@#$%^&*(' [int (value )]
125+ key = key .replace ('shift+' , '' )
126+ # function keys
127+ elif code >= 112 and code <= 123 :
128+ value = 'f%s' % (code - 111 )
129+ # number pad keys
130+ elif code >= 96 and code <= 105 :
131+ value = '%s' % (code - 96 )
132+ # keys with shift alternatives
133+ elif code in _SHIFT_LUT and 'shift+' in key :
134+ key = key .replace ('shift+' , '' )
135+ value = _SHIFT_LUT [code ]
136+ elif code in _LUT :
137+ value = _LUT [code ]
138+ key = key [:key .index ('k' )] + value
98139 return key
99140
100141
@@ -272,9 +313,7 @@ def handle_event(self, event):
272313 elif e_type == 'scroll' :
273314 self .scroll_event (x , y , event ['step' ])
274315 elif e_type in ('key_press' , 'key_release' ):
275- key = event ['key' ]
276- if 'u+' in key :
277- key = _get_key (key )
316+ key = _handle_key (event ['key' ])
278317 if e_type == 'key_press' :
279318 self .key_press_event (key )
280319 elif e_type == 'key_release' :
0 commit comments