{"id":9755,"date":"2021-01-04T23:29:34","date_gmt":"2021-01-04T15:29:34","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9755"},"modified":"2023-02-08T11:24:26","modified_gmt":"2023-02-08T03:24:26","slug":"js-keycode-deprecated","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/01\/js-keycode-deprecated\/","title":{"rendered":"\u544a\u522bJS keyCode"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9755\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9755<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/keyboard-cover.png\" width=\"480\" height=\"240\" alt=\"\u952e\u76d8\u7d20\u6750\u5360\u4f4d\u56fe\u5c01\u9762\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202101-keyboard.png';<\/script><\/p>\n<h3>\u4e00\u3001\u4e3a\u4ec0\u4e48keyCode\u4e0d\u63a8\u8350\u4f7f\u7528\u4e86\uff1f<\/h3>\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\uff0c\u8bf4\u5b9e\u8bdd\uff0c\u6211\u627e\u4e86\u5f88\u591a\u8d44\u6599\uff0c\u5e76\u6ca1\u6709\u7279\u522b\u660e\u786e\u7684\u56de\u7b54\u3002<\/p>\n<p>\u65e9\u4e9b\u5e74\uff0c\u6211\u5f97\u5230\u7684\u8bf4\u6cd5\u662f\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u81ea\u5b9a\u4e49\u952e\u76d8\uff0c\u5bfc\u81f4keyCode\u4e0d\u51c6\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u8fd9\u6b21\u641c\u5bfb\u8d44\u6599\uff0c\u6ca1\u6709\u89c1\u5230\u7c7b\u4f3c\u7684\u8bf4\u6cd5\u3002<\/p>\n<p>MDN\u4e0a\u7684\u89e3\u91ca\u662f\u5bf9\u6253\u5370\u5b57\u7b26\u4e0d\u53cb\u597d\u3002<\/p>\n<p>\u6211\u7422\u78e8\u7740\u53ef\u80fd\u662f\u8fd9\u51e0\u4e2a\u610f\u601d\uff1a<\/p>\n<h4>1. \u4e0d\u540c\u5b57\u7b26\u5171\u7528keyCode<\/h4>\n<p>\u952e\u76d8\u4e0a\u6709\u5f88\u591a\u6309\u952e\u662f\u540c\u65f6\u5bf9\u5e94\u4e24\u4e2a\u5b57\u7b26\u7684\uff0c\u4f8b\u5982\u201c<,\u201d\u548c\u201c>.\u201d\u5c31\u5728\u4e00\u4e2a\u952e\u4e0a\uff0c\u6570\u5b57\u952e\u90a3\u91cc\uff081!\u30012@\u30013#\u30014$\u30015%\u3001\u2026\u2026\uff09\u90fd\u662f\u4e00\u4e2a\u952e\u5bf9\u5e94\u591a\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u60f3\u8981\u8f93\u5165\u4e24\u4f4d\u5b57\u7b26\uff0c\u5f80\u5f80\u9700\u8981\u6309\u4f4fShift\u952e\u624d\u884c\uff0c\u4f8b\u5982\u5b57\u7b26\u201c<>!@#$%^&#038;*()\u2026\u2026\u201d\u7b49\u5b57\u7b26\u7684\u8f93\u5165\u3002<\/p>\n<p>\u800c<code>keyCode<\/code>\u503c\u662f\u8ddf\u7740\u952e\u76d8\u8d70\u7684\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\uff0c\u5f53\u6211\u4eec\u8f93\u5165\u201c<,\u201d\u548c\u201c>.\u201d\u7b49\u5b57\u7b26\u7684\u65f6\u5019\uff0c\u8fd4\u56de\u7684<code>keyCode<\/code>\u503c\u662f\u4e00\u6837\u7684\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u5224\u65ad\u7528\u6237\u662f\u5426\u6309\u4e0b\u4e86Shift\u952e\u624d\u77e5\u9053\u7a76\u7adf\u8f93\u5165\u7684\u662f\u54ea\u4e2a\u5b57\u7b26\u3002<\/p>\n<p>\u8fd9\u5c31\u5f88\u5570\u55e6\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>window.addEventListener('keydown', function (event) {\r\n  console.log(event.keyCode);\r\n});<\/pre>\n<p>\u7136\u540e\u5728\u9875\u9762\u4e2d\u5206\u522b\u8f93\u5165\u5b57\u7b26\u201c.>\u201d\uff0c\u5219\u4f1a\u770b\u5230\u8f93\u51fa\u7684keyCode\u503c\u90fd\u662f\u4e00\u6837\u7684<code>190<\/code>\uff0c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202012\/2020-12-28_231714.png\" width=\"478\" height=\"194\" alt=\"keyCode\u90fd\u662f190\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u660e\u660e\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\uff0c\u7ed3\u679c\u8f93\u51fa\u7684keyCode\u503c\u662f\u4e00\u6837\u7684\uff0c\u5bf9\u5f00\u53d1\u5c31\u4e0d\u53cb\u597d\u3002<\/p>\n<h4>2. \u76f8\u540c\u6309\u952e\u4e0d\u540ckeyCode<\/h4>\n<p>\u4f8b\u5982\u5168\u952e\u76d8\u4e2d\u7684\u6570\u5b57\u952e\u6309\u4f4fShift\u952e\u53ef\u4ee5\u8f93\u51fa\u5176\u4ed6\u5185\u5bb9\uff0c\u4f8b\u5982\u4e0b\u56fe\u6240\u793a\u7684\u6570\u5b57\u952e\u76d8\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-03_222856.jpg\" width=\"240\" height=\"257\" alt=\"\u6570\u5b57\u952e\u76d8\" class=\"alignnone size-medium\" \/><br \/>\n\uff0c<br \/>\n\u4f8b\u5982\u53f3\u4e0b\u89d2\u7684\u5c0f\u6570\u70b9\u5b57\u7b26<code>'.'<\/code>\u540c\u65f6\u6709Del\u952e\u7684\u80fd\u529b\u3002<\/p>\n<p>\u6b64\u65f6\uff0c\u6309\u4e0b\u6b64\u952e\u548cShift+\u6309\u4e0b\u6b64\u952e\u7684keyCode\u503c\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-03_223451.png\" width=\"492\" height=\"190\" alt=\"\u540c\u4e00\u6309\u952e\u4e0d\u540ckeyCode\u503c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u540e\u5927\u5bb6\u5c31\u4f1a\u53d1\u73b0\u6309\u4e0b<code>'.'<\/code>\u8fd4\u56de\u7684keyCode\u662f110\uff0c\u52a0\u4e86\u4e2aShift\u952e\u4e4b\u540e\u8fd4\u56de\u7684\u5c31\u53d8\u6210\u4e8646\u4e86\u3002<\/p>\n<p>\u8fd9\u5c31\u86cb\u75bc\u4e86\uff0c\u56e0\u4e3a\u4e0a\u9762\u540c\u6837\u6309\u952e\u8fd4\u56de\u540c\u6837keyCode\uff0c\u8fd9\u91cc\u5c45\u7136\u6447\u8eab\u4e00\u53d8\uff0c\u76f8\u540c\u6309\u952e\u8fd4\u56de\u4e86\u4e0d\u540c\u7684keyCode\u3002<\/p>\n<h4>3. \u76f8\u540c\u5b57\u7b26\u4e0d\u540ckeyCode<\/h4>\n<p>\u8fd8\u6ca1\u5b8c\uff0cShift+<code>'.'<\/code>\u8fd4\u56dekeyCode\u5c31\u662f\u76f4\u63a5\u6309\u4e0bdelete\u952e\u7684keyCode\u503c\uff0c\u7ed9\u4eba\u611f\u89c9\u53ea\u8981\u6700\u7ec8\u884c\u4e3a\u6216\u5b57\u7b26\u8f93\u51fa\u662f\u4e00\u6837\u7684\uff0ckeyCode\u503c\u4e5f\u5e94\u8be5\u662f\u4e00\u6837\u7684\uff0c\u5b9e\u9645\u4e0a\uff0c\u5e76\u4e0d\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<p>\u5373\u4f7f\u8f93\u5165\u540c\u4e00\u5b57\u7b26\uff0c\u4e5f\u53ef\u80fd\u4f1a\u662f\u4e0d\u540c\u7684keyCode\u503c\u3002<\/p>\n<p>\u8fd9\u91cc\u5927\u5bb6\u6ce8\u610f\u529b\u653e\u5728\u4e0b\u56fe\u4e2d\u53f3\u4e0a\u65b9\u7684\u52a0\u53f7\u548c\u51cf\u53f7\u4e0a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-03_223451.png\" width=\"492\" height=\"190\" alt=\"\u540c\u4e00\u6309\u952e\u4e0d\u540ckeyCode\u503c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u540c\u65f6\uff0c\u4e0a\u65b9\u7684\u4e00\u6392\u6570\u5b57\u952e\u90a3\u91cc\u4e5f\u662f\u6709\u52a0\u53f7\u548c\u51cf\u53f7\u7684\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-03_224306.jpg\" width=\"240\" height=\"149\" alt=\"\u52a0\u53f7\u548c\u51cf\u53f7code\u503c\u5bf9\u6bd4\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65f6\uff0c\u5206\u522b\u6309\u4e0b\u8fd9\u4e24\u4e2a\u952e\uff0c\u4f1a\u53d1\u73b0\u8f93\u51fa\u7684\u5b57\u7b26\u662f\u4e00\u6a21\u4e00\u6837\u7684\uff0c\u4f46\u662fkeyCode\u503c\u5374\u662f\u4e0d\u540c\u7684\uff0c\u5982\u4e0b\u622a\u56fe\u793a\u610f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-03_224723.png\" width=\"457\" height=\"169\" alt=\"\u77ed\u6a2a\u7ebf\u548c\u51cf\u53f7\u4e0d\u540c\u5b57\u7b26\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4e0a\u65b9\u6570\u5b57\u952e\u90a3\u91cc\u7684\u77ed\u6a2a\u7ebf\u8fde\u5b57\u7b26\u7684keyCode\u662f189\uff0c\u800c\u6570\u5b57\u952e\u76d8\u90a3\u91cc\u7684\u51cf\u53f7\u5b57\u7b26\u7684keyCode\u662f109\uff0c\u5b9e\u9645\u4e0a\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e00\u6a21\u4e00\u6837\u3002<\/p>\n<p>\u8bd5\u60f3\u4e0b\uff0c\u5982\u679c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5224\u65ad\u7528\u6237\u662f\u5426\u6309\u4e0b\u7684\u662f\u8fde\u5b57\u7b26\uff0c\u4f7f\u7528keyCode\u5224\u65ad\uff0c\u662f\u4e0d\u662f\u5f88\u5bb9\u6613\u51fabug\uff1f<\/p>\n<p>\u5176\u4ed6\u7684\u51e0\u4e2a\u6570\u5b57\u53ef\u6709\u7c7b\u4f3c\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u6570\u5b57\u952e\u76d8\u8f93\u51fa\u76841-9\u7684keyCode\u548c\u952e\u76d8\u4e0a\u97621-9\u6570\u5b57\u7684keyCode\u503c\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n<h4>4. \u4e2d\u6587\u8f93\u5165\u6cd5\u4e0b\u6807\u70b9\u7b26\u53f7keyCode\u90fd\u662f\u4e00\u6837\u7684<\/h4>\n<p>\u4f8b\u5982\u8f93\u5165\u6846\u4e2d\u662f\u4e2d\u6587\u8f93\u5165\u6cd5\uff0c\u6b64\u65f6\uff0c\u201c\uff0c\u3002\uff1b\u2018\u3010\u3011-=\u201d\u8fd9\u4e9b\u5b57\u7b26\u7684keyCode\u5168\u90e8\u90fd\u8fd4\u56de229\uff0c\u6839\u672c\u5c31\u6ca1\u6cd5\u7ee7\u7eed\u73a9\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-03_231633.png\" width=\"476\" height=\"306\" alt=\"\u4e2d\u6587\u8f93\u5165\u6cd5\u5747\u8fd4\u56dekeyCode 229\" class=\"alignnone size-medium\" \/><\/p>\n<p><strong>\u8865\u5145\u4e8e2023-02-08<\/strong><\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\uff0c\u4e2d\u6587\u8f93\u5165\u6cd5\u5f00\u542f\u65f6\u5019keyCode\u503c\u662f229\u8fd8\u662f\u6709\u7528\u7684\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u5e0c\u671b\u76d1\u542c\u8f93\u5165\u6846\u662f\u5426\u6309\u4e0bEnter\u952e\u89e6\u53d1submit\u884c\u4e3a\u7684\u65f6\u5019\uff0c\u4f7f\u7528<code>event.keyCode<\/code> \u53ef\u4ee5\u907f\u514d\u8f93\u5165\u6cd5\u56de\u8f66\uff08\u4f7f\u7528\u82f1\u6587\uff09\u4e0d\u5c0f\u5fc3\u89e6\u53d1submit\u4e8b\u4ef6\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528event.code\u6216event.key\uff0c\u65e0\u8bba\u8f93\u5165\u6cd5\u662f\u5426\u5f00\u542f\uff0c\u8fd4\u56de\u7684\u90fd\u662f &#8216;Enter&#8217;\uff0c\u5c31\u4f1a\u5bfc\u81f4\u4e2d\u6587\u8f93\u5165\u573a\u666f\u4e0b\uff0c\u5185\u5bb9\u8fd8\u6ca1\u5199\u5165\u5c31\u88ab\u63d0\u4ea4\u7684\u60c5\u51b5\u51fa\u73b0\u3002<\/p>\n<hr>\n<p>\u6216\u8bb8\u662f\u56e0\u4e3a\u4e0a\u9762\u6307\u51fa\u76844\u4e2akeyCode\u5bf9\u6253\u5370\u5b57\u7b26\u4e0d\u53cb\u597d\u7684\u5730\u65b9\uff0ckeyCode\u624d\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u76ee\u524d\u89c4\u8303\u63a8\u8350\u4f7f\u7528event.code\u6216event.key\u3002<\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9755\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u4e8c\u3001event.code\u548cevent.key\u7684\u533a\u522b<\/h3>\n<p><code>event.code<\/code>\u6307\u660e\u6309\u4e0b\u7684\u662f\u5177\u4f53\u54ea\u4e2a\u7269\u7406\u952e\uff0c\u952e\u76d8\u4e0a\u6bcf\u4e00\u4e2a\u6309\u952e\u90fd\u5bf9\u5e94\u4e00\u4e2a\u552f\u4e00\u7684<code>event.code<\/code>\u503c\uff0c\u5747\u4f7f\u7528\u5927\u5199\u82f1\u6587\u5355\u8bcd\u8868\u793a\u3002<\/p>\n<p><code>event.key<\/code>\u6307\u660e\u5177\u4f53\u8f93\u5165\u7684\u5b57\u7b26\u5185\u5bb9\uff0c\u5982\u679c\u662f\u975e\u6253\u5370\u5b57\u7b26\uff08\u4f8b\u5982Enter\u952e\u3001Esc\u952e\u3001Shift\u952e\u3001Alt\u952e\u7b49\uff09\uff0c\u5219\u8fd4\u56de\u5177\u4f53\u7684\u975e\u6253\u5370\u5b57\u7b26\u7684\u82f1\u6587\u540d\u79f0\uff0c\u5982\u679c\u8f93\u5165\u5185\u5bb9\u4e0e\u8f93\u5165\u6cd5\u6709\u5173\u5219\u8fd4\u56de\u56fa\u5b9a\u7684Process\u540d\u79f0\u3002<\/p>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u5927\u5bb6\u5feb\u901f\u4e86\u89e3\u5dee\u5f02\uff0c\u6211\u9009\u53d6\u4e86\u51e0\u4e2a\u5177\u6709\u4ee3\u8868\u6027\u7684\u6309\u952e\uff0c\u6574\u7406\u4e86\u4e2a\u8868\u683c\uff0c\u663e\u793a\u4e86\u4e0d\u540c\u6309\u952e\u4e0b<code>event.code<\/code>\u548c<code>event.key<\/code>\u7684\u503c\u3002<\/p>\n<table border=\"0\" width=\"100%\" class=\"params_table\" cellspacing=\"1\">\n<thead>\n<tr>\n<th>keyCode\u503c<\/th>\n<th>code\u503c<\/th>\n<th>key\u503c<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>49<\/td>\n<td>&#8216;Digit1&#8217;<\/td>\n<td>&#8216;1&#8217;<\/td>\n<td>\u4e0a\u65b9\u6570\u5b57\u952e1\u6309\u4e0b<\/td>\n<\/tr>\n<tr>\n<td>97<\/td>\n<td>&#8216;Numpad1&#8217;<\/td>\n<td>&#8216;1&#8217;<\/td>\n<td>\u5c0f\u952e\u76d8\u6570\u5b57\u952e1\u6309\u4e0b<\/td>\n<\/tr>\n<tr>\n<td>16<\/td>\n<td>&#8216;ShiftLeft&#8217;<\/td>\n<td>&#8216;Shift&#8217;<\/td>\n<td>\u5de6\u4fa7\u7684Shift\u952e<\/td>\n<\/tr>\n<tr>\n<td>16<\/td>\n<td>&#8216;ShiftRight&#8217;<\/td>\n<td>&#8216;Shift&#8217;<\/td>\n<td>\u53f3\u4fa7\u7684Shift\u952e<\/td>\n<\/tr>\n<tr>\n<td>190<\/td>\n<td>&#8216;Period&#8217;<\/td>\n<td>&#8216;.&#8217;<\/td>\n<td>\u4e3b\u952e\u76d8\u4e2d\u7684\u70b9\u7b26\u53f7<\/td>\n<\/tr>\n<tr>\n<td>110<\/td>\n<td>&#8216;NumpadDecimal&#8217;<\/td>\n<td>&#8216;.&#8217;<\/td>\n<td>\u6570\u5b57\u952e\u76d8\u4e2d\u7684\u5c0f\u6570\u70b9\u7b26\u53f7<\/td>\n<\/tr>\n<tr>\n<td>229<\/td>\n<td>&#8216;Period&#8217;<\/td>\n<td>&#8216;Process&#8217;<\/td>\n<td>\u4e2d\u6587\u8f93\u5165\u6cd5\u4e0b\u4e3b\u952e\u76d8\u4e2d\u7684\u70b9\u7b26\u53f7<\/td>\n<\/tr>\n<tr>\n<td>229<\/td>\n<td>&#8216;Minus&#8217;<\/td>\n<td>&#8216;Process&#8217;<\/td>\n<td>\u4e2d\u6587\u8f93\u5165\u6cd5\u4e0b\u4e3b\u952e\u76d8\u4e2d\u7684&#8217;-&#8216;\u7b26\u53f7<\/td>\n<\/tr>\n<tr>\n<td>189<\/td>\n<td>&#8216;Minus&#8217;<\/td>\n<td>&#8216;-&#8216;<\/td>\n<td>\u4e3b\u952e\u76d8\u4e2d\u7684&#8217;-&#8216;\u7b26\u53f7<\/td>\n<\/tr>\n<tr>\n<td>109<\/td>\n<td>&#8216;NumpadSubtract&#8217;<\/td>\n<td>&#8216;-&#8216;<\/td>\n<td>\u6570\u5b57\u952e\u76d8\u4e2d\u7684&#8217;-&#8216;\u7b26\u53f7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5bf9\u4e8e\u82f1\u6587\u573a\u666f\uff0c\u53ea\u9700\u8981\u4f7f\u7528<code>event.key<\/code>\u5c31\u53ef\u4ee5\u77e5\u9053\u952e\u76d8\u8f93\u5165\u7684\u5185\u5bb9\u4e86\u3002<\/p>\n<p>\u4f46\u662f\u5982\u679c\u662f\u4e2d\u6587\u573a\u666f\uff0c\u60c5\u51b5\u5c31\u53d8\u5f97\u590d\u6742\u7684\u591a\u3002<\/p>\n<p>\u5728\u4e2d\u6587\u8f93\u5165\u6846\u5f00\u542f\u7684\u573a\u666f\u4e0b\uff0c\u5982\u679c\u6309\u952e\u7684\u5185\u5bb9\u548c\u975e\u4e2d\u6587\u8f93\u5165\u6cd5\u4e0b\u7684\u5185\u5bb9\u4e0d\u4e00\u6837\uff0c\u5219<code>event.key<\/code>\u7684\u8fd4\u56de\u503c\u662f\u56fa\u5b9a\u7684Process\uff0c\u8868\u793a\u8f93\u5165\u7684\u5b57\u7b26\u5185\u5bb9\u548c\u952e\u76d8\u5bf9\u5e94\u7684\u539f\u59cb\u5185\u5bb9\u8fdb\u884c\u4e86\u5904\u7406\u3002<\/p>\n<p>\u4f8b\u5982\u4e3b\u952e\u76d8\u4e2d\u7684\u5b57\u7b26\u70b9\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5c31\u662f\u4e2a<code>.<\/code>\uff0c\u4f46\u662f\u5982\u679c\u662f\u4e2d\u6587\u8f93\u5165\u6846\uff0c\u5b57\u7b26\u70b9\u5c31\u662f\u53e5\u53f7<code>\u3002<\/code>\uff0c\u800c\u952e\u76d8\u4e0a\u5b8c\u5168\u5c31\u6ca1\u6709\u53e5\u53f7\u8fd9\u4e2a\u5b57\u7b26\uff0c\u8bf4\u660e\u5b57\u7b26\u70b9\u88ab\u8f93\u5165\u6cd5\u7ed9\u5904\u7406\u4e86\uff0c\u56e0\u6b64\u8fd4\u56de\u7684\u5c31\u662fProcess\u3002<\/p>\n<p>\u8fd9\u5c31\u5bfc\u81f4\uff0c\u5728\u4e2d\u6587\u8f93\u5165\u6cd5\u573a\u666f\u4e0b\uff0c\u7528\u6237\u6216\u8005\u5f00\u53d1\u8005\u662f\u65e0\u6cd5\u77e5\u9053\u6309\u952e\u5e94\u8be5\u8f93\u5165\u7684\u5185\u5bb9\u7684\u3002<\/p>\n<p>\u5373\u4f7f\u914d\u5408<code>event.code<\/code>\u4e5f\u4e0d\u884c\u3002<\/p>\n<p>\u6709\u4eba\u53ef\u80fd\u4f1a\u53cd\u9a73\uff0c<code>event.code<\/code>\u8fd4\u56de&#8217;Period&#8217;\uff0c\u4e0d\u5c31\u8868\u660e\u6309\u4e0b\u7684\u662f\u70b9\u53f7\u952e\uff0c\u6b64\u65f6<code>event.key<\/code>\u662f&#8217;Process&#8217;\uff0c\u4e0d\u5c31\u53ef\u4ee5\u5224\u65ad\u8f93\u5165\u7684\u662f\u53e5\u53f7\u4e86\u3002<\/p>\n<p>\u7406\u8bba\u4e0a\u53ef\u884c\uff0c\u5b9e\u64cd\u8d77\u6765\u5374\u6bd4\u9884\u60f3\u7684\u9ebb\u70e6\uff0c\u4e14\u4e0d\u5177\u6709\u53ef\u590d\u5236\u6027\u3002<\/p>\n<ul>\n<li>\u9ebb\u70e6\u5728\u4e8e\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u8fd9\u4e2a\u7b26\u53f7<code>\u300b<\/code>\uff0c\u4e5f\u5c31\u662f\u53f3\u4e66\u540d\u53f7\uff0c<code>event.code<\/code>\u8fd4\u56de\u7684\u4e5f\u662f&#8217;Period&#8217;\uff0c<code>event.key<\/code>\u4e5f\u662f&#8217;Process&#8217;\u3002\u8fd8\u9700\u8981\u5f00\u53d1\u8005\u5224\u65ad<code>event.shiftKey<\/code>\u662f\u5426\u4e3atrue\uff0c\u8868\u793aShift\u952e\u662f\u5426\u6309\u4e0b\u3002\n<p>\u4ee5\u53ca\uff0c\u6362\u4e2a\u5176\u4ed6\u8bed\u8a00\u8f93\u5165\u6cd5\uff0c\u5219\u6309\u4e0b&#8217;Period&#8217;\u952e\u8fd4\u56de\u7684\u5c31\u4e0d\u4e00\u5b9a\u662f\u53e5\u53f7\u4e86\u3002<\/li>\n<li>\u4e0d\u5177\u6709\u53ef\u590d\u5236\u6027\u5728\u4e8e\uff0c\u4e2d\u6587\u8f93\u5165\u6cd5\u7ecf\u5e38\u4f1a\u4f7f\u7528\u7a7a\u683c\u6216\u8005\u56de\u8f66\u8868\u793a\u9009\u4e2d\uff0c\u6b64\u65f6\uff0c<code>event.code<\/code>\u662fSpace\u6216\u8005Enter\uff0c<code>event.key<\/code>\u7684\u503c\u662fProcess\uff0c\u6839\u672c\u65e0\u6cd5\u5224\u65ad\u5230\u5e95\u8f93\u5165\u7684\u662f\u4ec0\u4e48\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5\uff0c<code>event.code<\/code>\u548c<code>event.key<\/code>\u8fd9\u4e24\u4e2a\u4e0d\u9002\u5408\u4e2d\u6587\u8f93\u5165\u6cd5\u4e0b\u7684\u8f93\u5165\u5224\u65ad\u3002<\/p>\n<p>\u597d\u5728\uff0c\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5f88\u5c11\u6709\u573a\u666f\u9700\u8981\u63d0\u524d\u77e5\u9053\u7528\u6237\u662f\u5426\u5f00\u542f\u4e86\u4e2d\u6587\u8f93\u5165\u6cd5\u3002<\/p>\n<p>\u66f4\u591a\u7684\u662f\u4e00\u4e9b\u529f\u80fd\u952e\u7684\u5224\u65ad\u3002<\/p>\n<p>\u4f8b\u5982\u7a7a\u683c\u3001\u56de\u8f66\u3001\u5220\u9664\u3001\u4e0a\u4e0b\u5de6\u53f3\u952e\u3001\u4e0a\u4e00\u9875\u4e0b\u4e00\u9875\u952e\uff0chome\/end\u952e\uff0cESC\u952e\u7b49\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u63a5\u4e0b\u6765\u7ed9\u5927\u5bb6\u7f57\u5217\u4ee5\u4e0b\u5e38\u89c1\u7684\u529f\u80fd\u952e\u5bf9\u5e94\u7684<code>event.key<\/code>\u548c<code>event.code<\/code>\u503c\u3002<\/p>\n<h3>\u4e09\u3001\u5e38\u89c1\u529f\u80fd\u952ekey\u503c<\/h3>\n<p>\u529f\u80fd\u952ekey\u503c\u66f4\u5b9e\u7528\uff0c\u56e0\u6b64\u653e\u5728\u524d\u9762\u5c55\u793a\u3002<\/p>\n<p>\u8be6\u89c1\u4e0b\u8868\uff1a<\/p>\n<table border=\"0\" width=\"300\" class=\"params_table\" cellspacing=\"1\">\n<thead>\n<tr>\n<th>\u6309\u952e\u540d\u79f0<\/th>\n<th>event.key<\/th>\n<th>keyCode\u503c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u56de\u8f66<\/td>\n<td>Enter<\/td>\n<td>13<\/td>\n<\/tr>\n<tr>\n<td>delete\u5220\u9664<\/td>\n<td>Delete<\/td>\n<td>46<\/td>\n<\/tr>\n<tr>\n<td>backspace\u9000\u683c<\/td>\n<td>Backspace<\/td>\n<td>8<\/td>\n<\/tr>\n<tr>\n<td>esc\u53d6\u6d88<\/td>\n<td>Escape<\/td>\n<td>27<\/td>\n<\/tr>\n<tr>\n<td>tab\u7d22\u5f15<\/td>\n<td>Tab<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>\u4e0a<\/td>\n<td>ArrowUp<\/td>\n<td>38<\/td>\n<\/tr>\n<tr>\n<td>\u4e0b<\/td>\n<td>ArrowDown<\/td>\n<td>40<\/td>\n<\/tr>\n<tr>\n<td>\u5de6<\/td>\n<td>ArrowLeft<\/td>\n<td>37<\/td>\n<\/tr>\n<tr>\n<td>\u53f3<\/td>\n<td>ArrowRight<\/td>\n<td>39<\/td>\n<\/tr>\n<tr>\n<td>pageDown\u4e0b\u4e00\u9875<\/td>\n<td>PageDown<\/td>\n<td>34<\/td>\n<\/tr>\n<tr>\n<td>pageUp\u4e0a\u4e00\u9875<\/td>\n<td>PageUp<\/td>\n<td>33<\/td>\n<\/tr>\n<tr>\n<td>home\u952e<\/td>\n<td>Home<\/td>\n<td>36<\/td>\n<\/tr>\n<tr>\n<td>end\u952e<\/td>\n<td>End<\/td>\n<td>35<\/td>\n<\/tr>\n<tr>\n<td>shift\u952e<\/td>\n<td>Shift<\/td>\n<td>16<\/td>\n<\/tr>\n<tr>\n<td>control\u952e<\/td>\n<td>Control<\/td>\n<td>17<\/td>\n<\/tr>\n<tr>\n<td>alt\u952e<\/td>\n<td>Alt<\/td>\n<td>18<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>KeyboardEvent.key\u503c\u517c\u5bb9\u6027\u8981\u6bd4KeyboardEvent.code\u503c\u597d\u4e00\u4e9b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-04_225556.png\" width=\"568\" height=\"386\" alt=\"KeyboardEvent.key\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>IE\u6d4f\u89c8\u5668\u52c9\u5f3a\u652f\u6301\uff0c\u8fd4\u56de\u7684\u503c\u53ef\u80fd\u548c\u89c4\u8303\u4e2d\u5b9a\u4e49\u7684\u6709\u51fa\u5165\u3002\u5982\u679c\u8981\u517c\u5bb9IE\u6d4f\u89c8\u5668\uff0c\u517c\u5bb9\u53ef\u4ee5\u7528keyCode\u5c5e\u6027\u6491\u4e00\u4f1a\u513f\u3002<\/p>\n<h3>\u56db\u3001\u5e38\u89c1\u529f\u80fd\u952e\u7684code\u503c<\/h3>\n<p>\u5982\u4e0b\u8868\u6240\u793a\uff0c\u5927\u90e8\u5206\u8fd4\u56de\u503c\u548ckey\u503c\u662f\u4e00\u6837\u7684\uff0c\u56e0\u4e3a\u90fd\u662f\u529f\u80fd\u952e\uff0c\u5982\u679c\u662f\u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5219code\u503c\u548ckey\u503c\u90a3\u5c31\u5b8c\u5168\u662f\u4e24\u7801\u4e8b\u4e86\uff1a<\/p>\n<table border=\"0\" width=\"300\" class=\"params_table\" cellspacing=\"1\">\n<thead>\n<tr>\n<th>\u6309\u952e\u540d\u79f0<\/th>\n<th>event.code<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u56de\u8f66<\/td>\n<td>Enter<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>delete\u5220\u9664<\/td>\n<td>Delete<\/td>\n<td>Shift+NumpadDecimal\u4e5f\u53ef\u80fd\u662f\u5220\u9664<\/td>\n<\/tr>\n<tr>\n<td>backspace\u9000\u683c<\/td>\n<td>Backspace<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>esc\u53d6\u6d88<\/td>\n<td>Escape<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>tab\u7d22\u5f15<\/td>\n<td>Tab<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>\u4e0a<\/td>\n<td>ArrowUp<\/td>\n<td>38<\/td>\n<\/tr>\n<tr>\n<td>\u4e0b<\/td>\n<td>ArrowDown<\/td>\n<td>40<\/td>\n<\/tr>\n<tr>\n<td>\u5de6<\/td>\n<td>ArrowLeft<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>\u53f3<\/td>\n<td>ArrowRight<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>pageDown\u4e0b\u4e00\u9875<\/td>\n<td>PageDown<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>pageUp\u4e0a\u4e00\u9875<\/td>\n<td>PageUp<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>home\u952e<\/td>\n<td>Home<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>end\u952e<\/td>\n<td>End<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>shift\u952e<\/td>\n<td>ShiftLeft\/ShiftRight<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>control\u952e<\/td>\n<td>ControlLeft\/ControlRight<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>alt\u952e<\/td>\n<td>AltLeft\/AltRight<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u517c\u5bb9\u6027\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-04_225648.png\" width=\"563\" height=\"343\" alt=\"KeyboardEvent.code\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e94\u3001\u9001\u4f60\u4e00\u6735\u5c0f\u7ea2\u82b1<\/h3>\n<p>\u9664\u4e86event.keyCode\u4e0d\u63a8\u8350\u4f7f\u7528\uff0cevent.which\u4e5f\u4e0d\u63a8\u8350\u4f7f\u7528\u4e86\uff0c\u5b98\u65b9\u540d\u79f0\u4e3aKeyboardEvent.which\u3002<\/p>\n<p>\u867d\u7136\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u4f46\u662f\u6309\u7167\u6211\u7684\u7406\u89e3\uff0c99%\u7684\u6982\u7387\u6d4f\u89c8\u5668\u8fd8\u4f1a\u4e00\u76f4\u4fdd\u6301\u652f\u6301\u7684\uff0c\u56e0\u4e3a\u8981\u662f\u53bb\u6389\u8fd9\u51e0\u4e2aAPI\u7279\u6027\uff0c\u8fd9\u4e16\u754c\u4e0a\u81f3\u5c11\u51e0\u767e\u4e07\u4e2a\u7f51\u7ad9\u5f00\u53d1\u8005\u4f1a\u70b8\u5f00\u9505\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u6761\u4ef6\u5141\u8bb8\uff08\u4e0d\u7528\u8003\u8651IE\u6d4f\u89c8\u5668\uff09\uff0c\u6211\u4eec\u8fd8\u662f\u4f18\u5148\u4f7f\u7528<code>event.key<\/code>\u6216\u8005<code>event.code<\/code>\u6765\u8bc6\u522b\u6309\u952e\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u76f8\u6bd4\u539f\u6765\u7684<code>event.keyCode<\/code>\u6216\u8005<code>eevent.which<\/code>\uff0c<code>event.key<\/code>\u548c<code>event.code<\/code>\u8981\u66f4\u597d\u4e0a\u624b\uff0c\u4f8b\u5982\u4e0a\u4e0b\u5de6\u53f3\u952e\u7684\u51e0\u4e2a\u6570\u5b57\uff0c\u6211\u8001\u662f\u8bb0\u4e0d\u4f4f\uff0c\u603b\u6709run\u4e00\u4e0b\u770b\u770b\u503c\u662f\u591a\u5c11\uff0c\u4f46\u662f\u5982\u679c\u662f<code>event.key<\/code>\uff0c\u5219\u76f4\u63a5\u4f7f\u7528\u8bed\u4e49\u5316\u7684\u82f1\u6587\u5355\u8bcd\u5373\u53ef\u3002<\/p>\n<p>\u53ea\u9700\u8981\u8bb0\u4f4f\u5355\u8bcd\u89c4\u5219\uff0c\u9996\u5b57\u6bcd\u5927\u5c0f\uff0c\u6bcf\u4e2a\u5206\u8bcd\u9996\u5b57\u6bcd\u5927\u5199\u5c31OK\u4e86\u3002<\/p>\n<p>OK\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u5168\u90e8\u5185\u5bb9\uff0c\u52a0\u6df1\u4e86\u4e0b\u6211\u81ea\u5df1\u5bf9\u952e\u76d8\u4e8b\u4ef6\u7684\u4e00\u4e9b\u4e86\u89e3\uff0c\u6574\u7406\u4e86\u51e0\u4e2a\u5e38\u7528\u529f\u80fd\u952e\u8868\u683c\uff0c\u8fd9\u4e2a\u56de\u5934\u5f00\u53d1\u6211\u4f1a\u7528\u5230\u3002MDN\u4e0a\u7684\u8868\u683c\u5b9e\u5728\u662f\u5197\u957f\uff0c\u4e0d\u9002\u5408\u5b9e\u6218\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u4f30\u8ba1\u5206\u4eab\u7684\u4eba\u4e0d\u4f1a\u5f88\u591a\u3002<\/p>\n<p>\u4e0d\u7ba1\u600e\u6837\uff0c\u4f9d\u7136\u9001\u4f60\u4e00\u6735\u5c0f\u7ea2\u82b1\uff0c\u611f\u8c22\u60a8\u7684\u9605\u8bfb\u4e0e\u652f\u6301\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f339.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9755\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9755<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/keyboard-cover.png\" width=\"160\" height=\"80\" alt=\"\u952e\u76d8\u7d20\u6750\u5360\u4f4d\u56fe\u5c01\u9762\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u672c\u6587\u5185\u5bb9\u4e3b\u89813\u90e8\u5206\uff0c\u4e00\u662f\u8bf4\u660e\u4e3a\u4ec0\u4e48keyCode\u820d\u5f03\u4e86\uff0c\u4e8c\u662f\u5bf9\u6bd4event.key\u548cevent.code\uff0c\u4e09\u662f\u6574\u7406\u5e38\u7528\u529f\u80fd\u952e\u7684event.key\u548cevent.code\u503c\uff0c\u65b9\u4fbf\u4f7f\u7528\u65f6\u5019\u5feb\u901f\u67e5\u627e\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1698,13,1699,1697,471,1700,1125],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9755"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9755"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9755\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}