jquery找出用户是否正在打字

时间:2010-12-10 19:15:57

标签: jquery

我的网页需要一些热键,只需一个热键。因此,我需要确定用户是否正在键入以应用热键。

3 个答案:

答案 0 :(得分:2)

您可以在文档正文上使用keypress事件

$(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.

答案 1 :(得分:1)

你的意思是this? (当您按TAB键并且焦点位于页面上的任何位置时,它会发出警报)

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

答案 2 :(得分:1)

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;
    }
  });
});

请不要破坏您的用户体验:将您的权力用于良好或非常棒。