最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect Ctrl + A in keyup event - Stack Overflow

programmeradmin1浏览0评论

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 - /

$('.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=".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 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.

Share Improve this question edited Aug 10, 2015 at 10:33 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Aug 10, 2015 at 6:21 VishnuVishnu 2,4526 gold badges37 silver badges59 bronze badges 8
  • What does <kbd>control</kbd> + <kbd>a</kbd> do in your browser? – Alex Booker Commented Aug 10, 2015 at 6:23
  • it prints the text ...its not breaking from function – Vishnu Commented Aug 10, 2015 at 6:29
  • keydown triggers before keyup right? – Praveen Puglia Commented Aug 10, 2015 at 6:42
  • 1 It's because when you release the last key, either there is no more ctrlKey, either the keyCode is not a or A anymore – Kaiido Commented Aug 10, 2015 at 6:45
  • @TheWarlock : yupe..check jsfiddle.net/f6rcgpmh/7 .not working for single letter. – Vishnu Commented Aug 10, 2015 at 6:45
 |  Show 3 more comments

2 Answers 2

Reset to default 20

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.

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/


If you need control on autocomplete, you have to put your controls before sending the data.

The Ctrl keyup event trigger causes your problem. I added another condition to your code,

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/.

发布评论

评论列表(0)

  1. 暂无评论