@@ -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