How to find the key code for a specific key

后端 未结 11 2036
猫巷女王i
猫巷女王i 2020-12-28 09:29

What\'s the easiest way to find the keycode for a specific key press?

Are there any good online tools that just capture any key event and show the code?

I wa

相关标签:
11条回答
  • 2020-12-28 10:05

    Just googled and result

    function displayunicode(e) {
      var unicode = e.keyCode ? e.keyCode : e.charCode;
      console.log(unicode);
    }
    <form>
      <input type="text" size="2" maxlength="1" onkeyup="displayunicode(event); this.select()" />
    </form>

    0 讨论(0)
  • 2020-12-28 10:11

    If you are looking for an online tool, https://keyjs.dev is probably the best option available:

    • By default, it displays e.key, e.code, e.which and e.keyCode for the keydown event.

    • If you need more than that, you can expand it and you will get a table with more properties for keydown, keypress and keyup.

    • If you still need more than that, open DevTools and you will see all the keys you press are logged to the console in a table-like style. Just click the row you are interested in to expand it and see the whole event object.

    Disclaimer: I'm the author.

    0 讨论(0)
  • 2020-12-28 10:17

    You can try use keycode.info to do this, it's an online tool.

    0 讨论(0)
  • 2020-12-28 10:18

    Try this:

    $('#myelement').keydown(function(event) {
      var code = event.keyCode;
      alert(code);
    }
    
    0 讨论(0)
  • 2020-12-28 10:19

        $(function () {
          $(document).keyup(function (e) {
             console.log(e.keyCode);
          });
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    Here's your online tool.

    0 讨论(0)
  • 2020-12-28 10:20

    Please try this.

    $('input').on('keyup', function(e){
       var key_code = e.which || e.keyCode;
       console.log(key_code );
    });
    

    For better help please follow the below link http://www.w3schools.com/jsref/event_key_keycode.asp

    0 讨论(0)
提交回复
热议问题