Learn Create Your Own Split Screen

Next Page

Next we'll add a listener so we can rotate whenever a key is pressed, using the left and right arrows.

Adding the listener:

addEventListener("keydown", MyKeyDownHandler);
And the function that gets called when the keydown event is heard:
function MyKeyDownHandler (MyEvent) { if (MyEvent.keyCode == 37) {thing.angle = thing.angle - 15}; // left if (MyEvent.keyCode == 39) {thing.angle = thing.angle + 15}; // right MyEvent.preventDefault(); }

Our code changes the angle of rotation by 15 degrees for each key press.


If this sort of code seems unfamiliar, then it may be worth going back and reviewing Responding to Keys.