Search code examples
javascripthtmlpreventdefaultnum-lock

preventDefault() action that works on numlock key 144 so that numberpad can still be used for shortcuts


I'm trying to build a website that uses the numberpad for keyboard shortcuts to push different buttons. For one of the events I need more than 9 shortcuts. To keep things simple on the user's end I'd like to use the numlock key #144. The problem is that when they use the number lock key it prevents the numbers from working for future button presses. I've tried using the preventDefault(); function but it doesn't seem to work. Is there an alternative that would enable me to do this?

if (e.which == 144) { // numlock on the numberpad
  preventDefault();
  document.getElementById("button1").click();
}
if (e.which == 96) { // 0 on the numberpad
  document.getElementById("button2").click();
}

Solution

  • You need to call preventDefault off your event parameter, generally e. There's no global preventDefault.

    Also, you can't control the state of the numlock. That is a low level hardware/software implementation.