From 6671c7624d19565f350556fd06adf84770ed662b Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Tue, 13 Mar 2012 20:29:02 -0500 Subject: [PATCH] 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 --- include/input.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/include/input.js b/include/input.js index 3124d083..1dfe7199 100644 --- a/include/input.js +++ b/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"); };