JS 代码
<html> <head> <script type = "text/javascript"> window.onload = function () {hotkeyhandler.init (); } var hotkeyhandler = {currentMainkey: null, currentValueKey: null, init: function () {hotkeyhandler.register (0, "k", ฟังก์ชัน () {alert ("注册成功");}); }, register: function (tag, value, func) {var mainkey = ""; สวิตช์ (แท็ก) {กรณี 0: MainKey = 17; // ctrl break; กรณีที่ 1: MainKey = 16; // shift break; กรณีที่ 2: MainKey = "18"; // alt break; } document.onkeyup = function (e) {hotkeyhandler.currentMainkey = null; } document.onkeydown = ฟังก์ชั่น (เหตุการณ์) {// 获取键值 var keycode = event.keycode; var keyValue = string.fromCharcode (event.keycode); if (hotKeyHandler.CurrentMainkey! = null) {ถ้า (keyValue == ค่า) {hotKeyHandler.CurrentMainkey = null; ถ้า (func! = null) func (); }} if (keycode == mainkey) hotkeyhandler.currentMainkey = keycode; }}} </script> </head> <body> 测试, 按下 ctrl+k 你就会发现神奇的事情发生了 </body> </html>