I’m struggling with the syntax for this user script I’m trying to write, which should basically follow this logic:
IF (LeftArrowKeyIsPressed) THEN doSomething; ELSE IF (RightArrowKeyIsPressed) THEN doSomethingElse; ELSE IF (any other key pressed) THEN doNothing;
Can someone help me out with the code syntax for structuring the check for the keypress? Assume an onkeydown event is being used.
Add an event listener, probably to the window, using
addEventListener. Then switch off theEvent.whichvalue to do what you want.Here’s the code. See it in action at jsFiddle.:
This code works on userscripts-applicable browsers (not IE). The jQuery version is the same except change the
addEventListenerline to: