Skip to content

Commit 5eec284

Browse files
committed
Move key code handling logic to python
Move key code handling logic to python Add support for space key Rename keys for consistency with Qt backend Add a link to the key codes Add comments
1 parent 11a3010 commit 5eec284

File tree

2 files changed

+91
-81
lines changed

2 files changed

+91
-81
lines changed

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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':

lib/matplotlib/backends/web_backend/mpl.js

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -472,44 +472,15 @@ mpl.figure.prototype.key_event = function(event, name) {
472472
this._key = null;
473473

474474
var value = '';
475-
if (event.ctrlKey && !(event.which == 17))
475+
if (event.ctrlKey && event.which != 17)
476476
value += "ctrl+";
477-
if (event.altKey && !(event.which == 18))
477+
if (event.altKey && event.which != 18)
478478
value += "alt+";
479+
if (event.shiftKey && event.which != 16)
480+
value += "shift+";
479481

480-
if (event.which == 17) {
481-
value += "control";
482-
if (event.shiftKey)
483-
value += "+shift"
484-
}
485-
else if (event.which == 18) {
486-
value += "alt";
487-
if (event.shiftKey)
488-
value += "+shift"
489-
}
490-
else if (event.which == 16)
491-
value += "shift";
492-
else {
493-
if (event.which >= 65 && event.which <= 90) {
494-
if (event.shiftKey)
495-
value += String.fromCharCode(event.which);
496-
else
497-
value += String.fromCharCode(event.which).toLowerCase();
498-
}
499-
else if (event.key) {
500-
if (event.shiftKey && (event.which <= 46 ||
501-
(event.key >= 91 && event.key <= 145)))
502-
value += "shift+"
503-
value += event.key.toLowerCase();
504-
}
505-
else if (event.originalEvent.keyIdentifier) {
506-
if (event.shiftKey)
507-
value += "shift+"
508-
value += event.originalEvent.keyIdentifier.toLowerCase();
509-
}
510-
else
511-
value += String.fromCharCode(event.which);
512-
}
482+
value += 'k';
483+
value += event.which.toString();
513484

514485
this.send_message(name, {key: value});
515486
return false;

0 commit comments

Comments
 (0)