KeyboardEvent: altKey property

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("UI Events")}}

The **KeyboardEvent.altKey** read-only property is a boolean value that indicates if the <kbd>alt</kbd> key (<kbd>Option</kbd> or <kbd>⌥</kbd> on macOS) was pressed (true) or not (false) when the event occurred.

When the <kbd>Alt</kbd> key itself is pressed or released, this property is true for its {{domxref("Element/keydown_event", "keydown")}} event and false for its {{domxref("Element/keyup_event", "keyup")}} event, because both events fire _after_ the state change.

Value

A boolean value.

Examples

html
<p>
  Press any character key, with or without holding down the ALT key.<br />
  You can also use the SHIFT key together with the ALT key.
</p>
<pre id="output"></pre>
js
const output = document.getElementById("output");

function showChar(e) {
  output.textContent = `Key KeyDown: "${e.key}"
ALT key KeyDown: ${e.altKey}
`;
}

document.addEventListener("keydown", showChar);

{{EmbedLiveSample("examples", "", "400")}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{ domxref("KeyboardEvent") }}