Browse Source

Improve keyboard shortcut handling

Use KeyboardEvent.key if available,
match both lower and upper case letters to keep previous behaviour

KeyboardEvent is a mess.

KeyboardEvent.which gives you, in theory, a decimal representation of the key pressed.
"r" or "R" gives you 82, which is "R", you can look at KeyboardEvent.shiftKey,
but you don't have access to capslock...

Maybe you want to use numbers, but of course NumPad will not give you the same than
"normal" numbers ...

Now if you use something else than letter, for exemple "?",
on a QWERTY keyboard "/" and "?" gives you 191,
on a AZERTY keyboard "," and "?" gives you 188, so we have to stick to letters.

This was for keydown and keyup events, keypressed event return the real char
(lower "a", "/", "?", ...) but it fails in some cases

The only non broken property is KeyboardEvent.key,
but it's only supported since Chrome 51, Opera 38, and not supported by Safari
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key

A good reference & test:
http://unixpapa.com/js/testkey.html
http://unixpapa.com/js/key.html

Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
j8
Etienne CHAMPETIER 9 years ago
parent
commit
9a4cee1818
1 changed files with 52 additions and 28 deletions
  1. 52
    28
      modules/keyboardshortcut/keyboardshortcut.js

+ 52
- 28
modules/keyboardshortcut/keyboardshortcut.js View File

@@ -3,67 +3,64 @@
3 3
 var shortcuts = {};
