Skip to content

Commit

Permalink
Remove (almost) all usage of keyCode in examples (#34122)
Browse files Browse the repository at this point in the history
Remove all usage of keyCode in examples
  • Loading branch information
Josh-Cena committed Jun 14, 2024
1 parent 3b8be0a commit af1e384
Show file tree
Hide file tree
Showing 24 changed files with 144 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ let upPressed = false;
let downPressed = false;
```

Then we will listen for the `keydown` and `keyup` events and act accordingly in both handler functions. Inside them we can get the code of the key that was pressed from the [keyCode](/en-US/docs/Web/API/KeyboardEvent/keyCode) property of the event object, see which key it is, and then set the proper variable. There are no helpers so you have to remember what the given codes are (or [look them up](/en-US/docs/Web/API/KeyboardEvent/keyCode#value_of_keycode)); `37` is the left arrow:
Then we will listen for the `keydown` and `keyup` events and act accordingly in both handler functions. Inside them we can get the code of the key that was pressed from the [`code`](/en-US/docs/Web/API/KeyboardEvent/code) property of the event object, see which key it is, and then set the proper variable. The codes are all readable string names, but you can [look them up](/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values) to be sure; `"ArrowLeft"` is the left arrow:

```js
function keyDownHandler(event) {
if (event.keyCode === 39) {
if (event.code === "ArrowRight") {
rightPressed = true;
} else if (event.keyCode === 37) {
} else if (event.code === "ArrowLeft") {
leftPressed = true;
}
if (event.keyCode === 40) {
if (event.code === "ArrowDown") {
downPressed = true;
} else if (event.keyCode === 38) {
} else if (event.code === "ArrowUp") {
upPressed = true;
}
}
```

The `keyUpHandler` looks almost exactly the same as the `keyDownHandler` above, but instead of setting the pressed variables to `true`, we would set them to `false`. If the left arrow is pressed (<kbd>⬅︎</kbd>; key code 37), we can set the `leftPressed` variable to `true` and in the `draw` function perform the action assigned to it — move the ship left:
The `keyUpHandler` looks almost exactly the same as the `keyDownHandler` above, but instead of setting the pressed variables to `true`, we would set them to `false`. If the left arrow is pressed (<kbd>⬅︎</kbd>; `"ArrowLeft"`), we can set the `leftPressed` variable to `true` and in the `draw` function perform the action assigned to it — move the ship left:

```js
function draw() {
Expand All @@ -73,20 +73,6 @@ function draw() {

The `draw` function first clears the whole Canvas — we draw everything from scratch on every single frame. Then the pressed key variables are checked and the `playerX` and `playerY` variables (that we define earlier just after `leftPressed` and the others) holding the position of the ship are adjusted by a given amount, let's say 5 pixels. Then the player's ship is drawn on the screen and the next draw is called from within the [requestAnimationFrame](/en-US/docs/Web/API/Window/requestAnimationFrame).

We could write our own `KeyCode` object containing the key codes. For example:

```js
const KeyboardHelper = { left: 37, up: 38, right: 39, down: 40 };
```

That way instead of using the codes to compare the input in the handler functions, we could do something like this, which is arguably easier to remember:

```js
leftPressed = event.keyCode === KeyboardHelper.left;
```

> **Note:** You can also find a list of the different keycodes and what keys they relate to in the [keyCode](/en-US/docs/Web/API/KeyboardEvent/keyCode) reference page.
![Pure JavaScript demo containing player's ship (with stars in the background) that can be controlled with keyboard and mouse.](controls-purejsgame.png)

You can see this example in action online at [end3r.github.io/JavaScript-Game-Controls](https://end3r.github.io/JavaScript-Game-Controls/) and the full source code can be found at [github.com/end3r/JavaScript-Game-Controls](https://github.com/end3r/JavaScript-Game-Controls/).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -427,12 +427,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -586,12 +586,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ window.addEventListener("load", updateCode);
// stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead
textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -350,12 +350,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -622,12 +622,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ window.addEventListener("load", updateCode);
// Stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead
textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -393,12 +393,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -552,12 +552,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -714,12 +714,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -933,12 +933,12 @@ window.addEventListener("load", updateCode);
// Stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead
textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = function (e) {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -547,12 +547,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = function (e) {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,12 @@ solution.addEventListener("click", () => {
// stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead
textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -826,12 +826,12 @@ solution.addEventListener("click", () => {
// stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead
textarea.onkeydown = (e) => {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,12 +710,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = function (e) {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -891,12 +891,12 @@ window.addEventListener("load", updateCode);
// make it write a tab at the caret position instead

textarea.onkeydown = function (e) {
if (e.keyCode === 9) {
if (e.code === "Tab") {
e.preventDefault();
insertAtCaret("\t");
}

if (e.keyCode === 27) {
if (e.code === "Escape") {
textarea.blur();
}
};
Expand Down
14 changes: 4 additions & 10 deletions files/en-us/learn/javascript/first_steps/arrays/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,12 @@ window.addEventListener("load", updateCode);
// stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead

const KEY_TAB = 9;
const KEY_ESC = 27;

textarea.onkeydown = (event) => {
if (event.keyCode === KEY_TAB) {
if (event.code === "Tab") {
event.preventDefault();
insertAtCaret("\t");
}
if (event.keyCode === KEY_ESC) {
if (event.code === "Escape") {
textarea.blur();
}
};
Expand Down Expand Up @@ -626,15 +623,12 @@ window.addEventListener("load", updateCode);
// stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead

const KEY_TAB = 9;
const KEY_ESC = 27;

textarea.onkeydown = (event) => {
if (event.keyCode === KEY_TAB) {
if (event.code === "Tab") {
event.preventDefault();
insertAtCaret("\t");
}
if (event.keyCode === KEY_ESC) {
if (event.code === "Escape") {
textarea.blur();
}
};
Expand Down
Loading

0 comments on commit af1e384

Please sign in to comment.