Jelajahi Sumber

Release down/pressed keys when window loses focus.

May window managers have a keyboard shortcut that switch away from the
current desktop (e.g. desktop switcher). Unfortunately, when this
happens, the meta/control keys that are used with the shortcut will
send a down event to the browser, but the up event will never be sent
because the browser no longer has focus at the point when the up event
happens. This can cause weird stuck key issues for VNC clients (not
just noVNC). To get around this, we try and detect when the browser
loses focus and release any keys that are on the keyDownList.

As an aside, if you run into this situation (in noVNC or another VNC
client), you can unstick the state by pressing and releasing the Ctrl,
Shift, Alt, etc.

Addresses: https://github.com/kanaka/noVNC/pull/135
Joel Martin 13 tahun lalu
induk
melakukan
6671c7624d
1 mengubah file dengan 27 tambahan dan 0 penghapusan
  1. 27 0
      include/input.js

+ 27 - 0
include/input.js

@@ -412,6 +412,26 @@ function onKeyUp(e) {
     return false;
 }
 
+function allKeysUp() {
+    Util.Debug(">> Keyboard.allKeysUp");
+    if (keyDownList.length > 0) {
+        Util.Info("Releasing pressed/down keys");
+    }
+    var i, keysym, fevt = null;
+    for (i = keyDownList.length-1; i >= 0; i--) {
+        fevt = keyDownList.splice(i, 1)[0];
+        keysym = fevt.keysym;
+        if (conf.onKeyPress && (keysym > 0)) {
+            Util.Debug("allKeysUp, keysym: " + keysym +
+                    " (keyCode: " + fevt.keyCode +
+                    ", which: " + fevt.which + ")");
+            conf.onKeyPress(keysym, 0, fevt);
+        }
+    }
+    Util.Debug("<< Keyboard.allKeysUp");
+    return;
+}
+
 //
 // Public API interface functions
 //
@@ -424,6 +444,9 @@ that.grab = function() {
     Util.addEvent(c, 'keyup', onKeyUp);
     Util.addEvent(c, 'keypress', onKeyPress);
 
+    // Release (key up) if window loses focus
+    Util.addEvent(window, 'blur', allKeysUp);
+
     //Util.Debug("<< Keyboard.grab");
 };
 
@@ -434,6 +457,10 @@ that.ungrab = function() {
     Util.removeEvent(c, 'keydown', onKeyDown);
     Util.removeEvent(c, 'keyup', onKeyUp);
     Util.removeEvent(c, 'keypress', onKeyPress);
+    Util.removeEvent(window, 'blur', allKeysUp);
+
+    // Release (key up) all keys that are in a down state
+    allKeysUp();
 
     //Util.Debug(">> Keyboard.ungrab");
 };