8000 Improve webagg support for non-alphanumeric key events on non-qwerty … · matplotlib/matplotlib@9a75c33 · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 9a75c33

Browse files
Alejandro Garcíatacaswell
authored andcommitted
Improve webagg support for non-alphanumeric key events on non-qwerty keyboards
1 parent 01d3149 commit 9a75c33

File tree

2 files changed

+24
-95
lines changed

2 files changed

+24
-95
lines changed

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 18 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -27,94 +27,27 @@
2727

2828
_log = logging.getLogger(__name__)
2929

30-
# http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
31-
_SHIFT_LUT = {59: ':',
32-
61: '+',
33-
173: '_',
34-
186: ':',
35-
187: '+',
36-
188: '<',
37-
189: '_',
38-
190: '>',
39-
191: '?',
40-
192: '~',
41-
219: '{',
42-
220: '|',
43-
221: '}',
44-
222: '"'}
45-
46-
_LUT = {8: 'backspace',
47-
9: 'tab',
48-
13: 'enter',
49-
16: 'shift',
50-
17: 'control',
51-
18: 'alt',
52-
19: 'pause',
53-
20: 'caps',
54-
27: 'escape',
55-
32: ' ',
56-
33: 'pageup',
57-
34: 'pagedown',
58-
35: 'end',
59-
36: 'home',
60-
37: 'left',
61-
38: 'up',
62-
39: 'right',
63-
40: 'down',
64-
45: 'insert',
65-
46: 'delete',
66-
91: 'super',
67-
92: 'super',
68-
93: 'select',
69-
106: '*',
70-
107: '+',
71-
109: '-',
72-
110: '.',
73-
111: '/',
74-
144: 'num_lock',
75-
145: 'scroll_lock',
76-
186: ':',
77-
187: '=',
78-
188: ',',
79-
189: '-',
80-
190: '.',
81-
191: '/',
82-
192: '`',
83-
219: '[',
84-
220: '\\',
85-
221: ']',
86-
222: "'"}
30+
_LUT = {'AltGraph': 'alt',
31+
'CapsLock': 'caps',
32+
'ArrowLeft': 'left',
33+
'ArrowUp': 'up',
34+
'ArrowRight': 'right',
35+
'ArrowDown': 'down',
36+
'NumLock': 'num_lock',
37+
'ScrollLock': 'scroll_lock'}
8738

8839

8940
def _handle_key(key):
90-
"""Handle key codes"""
91-
code = int(key[key.index('k') + 1:])
92-
value = chr(code)
93-
# letter keys
94-
if 65 <= code <= 90:
95-
if 'shift+' in key:
96-
key = key.replace('shift+', '')
97-
else:
98-
value = value.lower()
99-
# number keys
100-
elif 48 <= code <= 57:
101-
if 'shift+' in key:
102-
value = ')!@#$%^&*('[int(value)]
103-
key = key.replace('shift+', '')
104-
# function keys
105-
elif 112 <= code <= 123:
106-
value = 'f%s' % (code - 111)
107-
# number pad keys
108-
elif 96 <= code <= 105:
109-
value = '%s' % (code - 96)
110-
# keys with shift alternatives
111-
elif code in _SHIFT_LUT and 'shift+' in key:
112-
key = key.replace('shift+', '')
113-
value = _SHIFT_LUT[code]
114-
elif code in _LUT:
115-
value = _LUT[code]
116-
key = key[:key.index('k')] + value
117-
return key
41+
"""Handle key values"""
42+
value = key
43+
# Only set to lower if key value is an uppercase letter or
44+
# a combination of a modifier and an uppercase letter
45+
# (e.g. "ctrl+C", "A", and "ctrl+alt+T" must remain unaltered).
46+
if not value[-1:].isupper():
47+
value = value.lower()
48+
if key in _LUT:
49+
value = _LUT[key]
50+
return value
11851

11952

12053
class TimerTornado(backend_bases.TimerBase):

lib/matplotlib/backends/web_backend/js/mpl.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,35 +636,31 @@ mpl.figure.prototype.mouse_event = function (event, name) {
636636
};
637637

638638
mpl.figure.prototype._key_event_extra = function (_event, _name) {
639-
// Handle any extra behaviour associated with a key event
639+
// Ha ndle any extra behaviour associated with a key event
640640
};
641641

642642
mpl.figure.prototype.key_event = function (event, name) {
643643
// Prevent repeat events
644644
if (name === 'key_press') {
645-
if (event.which === this._key) {
645+
if (event.key === this._key) {
646646
return;
647647
} else {
648-
this._key = event.which;
648+
this._key = event.key;
649649
}
650650
}
651651
if (name === 'key_release') {
652652
this._key = null;
653653
}
654654

655655
var value = '';
656-
if (event.ctrlKey && event.which !== 17) {
656+
if (event.ctrlKey && event.key !== 'Control') {
657657
value += 'ctrl+';
658658
}
659-
if (event.altKey && event.which !== 18) {
659+
if (event.altKey && event.key !== 'Alt') {
660660
value += 'alt+';
661661
}
662-
if (event.shiftKey && event.which !== 16) {
663-
value += 'shift+';
664-
}
665662

666-
value += 'k';
667-
value += event.which.toString();
663+
value += event.key;
668664

669665
this._key_event_extra(event, name);
670666

0 commit comments

Comments
 (0)
0