4 4
 function initShortcutHandlers() {
5 5
     shortcuts = {
6
-        27: {
6
+        "ESCAPE": {
7 7
             character: "Esc",
8 8
             function: function() {
9 9
                 APP.UI.showKeyboardShortcutsPanel(false);
10 10
             }
11 11
         },
12
-        67: {
12
+        "C": {
13 13
             character: "C",
14 14
             id: "toggleChatPopover",
15 15
             function: function() {
16 16
                 APP.UI.toggleChat();
17 17
             }
18 18
         },
19
-        68: {
19
+        "D": {
20 20
             character: "D",
21 21
             id: "toggleDesktopSharingPopover",
22 22
             function: function () {
23 23
                 APP.conference.toggleScreenSharing();
24 24
             }
25 25
         },
26
-        70: {
26
+        "F": {
27 27
             character: "F",
28 28
             id: "filmstripPopover",
29 29
             function: function() {
30 30
                 APP.UI.toggleFilmStrip();
31 31
             }
32 32
         },
33
-        77: {
33
+        "M": {
34 34
             character: "M",
35 35
             id: "mutePopover",
36 36
             function: function() {
37 37
                 APP.conference.toggleAudioMuted();
38 38
             }
39 39
         },
40
-        82: {
40
+        "R": {
41 41
             character: "R",
42 42
             function: function() {
43 43
                 APP.conference.maybeToggleRaisedHand();
44 44
             }
45 45
 
46 46
         },
47
-        84: {
47
+        "T": {
48 48
             character: "T",
49 49
             function: function() {
50 50
                 APP.conference.muteAudio(true);
51 51
             }
52 52
         },
53
-        86: {
53
+        "V": {
54 54
             character: "V",
55 55
             id: "toggleVideoPopover",
56 56
             function: function() {
57 57
                 APP.conference.toggleVideoMuted();
58 58
             }
59 59
         },
60
-        191: {
61
-            character: "/",
60
+        "?": {
61
+            character: "?",
62 62
             function: function(e) {
63
-                // Only trigger on "?", not on "/".
64
-                if (e.shiftKey) {
65
-                    APP.UI.toggleKeyboardShortcutsPanel();
66
-                }
63
+                APP.UI.toggleKeyboardShortcutsPanel();
67 64
             }
68 65
         }
69 66
     };
@@ -72,20 +69,21 @@ function initShortcutHandlers() {
72 69
 var KeyboardShortcut = {
73 70
     init: function () {
74 71
         initShortcutHandlers();
72
+        var self = this;
75 73
         window.onkeyup = function(e) {
76
-            var keycode = e.which;
74
+            var key = self.getKeyboardKey(e).toUpperCase();
75
+            var num = parseInt(key, 10);
77 76
             if(!($(":focus").is("input[type=text]") ||
78 77
                 $(":focus").is("input[type=password]") ||
79 78
                 $(":focus").is("textarea"))) {
80
-                if (typeof shortcuts[keycode] === "object") {
81
-                    shortcuts[keycode].function(e);
79
+                if (shortcuts.hasOwnProperty(key)) {
80
+                    shortcuts[key].function(e);
82 81
                 }
83
-                else if (keycode >= "0".charCodeAt(0) &&
84
-                    keycode <= "9".charCodeAt(0)) {
85
-                    APP.UI.clickOnVideo(keycode - "0".charCodeAt(0) + 1);
82
+                else if (!isNaN(num) && num >= 0 && num <= 9) {
83
+                    APP.UI.clickOnVideo(num + 1);
86 84
                 }
87
-                //esc while the smileys are visible hides them
88
-            } else if (keycode === 27 &&
85
+            //esc while the smileys are visible hides them
86
+            } else if (key === "ESCAPE" &&
89 87
                 $('#smileysContainer').is(':visible')) {
90 88
                 APP.UI.toggleSmileys();
91 89
             }
@@ -95,13 +93,13 @@ var KeyboardShortcut = {
95 93
             if(!($(":focus").is("input[type=text]") ||
96 94
                 $(":focus").is("input[type=password]") ||
97 95
                 $(":focus").is("textarea"))) {
98
-                if(e.which === "T".charCodeAt(0)) {
96
+                var key = self.getKeyboardKey(e).toUpperCase();
97
+                if(key === "T") {
99 98
                     if(APP.conference.isLocalAudioMuted())
100 99
                         APP.conference.muteAudio(false);
101 100
                 }
102 101
             }
103 102
         };
104
-        var self = this;
105 103
         $('body').popover({ selector: '[data-toggle=popover]',
106 104
             trigger: 'click hover',
107 105
             content: function() {
@@ -116,14 +114,40 @@ var KeyboardShortcut = {
116 114
      * @returns {string} the keyboard shortcut used for the id given
117 115
      */
118 116
     getShortcut: function (id) {
119
-        for (var keycode in shortcuts) {
120
-            if (shortcuts.hasOwnProperty(keycode)) {
121
-                if (shortcuts[keycode].id === id) {
122
-                    return " (" + shortcuts[keycode].character + ")";
117
+        for (var key in shortcuts) {
118
+            if (shortcuts.hasOwnProperty(key)) {
119
+                if (shortcuts[key].id === id) {
120
+                    return " (" + shortcuts[key].character + ")";
123 121
                 }
124 122
             }
125 123
         }
126 124
         return "";
125
+    },
126
+    /**
127
+     * @param e a KeyboardEvent
128
+     * @returns {string} e.key or something close if not supported
129
+     */
130
+    getKeyboardKey: function (e) {
131
+        if (typeof e.key === "string") {
132
+            return e.key;
133
+        }
134
+        if (e.type === "keypress" && (
135
+                (e.which >= 32 && e.which <= 126) ||
136
+                (e.which >= 160 && e.which <= 255) )) {
137
+            return String.fromCharCode(e.which);
138
+        }
139
+        // try to fallback (0-9A-Za-z and QWERTY keyboard)
140
+        switch (e.which) {
141
+        case 27:
142
+            return "Escape";
143
+        case 191:
144
+            return e.shiftKey ? "?" : "/";
145
+        }
146
+        if (e.shiftKey || e.type === "keypress") {
147
+            return String.fromCharCode(e.which);
148
+        } else {
149
+            return String.fromCharCode(e.which).toLowerCase();
150
+        }
127 151
     }
128 152
 };
129 153
 

Loading…
Cancel
Save