gpt4 book ai didi

javascript - jQuery - 按下按键时执行代码并在释放按键时将其恢复正常

转载 作者:太空宇宙 更新时间:2023-11-04 15:39:55 24 4
gpt4 key购买 nike

这个想法是,如果碰巧按下了普通键和修饰键,则将它们链接起来。 cCtrl c

以下作品:

var inc = 10;

$(document).keydown(function(e) {
switch(e.which) {
case 16: // shift
inc = 100;

break;

case 17: // ctrl
inc = 1;

break;
}
});

$(document).keyup(function() {
inc = 10;
});

当按下按键时,变量被设置为相应的值,当松开按键时,变量将恢复正常。但我想知道是否:

A - 您可以等待第一个函数完成,然后执行使其恢复正常的代码

B - 如果有某种内置方法可以将其恢复为原始值而无需手动设置

最佳答案

如果您想知道 CTRLSHIFT 键是否用作修饰符,请不要查找它的键码。相反,请使用事件的 ctrlKeyshiftKey 属性。试试这个:

var inc = 10;
$('input').val(inc);

$(document).keydown(function(e) {
if (e.shiftKey) {
inc = 100;
} else if (e.ctrlKey) {
inc = 1;
}

$('input').val(inc);
});

$(document).keyup(function() {
inc = 10;

$('input').val(inc);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />

回答您的问题:

A - You can just wait for the first function to finish, and then execute the code that returns it to normal

B - If there's some kind of built-in way to just revert it back to its original value without manually setting it

这两个问题的答案都是否定的。您使用的模式是最好的方法。

关于javascript - jQuery - 按下按键时执行代码并在释放按键时将其恢复正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43973381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com