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

瀏覽器相容性

另見