我的网页需要一些热键,只需一个热键。因此,我需要确定用户是否正在键入以应用热键。
答案 0 :(得分:2)
您可以在文档正文上使用keypress事件
$(document.body).keypress(function(event){
//do your stuff (event.keyCode tells you what's been pressed)
});
答案 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;
}
});
});
请不要破坏您的用户体验:将您的权力用于良好或非常棒。