` and anything with the `contentEditable` or `tabindex` attribute.
-Since Firefox 65, the `keyup` and [`keydown`](/en-US/docs/Web/API/Element/keydown_event) events are now fired during {{glossary("IME")}} composition, to improve cross-browser compatibility for CJKT users ([Firefox bug 354358](https://bugzil.la/354358). To ignore all `keyup` events that are part of composition, do something like this (229 is a special value set for a `keyCode` relating to an event that has been processed by an input-method editor ([IME](https://www.w3.org/TR/ime-api/#IME))):
-
-```js
-eventTarget.addEventListener("keyup", (event) => {
- if (event.isComposing || event.keyCode === 229) {
- return;
- }
- // do something
-});
-```
-
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
@@ -99,12 +88,21 @@ function logKey(e) {
{{EmbedLiveSample("addEventListener_keyup_example")}}
-### onkeyup equivalent
+### keyup events with IME
+
+Since Firefox 65, the [`keydown`](/en-US/docs/Web/API/Element/keydown_event) and `keyup` events are now fired during {{glossary("IME")}} composition, to improve cross-browser compatibility for CJKT users ([Firefox bug 354358](https://bugzil.la/354358)). To ignore all `keyup` events that are part of composition, do something like this:
```js
-input.onkeyup = logKey;
+eventTarget.addEventListener("keyup", (event) => {
+ if (event.isComposing) {
+ return;
+ }
+ // do something
+});
```
+> **Note:** Unlike `keydown`, `keyup` events do not have special {{domxref("KeyboardEvent/keyCode", "keyCode")}} values for IME events. However, like `keydown`, `compositionstart` may fire _after_ `keyup` when typing the first character that opens up the IME, and `compositionend` may fire _before_ `keyup` when typing the last character that closes the IME. In these cases, `isComposing` is false even when the event is part of composition.
+
## Specifications
{{Specifications}}
diff --git a/files/en-us/web/api/eventtarget/addeventlistener/index.md b/files/en-us/web/api/eventtarget/addeventlistener/index.md
index 75551655aa48a54..09a1058dbb7316b 100644
--- a/files/en-us/web/api/eventtarget/addeventlistener/index.md
+++ b/files/en-us/web/api/eventtarget/addeventlistener/index.md
@@ -328,11 +328,11 @@ class SomeClass {
someMethod(e) {
console.log(this.name);
- switch (e.keyCode) {
- case 5:
+ switch (e.code) {
+ case "ArrowUp":
// some code here…
break;
- case 6:
+ case "ArrowDown":
// some code here…
break;
}
diff --git a/files/en-us/web/api/htmltextareaelement/index.md b/files/en-us/web/api/htmltextareaelement/index.md
index 06b8fd6e65f018f..c64bbc50f4b595a 100644
--- a/files/en-us/web/api/htmltextareaelement/index.md
+++ b/files/en-us/web/api/htmltextareaelement/index.md
@@ -101,9 +101,9 @@ Make a textarea autogrow while typing:
#### JavaScript
```js
-function autoGrow(oField) {
- if (oField.scrollHeight > oField.clientHeight) {
- oField.style.height = `${oField.scrollHeight}px`;
+function autoGrow(field) {
+ if (field.scrollHeight > field.clientHeight) {
+ field.style.height = `${field.scrollHeight}px`;
}
}
```
@@ -212,71 +212,6 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis, arcu vita
{{EmbedLiveSample('Insert_HTML_tags_example', 600, 300)}}
-### Maximum length and number of lines example
-
-Create a textarea with a maximum number of characters per line and a maximum number of lines:
-
-First, create a function that takes the text field and a key event as input and determines if any of the limits have been reached. If the limit has not been reached, it will return the key.
-
-```js
-function checkRows(oField, oKeyEvent) {
- let nKey = (
- oKeyEvent ||
- /* old IE */ window.event || /* check is not supported! */ { keyCode: 38 }
- ).keyCode,
- // put here the maximum number of characters per line:
- nCols = 30,
- // put here the maximum number of lines:
- nRows = 5,
- nSelS = oField.selectionStart,
- nSelE = oField.selectionEnd,
- sVal = oField.value,
- nLen = sVal.length,
- nBackward = nSelS >= nCols ? nSelS - nCols : 0,
- nDeltaForw =
- sVal
- .substring(nBackward, nSelS)
- .search(new RegExp(`\\n(?!.{0,${String(nCols - 2)}}\\n)`)) + 1,
- nRowStart = nBackward + nDeltaForw,
- aReturns = (
- sVal.substring(0, nSelS) + sVal.substring(nSelE, sVal.length)
- ).match(/\n/g),
- nRowEnd = nSelE + nRowStart + nCols - nSelS,
- sRow =
- sVal.substring(nRowStart, nSelS) +
- sVal.substring(nSelE, nRowEnd > nLen ? nLen : nRowEnd),
- bKeepCols =
- nKey === 13 ||
- nLen + 1 < nCols ||
- /\n/.test(sRow) ||
- ((nRowStart === 0 || nDeltaForw > 0 || nKey > 0) &&
- (sRow.length < nCols ||
- (nKey > 0 && (nLen === nRowEnd || sVal.charAt(nRowEnd) === "\n"))));
-
- return (
- (nKey !== 13 || (aReturns ? aReturns.length + 1 : 1) < nRows) &&
- ((nKey > 32 && nKey < 41) || bKeepCols)
- );
-}
-```
-
-In the HTML we just need to hook our function to the `onkeypress` event and specify that our textarea does not accept pasting:
-
-```html
-
-```
-
-{{EmbedLiveSample('Maximum_length_and_number_of_lines_example', 600, 300)}}
-
## Specifications
{{Specifications}}
diff --git a/files/en-us/web/api/keyboardevent/index.md b/files/en-us/web/api/keyboardevent/index.md
index 53690f335290d9d..d231348397c2ed0 100644
--- a/files/en-us/web/api/keyboardevent/index.md
+++ b/files/en-us/web/api/keyboardevent/index.md
@@ -143,14 +143,10 @@ _This interface also inherits properties of its parents, {{domxref("UIEvent")}}
- : Returns a number representing the Unicode reference number of the key; this property is used only by the `keypress` event. For keys whose `char` property contains multiple characters, this is the Unicode value of the first character in that property. In Firefox 26 this returns codes for printable characters.
- > **Warning:** This property is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.
-
- {{domxref("KeyboardEvent.keyCode")}} {{deprecated_inline}} {{ReadOnlyInline}}
- : Returns a number representing a system and implementation dependent numerical code identifying the unmodified value of the pressed key.
- > **Warning:** This property is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.
-
- {{domxref("KeyboardEvent.keyIdentifier")}} {{Non-standard_inline}} {{deprecated_inline}} {{ReadOnlyInline}}
- : This property is non-standard and has been deprecated in favor of {{domxref("KeyboardEvent.key")}}. It was part of an old version of DOM Level 3 Events.
diff --git a/files/en-us/web/api/keyboardevent/keycode/index.md b/files/en-us/web/api/keyboardevent/keycode/index.md
index 2d5f7d7106e9c69..6bfa190a7bf3e80 100644
--- a/files/en-us/web/api/keyboardevent/keycode/index.md
+++ b/files/en-us/web/api/keyboardevent/keycode/index.md
@@ -14,45 +14,9 @@ The deprecated **`KeyboardEvent.keyCode`** read-only property represents a syste
This is usually the decimal ASCII ({{RFC(20)}}) or Windows 1252 code corresponding to the key. If the key can't be identified, this value is `0`.
-You should avoid using this if possible; it's been deprecated for some time. Instead, you should use {{domxref("KeyboardEvent.code")}}, if it's implemented. Unfortunately, some browsers still don't have it, so you'll have to be careful to make sure you use one which is supported on all target browsers.
+You should avoid using this if possible; it's been deprecated for some time. Instead, you should use {{domxref("KeyboardEvent.code")}} (for the physical key pressed) or {{domxref("KeyboardEvent.key")}} (for the character the key maps to). Check compatibility for either property if you target very old browsers.
-> **Note:** Web developers shouldn't use the `keyCode` attribute for printable characters when handling `keydown` and `keyup` events. As described above, the `keyCode` attribute is not useful for printable characters, especially those input with the Shift or Alt key pressed. When implementing a shortcut key handler, the {{domxref("Element/keypress_event", "keypress")}} event is usually better (at least when Gecko is the runtime in use).
-
-## Examples
-
-```js
-window.addEventListener(
- "keydown",
- (event) => {
- if (event.defaultPrevented) {
- return; // Should do nothing if the default action has been cancelled
- }
-
- let handled = false;
- if (event.key !== undefined) {
- // Handle the event with KeyboardEvent.key
- handled = true;
- } else if (event.keyCode !== undefined) {
- // Handle the event with KeyboardEvent.keyCode
- handled = true;
- }
-
- if (handled) {
- // Suppress "double action" if event handled
- event.preventDefault();
- }
- },
- true,
-);
-```
-
-## Specifications
-
-{{Specifications}}
-
-## Browser compatibility
-
-{{Compat}}
+> **Note:** Web developers shouldn't use the `keyCode` attribute for printable characters when handling `keydown` and `keyup` events. As described above, the `keyCode` attribute is not useful for printable characters, especially those input with the Shift or Alt key pressed.
## Value of keyCode
@@ -3299,6 +3263,42 @@ Gecko defines a lot of `keyCode` values in `KeyboardEvent` for making the mappin
+## Examples
+
+```js
+window.addEventListener(
+ "keydown",
+ (event) => {
+ if (event.defaultPrevented) {
+ return; // Should do nothing if the default action has been cancelled
+ }
+
+ let handled = false;
+ if (event.key !== undefined) {
+ // Handle the event with KeyboardEvent.key
+ handled = true;
+ } else if (event.keyCode !== undefined) {
+ // Handle the event with KeyboardEvent.keyCode
+ handled = true;
+ }
+
+ if (handled) {
+ // Suppress "double action" if event handled
+ event.preventDefault();
+ }
+ },
+ true,
+);
+```
+
+## Specifications
+
+{{Specifications}}
+
+## Browser compatibility
+
+{{Compat}}
+
### OEM specific keys on Windows
On Windows, some values of virtual keycode are defined (reserved) for OEM specific key. They are available for special keys on non-standard keyboard. In other words, some values are used for different meaning by two or more vendors (or hardware).
diff --git a/files/en-us/web/media/audio_and_video_delivery/index.md b/files/en-us/web/media/audio_and_video_delivery/index.md
index f6d2eaddf23dfcd..a72152a70462a87 100644
--- a/files/en-us/web/media/audio_and_video_delivery/index.md
+++ b/files/en-us/web/media/audio_and_video_delivery/index.md
@@ -282,7 +282,7 @@ window.onload = () => {
}
function checkKey(e) {
- if (e.keycode === 32) {
+ if (e.code === "Space") {
// space bar
switchState();
}