How do I go about binding a function to left and right arrow keys in Javascript and/or jQuery? I looked at the js-hotkey plugin for jQuery (wraps the built-in bind function to add an argument to recognize specific keys), but it doesn't seem to support arrow keys.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
You can use jQuery bind:
You can use KeyboardJS. I wrote the library for tasks just like this.
Checkout the library here => http://robertwhurst.github.com/KeyboardJS/
With coffee & Jquery
A terse solution using plain Javascript (thanks to Sygmoral for suggested improvements):
Also see https://stackoverflow.com/a/17929007/1397061.
You can check wether an
arrow key
is pressed by:A robust Javascript library for capturing keyboard input and key combinations entered. It has no dependencies.
http://jaywcjlove.github.io/hotkeys/