JavaScript 键盘与鼠标事件监听详解:KeyboardEvent 与 MouseEvent 属性解析
本文详细介绍 JavaScript 中键盘和鼠标事件的监听方法。重点解析 KeyboardEvent 的 key、code 及修饰键属性,以及 MouseEvent 的按钮状态、坐标和修饰键属性。通过代码示例展示如何实时获取按键和点击数据,并附带常见键值参考表,帮助开发者掌握前端交互事件处理。
JavaScript 键盘与鼠标事件监听概述
在实际开发场景中,监听用户的按键输入与鼠标点击是常见需求。本文将对这两类事件的处理机制进行系统梳理。
KeyboardEvent 键盘事件详解
KeyboardEvent
KeyboardEvent 对象用于描述键盘交互行为,每个事件实例对应一次按键操作。通过 keydown、keypress 和 keyup 三种事件类型可以区分按键的不同状态。需要注意的是,KeyboardEvent 仅反映物理按键动作,若需处理文本输入内容,建议使用 HTML5 的 input 事件,因为在某些手持设备(如平板电脑)上,按键事件可能不会被触发。
方法
该接口继承了父级对象 UIEvent 和 Event 的方法。
- KeyboardEvent.getModifierState() 方法返回一个布尔值,指示在事件创建时,Alt、Shift、Ctrl 或 Meta 等修饰键是否处于按下状态。
- KeyboardEvent.initKeyEvent() 用于初始化一个 KeyboardEvent 对象。目前推荐的标准做法是使用 KeyboardEvent() 构造函数。
- KeyboardEvent.initKeyboardEvent() 同样用于初始化 KeyboardEvent 对象,但现代标准推荐使用 KeyboardEvent() 构造函数替代此方法。
以下介绍常用的属性:
- KeyboardEvent.key
- KeyboardEvent.code
- KeyboardEvent.ctrlKey
- KeyboardEvent.shiftKey
- KeyboardEvent.altKey
- KeyboardEvent.metaKey
ctrlKey、shiftKey、altKey 和 metaKey 属性较为直观,用于判断按下键盘时对应的 Ctrl、Shift、Alt 或 Meta 键是否同时被按下。若已按下,则返回 true,这在判断组合键(如 Alt + a)时非常有用。关于 metaKey,在 Mac 平台上它代表 Command 键(⌘),而在 Windows 平台上代表 Windows 键(⊞)。需要注意的是,并非所有 Windows 键盘都配备此键。接下来将重点介绍 key 和 code 这两个关键属性。
KeyboardEvent.key
如果按下的键代表一个可打印字符,key 属性的值即为该字符本身(例如:a、Enter、Shift、CapsLock、Backspace)。
KeyboardEvent.code
code 属性的值相对特殊,它表示物理按键在键盘上的具体位置。例如,按下字母 a 键时,code 值为 KeyA;按下左侧 Shift 键时,code 值为 ShiftLeft。这意味着 code 反映的是按键的物理位置,而非其产生的字符。由于不同语言布局的键盘在同一物理位置上可能对应不同字符,因此按同一个物理键时,key 值可能不同,但 code 值保持一致。例如,KeyQ 通常代表普通键盘上的 q 键,但在 Dvorak 键盘布局下,该位置对应的字符是单引号('),此时 key 值会变化,而 code 值仍为 KeyQ。
KeyboardEvent.keyCode 只读属性
该属性返回一个表示系统和实现相关的数字代码,用于标识按键的未修改值。了解上述属性后,即可通过代码实时获取输入的键值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>键盘事件监听</title>
<style>
* {
margin: 0;
padding: 0;
}
#container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
padding-top: 20px;
}
table,
table tr th,
table tr td {
border: 1px solid #000;
}
table {
min-height: 25px;
line-height: 25px;
text-align: center;
border-collapse: collapse;
padding: 2px;
}
th {
width: 150px;
}
</style>
<script type="text/javascript" language=JavaScript>
window.onload = function () {
const key = document.getElementById('key');
const keyCode = document.getElementById('keyCode');
const code = document.getElementById('code');
const ctrlKey = document.getElementById('ctrlKey');
const metaKey = document.getElementById('metaKey');
const shiftKey = document.getElementById('shiftKey');
const altKey = document.getElementById('altKey');
const combineKey = document.getElementById('combineKey');
document.onkeydown = function(event) {
var e = event || window.event || arguments.callee.caller.arguments[0];
e.preventDefault(); //阻止默认事件
// 设置获取的值
key.innerHTML = e.key;
keyCode.innerHTML = e.keyCode;
code.innerHTML = e.code;
ctrlKey.innerHTML = e.ctrlKey;
metaKey.innerHTML = e.metaKey;
shiftKey.innerHTML = e.shiftKey;
altKey.innerHTML = e.altKey;
if (e.altKey || e.shiftKey || e.metaKey || e.ctrlKey) {
let result = '';
if (e.altKey) {
result = 'Alt';
} else if (e.shiftKey) {
result = 'Shift';
} else if (e.metaKey) {
result = 'Meta';
} else if (e.ctrlKey) {
result = 'Control';
}
combineKey.innerHTML = result !== e.key ? `${result} + ${e.key}` : `${result}`;
} else {
combineKey.innerHTML = '-';
}
};
}
</script>
</head>
<body>
<div id="container">
<table border="0px">
<tr>
<th>key</th>
<th>keyCode</th>
<th>code</th>
<th>ctrlKey</th>
<th>metaKey</th>
<th>shiftKey</th>
<th>altKey</th>
<th>组合健</th>
</tr>
<tr>
<td id="key">-</td>
<td id="keyCode">-</td>
<td id="code">-</td>
<td id="ctrlKey">-</td>
<td id="metaKey">-</td>
<td id="shiftKey">-</td>
<td id="altKey">-</td>
<td id="combineKey">-</td>
</tr>
</table>
<hr />
</div>
</body>
</html>
输出结果如下:

