开发者

jquery find out whether user is typing or not

开发者 https://www.devze.com 2023-01-30 13:15 出处:网络
My web page need some hotkeys, just single hotkey. So I need to find out whether user is typing or not to app开发者_开发问答ly hotkeys.You can use the keypress event on the document\'s body

My web page need some hotkeys, just single hotkey. So I need to find out whether user is typing or not to app开发者_开发问答ly hotkeys.


You can use the keypress event on the document's body

$(document.body).keypress(function(event){
  //do your stuff (event.keyCode tells you what's been pressed)
});

You can also consider using the jQuery plug-in for hotkeys.


You mean like this? (it does an alert when you press the TAB key and the focus is anywhere on the page)

$(document).keypress(function(event) {
    if (event.keyCode == 9) //TAB key
    {
        alert('this is the tab key!');
    }
});


jQuery(function($){
  $(window).keypress(function(e){ //alternatively look up the keydown and keyup functions
    switch (e.which)
    {
      case 13: //whatever key code you want to check for
      {
        //do stuff
      } return false; //prevent propagation of keypress event
      default:
      {
        //log the keycode to console so you can figure out which key is which
        //there are a number of tables of keycodes available online.
        if (window.console) console.log(e.which);
      } break;
    }
  });
});

Please do not destroy your user experience: use your powers for good or for awesome.

0

精彩评论

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

关注公众号