| 事件 | 觸發時機 | keyCode/which | 可取得字元 | 適用場景 |
|---|---|---|---|---|
keydown | 按鍵**按下**瞬間 | 所有按鍵 (含功能鍵) | 否 | 遊戲控制、快捷鍵、導航 |
keypress | 字元**輸入**時 (按下並釋放) | 僅可列印字元 | 是 (String.fromCharCode) | 文字輸入驗證、即時搜尋 |
keyup | 按鍵**釋放**瞬間 | 所有按鍵 | 否 | 輸入完成後處理、自動完成 |
注意:
keypress已被棄用 (Deprecated),現代專案請改用keydown+event.key或beforeinput事件。
var KEY = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
// 字母鍵 65-90 (A-Z)
// 數字鍵 48-57 (0-9)
};
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).keydown(function(event) {
switch(event.keyCode) {
case 37: // Left
console.log('左鍵');
// to_left();
break;
case 39: // Right
console.log('右鍵');
// to_right();
break;
case 27: // ESC
console.log('ESC');
break;
case 13: // Enter
console.log('Enter');
break;
default:
console.log('其他鍵:', event.keyCode);
}
});
</script>
event.key (不依賴 keyCode)$(document).on('keydown', function(e) {
// event.key 回傳字元值 (如 "ArrowLeft", "Enter", "a", "1")
// 更語意化、不需記憶 keyCode
switch(e.key) {
case "ArrowLeft":
case "Left": // 相容舊瀏覽器
console.log("左"); break;
case "ArrowRight":
case "Right":
console.log("右"); break;
case "Escape":
case "Esc":
console.log("ESC"); break;
case "Enter":
console.log("Enter"); break;
default:
if (e.key.length === 1) {
console.log("字元鍵:", e.key);
}
}
// 組合鍵檢測
if (e.ctrlKey && e.key === 's') {
e.preventDefault(); // 阻止瀏覽器預設儲存
console.log("Ctrl+S 被攔截");
}
});
<style>
.panel { display: none; padding: 20px; border: 1px solid #ddd; margin: 10px 0; }
.panel.active { display: block; animation: slideIn 0.3s; }
@keyframes slideIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }
</style>
<div class="panel active" id="home">首頁內容</div>
<div class="panel" id="about">關於我們</div>
<div class="panel" id="contact">聯絡資訊</div>
<script>
var panels = ['#home', '#about', '#contact'];
var currentIndex = 0;
function showPanel(index) {
$('.panel').removeClass('active');
$(panels[index]).addClass('active');
currentIndex = index;
}
$(document).keydown(function(e) {
switch(e.key) {
case "ArrowLeft":
showPanel((currentIndex - 1 + panels.length) % panels.length);
break;
case "ArrowRight":
showPanel((currentIndex + 1) % panels.length);
break;
case "Home":
showPanel(0); break;
case "End":
showPanel(panels.length - 1); break;
}
});
// 滑鼠點擊也同步更新 currentIndex
$('.panel').click(function() {
currentIndex = panels.indexOf('#' + this.id);
});
$('#myForm').on('keydown', 'input, textarea', function(e) {
// Ctrl + Enter 提交
if (e.ctrlKey && e.key === 'Enter') {
e.preventDefault();
$(this).closest('form').submit();
}
// ESC 重置
if (e.key === 'Escape') {
this.form.reset();
}
});
var searchTimer;
$('#searchInput').on('input', function() { // 推薦用 input 事件
clearTimeout(searchTimer);
var query = this.value.trim();
searchTimer = setTimeout(function() {
if (query.length >= 2) {
$.get('/api/search', { q: query }, function(data) {
renderResults(data);
});
} else {
clearResults();
}
}, 300); // 300ms 防抖
});
aria-keyshortcuts 標註自訂快捷鍵event.key 替代 keyCode,語意清晰且跨平台一致input 事件 (涵蓋輸入法、貼上、拖曳等所有輸入方式)