最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Focus on field after keydown event without inserting character - Stack Overflow

matteradmin4PV0评论

I have a shortcut key K. It should focus on my input, but I don't want it to insert the letter K when it focuses.

$(document).keydown(function(event) { 
    if (event.which == 75) {
        $('input').focus();
    }
});
<script src=".1.0/jquery.min.js"></script>
<input type="text">

I have a shortcut key K. It should focus on my input, but I don't want it to insert the letter K when it focuses.

$(document).keydown(function(event) { 
    if (event.which == 75) {
        $('input').focus();
    }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text">

Share Improve this question edited Jan 12, 2016 at 11:03 Praveen Kumar Purushothaman 167k27 gold badges213 silver badges260 bronze badges asked Jan 12, 2016 at 10:52 Paran0aParan0a 3,4573 gold badges27 silver badges48 bronze badges 1
  • event.preventDefault(); – Johannes Jander Commented Jan 12, 2016 at 10:53
Add a ment  | 

4 Answers 4

Reset to default 4

You can use event.preventDefault() to stop the standard behaviour of the event. Note however that this will stop the letter K from being able to be typed in the input. To allow that you need to add a keydown handler to the input itself which stops the event propagation reaching the document. Try this:

$(document).keydown(function(event) {
  if (event.which == 75) {
    event.preventDefault();
    $('input').focus();
  }
});

$('input').keydown(function(e) {
  e.stopPropagation();
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text">

This is another way:

At the time of keydown, if it is k and the input does not have focus then prevent the default behavior and give focus to the text field.

$(document).keydown(function(event) {
  if (event.which == 75 && !$('input').is(":focus")) {
    event.preventDefault();
    $('input').focus();
  }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text">

If you want to abruptly stop the propagation, you can also use return false;:

$(document).keydown(function(event) {
  if (event.which == 75) {
    $('input').focus();
    return false;
  }
});
$('input').keydown(function(e) {
  e.stopPropagation();
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text">

var input = $('input');

$(document).on('keyup', function (e) {
  if(input.is(':focus')){
    return;
  }
  if (e.which == 75) {
        input.focus();
    }
});
  1. Listen to the keyup event;
  2. If the input is already focused, don't focus again.
Post a comment

comment list (0)

  1. No comments so far