4

If I want to check if enter is pressed, I will do something like:

addEventListener('keypress', function (e) {
    if (e.key === 'Enter') alert('You pressed enter!');
});

So, will that same code work on a Mac, will it register mac's return as enter?

jukenduit
  • 322
  • 3
  • 15
  • 3
    Yes they are same,Possible duplicate of [How to detect pressing Enter on keyboard using jQuery?](https://stackoverflow.com/questions/979662/how-to-detect-pressing-enter-on-keyboard-using-jquery) – Just code Dec 20 '18 at 11:00
  • 1
    13 is equal for enter key, you can try `e.key === 13` which should return true on every where mac, windows etc – Ayaz Ali Shah Dec 20 '18 at 11:01
  • [javascript - Replacement for deprecated `keypress` DOM event - Stack Overflow](https://stackoverflow.com/questions/52882144/replacement-for-deprecated-keypress-dom-event) – Andreas Dec 20 '18 at 11:06
  • As @AyazShah stated, use the code - this will be more reliable. – Phil Cooper Dec 20 '18 at 11:10
  • @Justcode This is not a jQuery question but a pure javascript question – Esko Dec 20 '18 at 11:15
  • @Justcode This is pure javascript, how did jquery come into this? And I still don't have a definite answer. So `e.key` will be equal to `Enter` on a mac? – jukenduit Dec 20 '18 at 12:21
  • @jukenduit I know this is javascript, the question is about the key code, instead of the enter you can use 13 that's it, its not about javascript or jquery its about keycode. – Just code Dec 20 '18 at 12:22
  • @Justcode No, the Q is about key not keycode. I can use e.keycode 13 but not e.key 13. – jukenduit Dec 20 '18 at 12:23
  • @jukenduit there are 2 questions included, both are answered in my comment. please check – Just code Dec 20 '18 at 12:24

1 Answers1

0

As we can see in the MDN table the answer is "Yes", they are the same.

Jaood_xD
  • 808
  • 2
  • 4
  • 16