简体   繁体   中英

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM