Skip to content

Commit 02eb8f7

Browse files
authored
Fix: keyboard stops working after desktop reconnect (#8135)
Fix regression from `6d33535` causing occasional keyboard entry failure due to `GrabKeyInput` overwriting handlers without saving
1 parent a2f7368 commit 02eb8f7

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

public/scripts/agent-desktop-0.0.2.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,12 @@ var CreateAgentRemoteDesktop = function (canvasid, scrolldiv) {
841841

842842
obj.GrabKeyInput = function () {
843843
if (obj.xxKeyInputGrab == true) return;
844+
// Save the current handlers so UnGrabKeyInput can restore them instead of
845+
// leaving the document without any keyboard handler. Only take ownership
846+
// of slots this instance does not already own.
847+
if (document.onkeydown !== obj.xxKeyDown) { obj._savedOnKeyDown = document.onkeydown; }
848+
if (document.onkeyup !== obj.xxKeyUp) { obj._savedOnKeyUp = document.onkeyup; }
849+
if (document.onkeypress !== obj.xxKeyPress) { obj._savedOnKeyPress = document.onkeypress; }
844850
document.onkeyup = obj.xxKeyUp;
845851
document.onkeydown = obj.xxKeyDown;
846852
document.onkeypress = obj.xxKeyPress;
@@ -849,9 +855,14 @@ var CreateAgentRemoteDesktop = function (canvasid, scrolldiv) {
849855

850856
obj.UnGrabKeyInput = function () {
851857
if (obj.xxKeyInputGrab == false) return;
852-
document.onkeyup = null;
853-
document.onkeydown = null;
854-
document.onkeypress = null;
858+
// Restore the handlers that were in place before GrabKeyInput, but only
859+
// for slots this instance still owns. A newer session may have taken over
860+
// the slots meanwhile (disconnect + reconnect of a grabbed session), in
861+
// which case leave the current handlers alone.
862+
if (document.onkeydown === obj.xxKeyDown) { document.onkeydown = obj._savedOnKeyDown || null; }
863+
if (document.onkeyup === obj.xxKeyUp) { document.onkeyup = obj._savedOnKeyUp || null; }
864+
if (document.onkeypress === obj.xxKeyPress) { document.onkeypress = obj._savedOnKeyPress || null; }
865+
obj._savedOnKeyDown = obj._savedOnKeyUp = obj._savedOnKeyPress = null;
855866
obj.xxKeyInputGrab = false;
856867
}
857868

0 commit comments

Comments
 (0)