Skip to content
Advertisement

Registering keyup on Ctrl when used to change tabs in JQuery

I have create a wysiwyg HTML editor. When the user holds down keys down Ctrl, it sets a variable ctrlPressed to true. When the key up event is fired with the Ctrl keycode, then ctrlPressed is set to false.

However, when the user presses Ctrl+PgUp to change tabs, there is no way for ctrlPressed to be set to false again since when they release the control key, it fires the keyup event in the new tab, so when the user returns to the original tab with my website on it, my website still thinks Ctrl is pressed. That means when they try to type an i, it sets the text to italics and when the user presses “s”, the content saves until they press and release Ctrl again.

Is there a solution to this problem?

Advertisement

Answer

In order to set the ctrlPressed variable to false whenever an user leaves the site, you could attach the blur() event to the $(window) element like this:

$(window).blur(function(){
    ctrlPressed = false;
});
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement