From beaf996b07a9f325296d375427bcd999f8d8b157 Mon Sep 17 00:00:00 2001 From: Ricester <48448626+Ricester@users.noreply.github.com> Date: Sun, 9 Jun 2024 10:00:18 +0100 Subject: [PATCH] Added arrow key functionality Added keyboard movement to controls in README, added extra conditions so that arrow keys are useable for paddle movement in the keyDownHandler() function. --- README.md | 2 +- script.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b731d43..1f7d28c 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ You can use Gitpod in the cloud [![Gitpod Ready-to-Code](https://img.shields.io/ ## Controls -- Move mouse to control the paddle +- Move mouse to control the paddle, or use 'A'and the left arrow key or 'D' and the right arrow key for left and right movement respectively. Created with diff --git a/script.js b/script.js index 4a26d8c..3471ecf 100644 --- a/script.js +++ b/script.js @@ -244,11 +244,11 @@ function mouseMoveHandler(e) { } } -document.addEventListener("keypress", keyDownHandler); +document.addEventListener("keydown", keyDownHandler); function keyDownHandler(e) { - // "D" for right and "A" for left movement - if(e.code == "KeyD") { + // "D" or right arrow key for right and "A" or left arrow key for left movement + if(e.code == "KeyD" || e.code == "ArrowRight") { let relativeX = paddleX + 10; if(relativeX < canvas.width - 100) { @@ -256,7 +256,7 @@ function keyDownHandler(e) { } } - if(e.code == "KeyA") { + if(e.code == "KeyA" || e.code == "ArrowLeft") { let relativeX = paddleX - 10; if(relativeX > 0 ) {