开发者

JavaScript key listener disabled when inside a text form

I have a key listener assigned to the arrow keys to navigate a slideshow. But I want to disable the key listener, temporarily, while a user is typing inside an input field. How can I do that? My current code looks like this:

//Listen to the keys
    function checkKey(e) {
    switch (e.keyCode) {
    case 37:
        changeImage('prev');
        break;
    case 39:
        changeImage('next');;
        break;
        }
    }
    if (jQuery.browser.mozilla) {
            jQu开发者_运维百科ery(document).keypress (checkKey);
    } else {
            jQuery(document).keydown (checkKey);
    }


First, there's no need for the browser check. For checking arrow keys, just use the keydown event for all keys.

Second, I suggest (as Sean Hogan did) checking the target of the event before doing the slideshow stuff. The following will work on all mainstream desktop browsers:

document.body.onkeydown = function(evt) {
    evt = evt || window.event;
    var target = evt.target || evt.srcElement;
    var targetTagName = (target.nodeType == 1) ? target.nodeName.toUpperCase() : "";
    if ( !/INPUT|SELECT|TEXTAREA/.test(targetTagName) ) { 
        switch (evt.keyCode) {
            case 37:
                changeImage('prev');
                break;
            case 39:
                changeImage('next');
                break;
        }
    }
}


A bit ugly, but should work:

var moz = jQuery.browser.mozilla;
if (moz) {
    jQuery(document).keypress(checkKey);
} else {
    jQuery(document).keydown(checkKey);
}
jQuery("#myInput").focus(function() {
    if (moz) {
        jQuery(document).unbind("keypress");
    } else {
        jQuery(document).unbind("keydown");
    }
}).blur(function() {
    if (moz) {
        jQuery(document).keypress(checkKey);
    } else {
        jQuery(document).keydown(checkKey);
    }
});


If the focus is on an input element then that element will be the target for key events.

So you could just do a check on event.target.tagName.

e.g.

function checkKey(e) {  
  switch (e.target.tagName) {  
    case "INPUT": case "SELECT": case "TEXTAREA": return;  
  }  
  // rest of your handler goes here ...  
}  


Add onfocus and onblur event to the input field and set a global variable value. Check for that global variable in the begining of your checkKey event handler.

<input type="textbox" onfocus="window.inTextBox = true;" onblur="window.inTextBox = false;" />

function checkKey(e) {
 if (!window.inTextBox)
 {
  ...
 }
}


I really like the simplicity of Ilya Volodin's suggestion, but I would set the event handler in the script and not embed it into the html:

  var textFocus = false; 

  $("textbox").focus(function() {
      textFocus = true;
   });

  $("textbox").blur(function() {
      textFocus = false;
   });

  function navKeys() {
       if (textFocus) {
            return false;
       } else {
       ......
       }
   }

This would be even simpler if jquery had :focus as a selector.

      function navKeys() {
       if ($("textbox:focus") {
            return false;
       } else {
       ......
       }
   }

But that is just hypothetical code at this point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