Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  194] [ 6]  / answers: 1 / hits: 22856  / 9 Years ago, mon, august 10, 2015, 12:00:00

I want to detect the Control + A event in input. I can find the Control + A event, but the function is continuing even after return false.



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 + A won't highlight text in keydown when it returned false.


More From » jquery

 Answers
8

Actually the function stops. What you are experiencing is that two keyup events trigger: the one from ctrl and the one from A.



The first one returns as expected because it does fill the requirements: ctrlKey == true and 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 but keyCode == 65 || keyCode == 97 is not.


  • If you last released the A, then ctrlKey is now false.




Then the line which sets #status to an error message is run.


[#65474] Friday, August 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacied

Total Points: 124
Total Questions: 84
Total Answers: 98

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;