当我们在键盘上输入键值时,会有相应的属性显示,也可以点击该链接实时尝试:
See the Pen keyboardEvent by suwu150 (@suwu150) on CodePen.
MouseEvent 鼠标事件详解
MouseEvent
了解了键盘事件监听后,接下来学习鼠标事件的监听。MouseEvent 接口用于处理用户与指针设备(如鼠标)的交互,常见事件包括 click、dblclick、mouseup 和 mousedown。
以下介绍常用的属性:
- MouseEvent.altKey 只读属性,当鼠标事件触发时,如果 Alt 键被按下,则返回 true。
- MouseEvent.button 只读属性,当鼠标事件触发时,如果鼠标按钮被按下(如有),将返回一个数值。这些数值的含义分别为:button=0 表示鼠标左键,button=2 表示鼠标右键,button=1 表示鼠标中间键。
- MouseEvent.buttons 只读属性,当鼠标事件触发时,如果多个鼠标按钮被按下(如有),将返回一个或多个代表鼠标按钮的数字。这些数值的含义分别为:buttons=1 表示鼠标左键,buttons=2 表示鼠标右键,buttons=3 表示同时按下左键和右键,buttons=4 表示鼠标中间键,buttons=5 表示同时按下左键和中间键,buttons=6 表示同时按下中间键和右键,buttons=7 表示同时按下左键、右键和中间键。
- MouseEvent.clientX 只读属性,表示鼠标指针在点击元素(DOM)中的 X 坐标。
- MouseEvent.clientY 只读属性,表示鼠标指针在点击元素(DOM)中的 Y 坐标。
- MouseEvent.ctrlKey 只读属性,当鼠标事件触发时,如果 Control 键被按下,则返回 true。
- MouseEvent.metaKey 只读属性,当鼠标事件触发时,如果 Meta 键被按下,则返回 true。
- MouseEvent.shiftKey 只读属性,当鼠标事件触发时,如果 Shift 键被按下,则返回 true。
- MouseEvent.which 只读属性,当鼠标事件触发时,表示被按下的按钮。
同样地,我们可以使用代码来获取鼠标点击时触发的值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>键盘事件监听</title>
<style>
* {
margin: 0;
padding: 0;
}
#container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
padding-top: 20px;
}
table,
table tr th,
table tr td {
border: 1px solid #000;
}
table {
min-height: 25px;
line-height: 25px;
text-align: center;
border-collapse: collapse;
padding: 2px;
}
th {
width: 130px;
}
</style>
<script type="text/javascript" language=JavaScript>
function doNothing(){
window.event.returnValue=false;
return false;
}
window.onload = function () {
const button = document.getElementById('button');
const buttons = document.getElementById('buttons');
const clientX = document.getElementById('clientX');
const clientY = document.getElementById('clientY');
const ctrlKey = document.getElementById('ctrlKey');
const metaKey = document.getElementById('metaKey');
const shiftKey = document.getElementById('shiftKey');
const altKey = document.getElementById('altKey');
const which = document.getElementById('which');
const combineKey = document.getElementById('combineKey');
document.onmousedown = function(event) {
var e = event || window.event || arguments.callee.caller.arguments[0];
e.preventDefault(); //阻止默认事件
// 设置获取的值
button.innerHTML = e.button;
buttons.innerHTML = e.buttons;
clientX.innerHTML = e.clientX;
clientY.innerHTML = e.clientY;
ctrlKey.innerHTML = e.ctrlKey;
metaKey.innerHTML = e.metaKey;
shiftKey.innerHTML = e.shiftKey;
altKey.innerHTML = e.altKey;
which.innerHTML = e.which;
function getButton(value) {
let buttonname = '';
if (value === 0) buttonname = '鼠标左键';
if (value === 1) buttonname = '鼠标中间键';
if (value === 2) buttonname = '鼠标右键';
return buttonname;
}
if (e.altKey || e.shiftKey || e.metaKey || e.ctrlKey) {
let result = '';
if (e.altKey) {
result = 'Alt';
} else if (e.shiftKey) {
result = 'Shift';
} else if (e.metaKey) {
result = 'Meta';
} else if (e.ctrlKey) {
result = 'Control';
}
combineKey.innerHTML = `${result} + ${getButton(e.button)}`;
} else {
combineKey.innerHTML = getButton(e.button);
}
};
}
</script>
</head>
<body oncontextmenu="doNothing()">
<div id="container">
<table border="0px">
<tr>
<th>button</th>
<th>buttons</th>
<th>clientX</th>
<th>clientY</th>
<th>ctrlKey</th>
<th>metaKey</th>
<th>shiftKey</th>
<th>altKey</th>
<th>which</th>
<th>组合健</th>
</tr>
<tr>
<td id="button">-</td>
<td id="buttons">-</td>
<td id="clientX">-</td>
<td id="clientY">-</td>
<td id="ctrlKey">-</td>
<td id="metaKey">-</td>
<td id="shiftKey">-</td>
<td id="altKey">-</td>
<td id="which">-</td>
<td id="combineKey">-</td>
</tr>
</table>
<hr />
</div>
</body>
</html>
运行效果如下:

