[英]keyboard sequence to click on button
I would like a system administrator to easily create new accounts in an application. 我希望系统管理员可以在应用程序中轻松创建新帐户。 I was thinking keys alt and shift would trigger the "Create New User" button or defaultButton2 in my application.
我以为按键alt和shift会触发我的应用程序中的“创建新用户”按钮或defaultButton2。 I can get one key to work, but combining both keys doesn't seem to work.
我可以使用一个键来工作,但是将两个键组合起来似乎无效。
$(document).ready(function () {
$("input").bind("keydown", function (event) {
var keycode = (event.keyCode ? event.keyCode :
(event.which ? event.which : event.charCode));
if (keycode == 16 && keycode == 18) {
document.getElementById('defaultButton2').click();
return false;
} else {
return true;
}
});
});
The keydown event ( mdn ) has booleans for the shiftkey, altkey and control key to detect when combinations of buttons are pressed. keydown事件( mdn )具有Shift键,Alt键和Control键的布尔值,以检测何时按下按钮组合。 You can therefore just check those.
因此,您可以只检查那些。 The keyCode is only for the last key pressed.
keyCode仅用于最后按下的键。
If you want to detect other keys, eg if "a" and "s" are pressed at the same time, you need to mess around with custom keydown and keyup events and track things yourself. 如果要检测其他键,例如如果同时按下“ a”和“ s”,则需要弄乱自定义的keydown和keyup事件并自己进行跟踪。
$('body').on( 'keydown', function(e) { if( e.altKey && e.shiftKey ) { console.log( "Both pressed!" ); } } );
body { background-color: #DDDDDD; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Click Here
You almost did it right... 你几乎做对了...
$(document).ready(function(){
$("input").keydown(function(e) {
// 18 is the key for alt
if(e.keyCode == 18 && e.shiftKey) {
$("button").click();
}
});
});
Here is a working JSFiddle and if you're looking for the JS keycodes have a look here . 这是一个可以工作的JSFiddle ,如果您正在寻找JS键码,请在这里看看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.