2

I expect the result is 13. but the browser console give me
{ "altKey": false, "ctrlKey": true, "shiftKey": false, "char": undefined, "charCode": 10, "key": "Enter", "keyCode": 10 }

why 10 , is it normal? it show 10 too if I press CTRL+j

$('body').keypress(
    ({altKey,ctrlKey,shiftKey,char,charCode,key,keyCode})=>
        console.log({altKey,ctrlKey,shiftKey,char,charCode,key,keyCode})
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  • See this answer to a similar problem: https://stackoverflow.com/a/2904944/1532004 note: the solution is the same, just check for keycode m instead of c or v. – Zim84 Jul 18 '18 at 05:08

2 Answers2

0

Check just for the value of 13 in event.keyCode.

$('body').keypress((event) => {
    if (event.ctrlKey && event.key === 'm') {
      console.log("ctrl+m pressed");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
  • event.keyCode === 13 is for Enter key not for ctrl+m – Pramod Jul 18 '18 at 04:56
  • hmm, "ctrl+m" and "enter" were both generating a keyCode of 13 in my browser, but you're right, that's ambiguous, I've updated my code snippet with improved code, thanks for pointing it out – pretzelhammer Jul 18 '18 at 05:04
0

check below code 77 keycode for "m"

function KeyPress(e) {
      var evtobj = window.event? event : e
      console.log(evtobj.keyCode);
      if (evtobj.keyCode == 77 && evtobj.ctrlKey) alert("Ctrl+m");
}

document.onkeydown = KeyPress;
Pramod
  • 424
  • 2
  • 7
  • 28