可参见以下链接进行操作:
See the Pen MouseEvent by suwu150 (@suwu150) on CodePen.
常见键值参考
常见键值
| 字母和数字键的键码值(keyCode) | |||||||
| 按键 | 键码 | 按键 | 键码 | 按键 | 键码 | 按键 | 键码 |
| A | 65 | J | 74 | S | 83 | 1 | 49 |
| B | 66 | K | 75 | T | 84 | 2 | 50 |
| C | 67 | L | 76 | U | 85 | 3 | 51 |
| D | 68 | M | 77 | V | 86 | 4 | 52 |
| E | 69 | N | 78 | W | 87 | 5 | 53 |
| F | 70 | O | 79 | X | 88 | 6 | 54 |
| G | 71 | P | 80 | Y | 89 | 7 | 55 |
| H | 72 | Q | 81 | Z | 90 | 8 | 56 |
| I | 73 | R | 82 | 0 | 48 | 9 | 57 |
| 数字键盘上的键的键码值(keyCode) | 功能键键码值(keyCode) | ||||||
| 按键 | 键码 | 按键 | 键码 | 按键 | 键码 | 按键 | 键码 |
| 0 | 96 | 8 | 104 | F1 | 112 | F7 | 118 |
| 1 | 97 | 9 | 105 | F2 | 113 | F8 | 119 |
| 2 | 98 | * | 106 | F3 | 114 | F9 | 120 |
| 3 | 99 | + | 107 | F4 | 115 | F10 | 121 |
| 4 | 100 | Enter | 108 | F5 | 116 | F11 | 122 |
| 5 | 101 | - | 109 | F6 | 117 | F12 | 123 |
| 6 | 102 | . | 110 | ||||
| 7 | 103 | / | 111 | ||||
| 控制键键码值(keyCode) | |||||||
| 按键 | 键码 | 按键 | 键码 | 按键 | 键码 | 按键 | 键码 |
| BackSpace | 8 | Esc | 27 | Right Arrow | 39 | -_ | 189 |
| Tab | 9 | Spacebar | 32 | Dw Arrow | 40 | .> | 190 |
| Clear | 12 | Page Up | 33 | Insert | 45 | /? | 191 |
| Enter | 13 | Page Down | 34 | Delete | 46 | `~ | 192 |
| Shift | 16 | End | 35 | Num Lock | 144 | [{ | 219 |
| Control | 17 | Home | 36 | ;: | 186 | /| | 220 |
| Alt | 18 | Left Arrow | 37 | =+ | 187 | ]} | 221 |
| Cape Lock | 20 | Up Arrow | 38 | ,< | 188 | '" | 222 |
| 多媒体键码值(keyCode) | |||||||
| 按键 | 键码 | 按键 | 键码 | 按键 | 键码 | 按键 | 键码 |
| 音量加 | 175 | ||||||
| 音量减 | 174 | ||||||
| 停止 | 179 | ||||||
| 静音 | 173 | ||||||
| 浏览器 | 172 | ||||||
| 邮件 | 180 | ||||||
| 搜索 | 170 | ||||||
| 收藏 | 171 | ||||||
参考链接:
1.https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent 2.https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/ctrlKey
到此这篇关于 JavaScript 如何实现监听键盘输入和鼠标监点击的文章就介绍到这了,更多相关 JavaScript 键盘鼠标监听功能内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!
JavaScript将图片转换为Base64编码的两种途径
2026-11-05 16:43:05
怎么在 Vue 或 React 项目中正确集成 animate.js
2026-11-05 10:23:38
JS注册事件:addEventListener基础语法与参数详解
2026-11-05 08:44:56
JSP和JS的区别:运行环境与服务端/客户端职责解析
2026-11-04 23:34:36
pdf.js 官方下载地址:GitHub Release 获取预编译包
2026-11-04 23:07:27
