KeyboardEvent: shiftKey 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

KeyboardEvent.shiftKey 只读属性是一个布尔值,表示在事件发生时 Shift 键是否被按下(true)或未被按下(false)。

按下 Shift 键也可能改变事件的 key。例如,按下 B 会生成 key: "b",而同时按下 Shift 会生成 key: "B"

一个布尔值。

示例

html
<p>
  Press any character key, with or without holding down the SHIFT 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}"
SHIFT key KeyDown: ${e.shiftKey}
`;
}

document.addEventListener("keydown", showChar);

规范

规范
UI 事件
# dom-keyboardevent-shiftkey

浏览器兼容性

另见