MouseEvent: ctrlKey 属性
MouseEvent.ctrlKey 只读属性是一个布尔值,表示在给定鼠标事件发生时,Ctrl 键是否被按下。
在 Macintosh 键盘上,此键标有 Control 键。另外请注意,在 Mac 上,按住 Control 键的单击操作会被操作系统拦截并用于打开上下文菜单,因此在单击事件中无法检测到 ctrlKey。
使用触控板进行捏合缩放也会发送一个模拟的 wheel 事件,并将 ctrlKey 设置为 true。
值
一个布尔值,其中 true 表示按下了该键,false 表示该键未按下。
示例
此示例在触发 mousemove 事件时记录 ctrlKey 属性。
HTML
html
<p id="log">The ctrl key was pressed while the cursor was moving: false</p>
JavaScript
js
const log = document.querySelector("#log");
window.addEventListener("mousemove", logKey);
function logKey(e) {
log.textContent = `The ctrl key was pressed while the cursor was moving: ${e.ctrlKey}`;
}
结果
规范
| 规范 |
|---|
| UI 事件 # dom-mouseevent-ctrlkey |
浏览器兼容性
加载中…