javascript 在 keyup 事件中检测 Ctrl + A
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31912784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect Ctrl + A in keyup event
提问by Vishnu
I want to detect the Control+ Aevent in input. I can find the Control+ Aevent, but the function is continuing even after return false
.
我想检测输入中的Control+A事件。我可以找到Control+A事件,但即使在return false
.
jsFiddle - http://jsfiddle.net/f6rcgpmh/4/
jsFiddle - http://jsfiddle.net/f6rcgpmh/4/
$('.searchTerm').keyup(function(e) {
$("#status").text("");
if (e.ctrlKey) {
if (e.keyCode == 65 || e.keyCode == 97) { // 'A' or 'a'
console.log("Control pressed");
e.preventDefault();
return false;
}
}
$("#status").text("This should not work if Ctrl + A is pressed");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="search" class="search">
<input class="searchTerm" placeholder="Filter Books...">
<input class="searchButton" type="submit">
</form>
<div id="status"></div>
I want this to work in keyup
not in keydown
. Because I am using autosearch and I don't want to call function before keyrelease. And also Ctrl+ Awon't highlight text in keydown when it returned false.
我希望这在keyup
not in 中起作用keydown
。因为我正在使用自动搜索并且我不想在 keyrelease 之前调用函数。而且Ctrl+A不突出的keydown文本时,它返回false。
采纳答案by Kaiido
Actually the function stops. What you are experiencing is that two keyup events trigger: the one from ctrland the one from A.
实际上功能停止了。您遇到的是两个 keyup 事件触发:一个 fromctrl和一个 from A。
The first one returns as expected because it does fill the requirements: ctrlKey == true
and keyCode == 65 || keyCode == 97
.
第一个按预期返回,因为它确实满足要求:ctrlKey == true
和keyCode == 65 || keyCode == 97
。
But the second one, there will be only one key pressed so both statements can't be true together:
但是第二个,只会按下一个键,所以这两个陈述不能同时成立:
If you last released the ctrl, then
ctrlKey
is true butkeyCode == 65 || keyCode == 97
is not.If you last released the A, then
ctrlKey
is now false.
如果您上次发布了ctrl,
ctrlKey
则为 true 但keyCode == 65 || keyCode == 97
不是。如果您上次发布了A,那么
ctrlKey
现在是假的。
Then the line which sets #status
to an error message is run.
然后#status
运行设置为错误消息的行。
回答by ebilgin
Actually it's not. You must change your event from 'keyup'
to 'keydown'
. Then try it again. You can check this fiddle. http://jsfiddle.net/ebilgin/f6rcgpmh/5/
其实不是。您必须将事件从 更改'keyup'
为'keydown'
。然后再试一次。你可以检查这个小提琴。http://jsfiddle.net/ebilgin/f6rcgpmh/5/
If you need control on autocomplete, you have to put your controls before sending the data.
如果您需要控制自动完成,则必须在发送数据之前放置您的控件。
The Ctrlkeyup event trigger causes your problem. I added another condition to your code,
该Ctrlkeyup事件触发器,使您的问题。我在你的代码中添加了另一个条件,
if (e.keyCode == 17) // Ctrl key, fires at Ctrl's keyup.
return false;
You can check my new fiddle, http://jsfiddle.net/ebilgin/f6rcgpmh/10/.
你可以检查我的新小提琴,http://jsfiddle.net/ebilgin/f6rcgpmh/10/。