js 判断键盘事件大全(兼容FireFox和IE,包括退格、回车、空格、方向键、删除键等)

发布时间:2019-11-29编辑:脚本学堂
为大家介绍js键盘事件全面控制(兼容FireFox和IE)的方法,难得一遇的好文章,有需要的朋友,千万不要错过。脚本学堂,为大家收集整理。

附:键盘对应编码值
 

复制代码 代码示例:

<html>
<head>
</head>
<body onkeyup="window.alert(showKeyName(event))">
按键测试,支持像 Ctrl+Alt+Shift+T 的组合键(注:非浏览器热键)
<script type="text/javascript">
function showKeyName(e)
{
var keyName;
switch(e.keyCode)
{
case 8:keyName = e.keyCode+"[退格]";break;
case 9:keyName = e.keyCode+"[Tab]";break;
case 12:keyName = e.keyCode+"[Clear]";break;
case 13:keyName = e.keyCode+"[Enter]";break;
case 16:keyName = e.keyCode+"[Shift]";break;
case 17:keyName = e.keyCode+"[Ctrl]";break;
case 18:keyName = e.keyCode+"[Alt]";break;
case 19:keyName = e.keyCode+"[PauseBreak]";break;
case 20:keyName = e.keyCode+"[Caps Lock]";break;
case 27:keyName = e.keyCode+"[Esc]";break;
case 32:keyName = e.keyCode+"[空格]";break;
case 33:keyName = e.keyCode+"[PageUp]";break;
case 34:keyName = e.keyCode+"[PageDown]";break;
case 35:keyName = e.keyCode+"[End]";break;
case 36:keyName = e.keyCode+"[Home]";break;
case 37:keyName = e.keyCode+"[方向键左]";break;
case 38:keyName = e.keyCode+"[方向键上]";break;
case 39:keyName = e.keyCode+"[方向键右]";break;
case 40:keyName = e.keyCode+"[方向键下]";break;

case 41:keyName = e.keyCode+"[Select]";break;
case 42:keyName = e.keyCode+"[Print]";break;

case 43:keyName = e.keyCode+"[Execute]";break;
case 45:keyName = e.keyCode+"[Insert]";break;
case 46:keyName = e.keyCode+"[Delete]";break;
case 47:keyName = e.keyCode+"[Help]";break;
case 48:keyName = e.keyCode+"[0 equal braceright]";break;
case 49:keyName = e.keyCode+"[1 exclam onesuperior]";break;
case 50:keyName = e.keyCode+"[2 quotedbl twosuperior]";break;
case 51:keyName = e.keyCode+"[3 section threesuperior]";break;
case 52:keyName = e.keyCode+"[4 dollar]";break;
case 53:keyName = e.keyCode+"[5 percent]";break;

case 54:keyName = e.keyCode+"[6 ampersand]";break;
case 55:keyName = e.keyCode+"[7 slash braceleft]";break;
case 56:keyName = e.keyCode+"[8 parenleft bracketleft]";break;
case 57:keyName = e.keyCode+"[9 parenright bracketright]";break;
case 65:keyName = e.keyCode+"[a A]";break;
case 66:keyName = e.keyCode+"[b B]";break;
case 67:keyName = e.keyCode+"[c C]";break;
case 68:keyName = e.keyCode+"[d D]";break;
case 69:keyName = e.keyCode+"[e E EuroSign]";break;
case 70:keyName = e.keyCode+"[f F]";break;
case 71:keyName = e.keyCode+"[g G]";break;
case 72:keyName = e.keyCode+"[h H]";break;
case 73:keyName = e.keyCode+"[i I]";break;
case 74:keyName = e.keyCode+"[j J]";break;
case 75:keyName = e.keyCode+"[k K]";break;
case 76:keyName = e.keyCode+"[l L]";break;
case 77:keyName = e.keyCode+"[m M mu]";break;
case 78:keyName = e.keyCode+"[n N]";break;
case 79:keyName = e.keyCode+"[o O]";break;


case 80:keyName = e.keyCode+"[p P]";break;


case 81:keyName = e.keyCode+"[q Q at]";break;


case 82:keyName = e.keyCode+"[r R]";break;


case 83:keyName = e.keyCode+"[s S]";break;


case 84:keyName = e.keyCode+"[t T]";break;


case 85:keyName = e.keyCode+"[u U]";break;


case 86:keyName = e.keyCode+"[v V]";break;


case 87:keyName = e.keyCode+"[w W]";break;


case 88:keyName = e.keyCode+"[x X]";break;


case 89:keyName = e.keyCode+"[y Y]";break;

case 90:keyName = e.keyCode+"[z Z]";break;
case 91:keyName = e.keyCode+"[左Win]";break;
case 92:keyName = e.keyCode+"[右Win]";break;
case 93:keyName = e.keyCode+"[快捷菜单键]";break;
case 95:keyName = e.keyCode+"[Sleep]";break;
case 96:keyName = e.keyCode+"[小键盘区0]";break;
case 97:keyName = e.keyCode+"[小键盘区1]";break;
case 98:keyName = e.keyCode+"[小键盘区2]";break;
case 99:keyName = e.keyCode+"[小键盘区3]";break;
case 100:keyName = e.keyCode+"[小键盘区4]";break;
case 101:keyName = e.keyCode+"[小键盘区5]";break;
case 102:keyName = e.keyCode+"[小键盘区6]";break;
case 103:keyName = e.keyCode+"[小键盘区7]";break;
case 104:keyName = e.keyCode+"[小键盘区8]";break;
case 105:keyName = e.keyCode+"[小键盘区9]";break;
case 106:keyName = e.keyCode+"[*]";break;
case 107:keyName = e.keyCode+"[+]";break;
case 109:keyName = e.keyCode+"[-]";break;
case 110:keyName = e.keyCode+"[.]";break;
case 111:keyName = e.keyCode+"[/]";break;
case 112:keyName = e.keyCode+"[F1]";break;
case 113:keyName = e.keyCode+"[F2]";break;
case 114:keyName = e.keyCode+"[F3]";break;
case 115:keyName = e.keyCode+"[F4]";break;
case 116:keyName = e.keyCode+"[F5]";break;
case 117:keyName = e.keyCode+"[F6]";break;
case 118:keyName = e.keyCode+"[F7]";break;
case 119:keyName = e.keyCode+"[F8]";break;
case 120:keyName = e.keyCode+"[F9]";break;
case 121:keyName = e.keyCode+"[F10]";break;
case 122:keyName = e.keyCode+"[F11]";break;
case 123:keyName = e.keyCode+"[F12]";break;

case 124:keyName = e.keyCode+"[F13]";break;


case 125:keyName = e.keyCode+"[F14]";break;


case 126:keyName = e.keyCode+"[F15]";break;


case 127:keyName = e.keyCode+"[F16]";break;


case 128:keyName = e.keyCode+"[F17]";break;


case 129:keyName = e.keyCode+"[F18]";break;


case 130:keyName = e.keyCode+"[F19]";break;


case 131:keyName = e.keyCode+"[F20]";break;


case 132:keyName = e.keyCode+"[F21]";break;


case 133:keyName = e.keyCode+"[F22]";break;


case 134:keyName = e.keyCode+"[F23]";break;


case 135:keyName = e.keyCode+"[F24]";break;


case 136:keyName = e.keyCode+"[Num_Lock]";break;


case 137:keyName = e.keyCode+"[Scroll_Lock]";break;
case 144:keyName = e.keyCode+"[NumLock]";break;
case 145:keyName = e.keyCode+"[ScrollLock]";break;
case 186:keyName = e.keyCode+"[;]";break;
case 187:keyName = e.keyCode+"[=]";break;
case 188:keyName = e.keyCode+"[,]";break;
case 189:keyName = e.keyCode+"[-]";break;
case 190:keyName = e.keyCode+"[.]";break;
case 191:keyName = e.keyCode+"[/]";break;
case 192:keyName = e.keyCode+"[`]";break;

case 210:keyName = e.keyCode+"[plusminus hyphen macron]";break;


case 211:keyName = e.keyCode+"[]";break;


case 212:keyName = e.keyCode+"[copyright registered]";break;


case 213:keyName = e.keyCode+"[guillemotleft guillemotright]";break;


case 214:keyName = e.keyCode+"[masculine ordfeminine]";break;


case 215:keyName = e.keyCode+"[ae AE]";break;


case 216:keyName = e.keyCode+"[cent yen]";break;


case 217:keyName = e.keyCode+"[questiondown exclamdown]";break;


case 218:keyName = e.keyCode+"[onequarter onehalf threequarters]";break;
case 219:keyName = e.keyCode+"[[]";break;
case 220:keyName = e.keyCode+"[//]";break;
case 221:keyName = e.keyCode+"[]]";break;
case 222:keyName = e.keyCode+"[']";break;

case 227:keyName = e.keyCode+"[multiply division]";break;


case 228:keyName = e.keyCode+"[acircumflex Acircumflex]";break;


case 229:keyName = e.keyCode+"[ecircumflex Ecircumflex]";break;


case 230:keyName = e.keyCode+"[icircumflex Icircumflex]";break;


case 231:keyName = e.keyCode+"[ocircumflex Ocircumflex]";break;


case 232:keyName = e.keyCode+"[ucircumflex Ucircumflex]";break;


case 233:keyName = e.keyCode+"[ntilde Ntilde]";break;


case 234:keyName = e.keyCode+"[yacute Yacute]";break;


case 235:keyName = e.keyCode+"[oslash Ooblique]";break;


case 236:keyName = e.keyCode+"[aring Aring]";break;


case 237:keyName = e.keyCode+"[ccedilla Ccedilla]";break;


case 238:keyName = e.keyCode+"[thorn THORN]";break;


case 239:keyName = e.keyCode+"[eth ETH]";break;


case 240:keyName = e.keyCode+"[diaeresis cedilla currency]";break;


case 241:keyName = e.keyCode+"[agrave Agrave atilde Atilde]";break;


case 242:keyName = e.keyCode+"[egrave Egrave]";break;


case 243:keyName = e.keyCode+"[igrave Igrave]";break;


case 244:keyName = e.keyCode+"[ograve Ograve otilde Otilde]";break;


case 245:keyName = e.keyCode+"[ugrave Ugrave]";break;


case 246:keyName = e.keyCode+"[adiaeresis Adiaeresis]";break;


case 247:keyName = e.keyCode+"[ediaeresis Ediaeresis]";break;


case 248:keyName = e.keyCode+"[idiaeresis Idiaeresis]";break;


case 249:keyName = e.keyCode+"[odiaeresis Odiaeresis]";break;


case 250:keyName = e.keyCode+"[udiaeresis Udiaeresis]";break;


case 251:keyName = e.keyCode+"[ssharp question backslash]";break;


case 252:keyName = e.keyCode+"[asciicircum degree]";break;


case 253:keyName = e.keyCode+"[3 sterling]";break;


case 254:keyName = e.keyCode+"[Mode_switch]";break;
//case 255:keyName = e.keyCode+"[Power]";break;
case 255:keyName = e.keyCode+"[Wake]";break;
default:keyName = e.keyCode+"[" + String.fromCharCode(e.keyCode) + "]";break;
}
if((e.shiftKey)&&(e.keyCode!=16)){keyName = e.keyCode+"[Shift] + " + keyName;}
if((e.altKey)&&(e.keyCode!=18)){keyName = e.keyCode+"[Alt] + " + keyName;}
if((e.ctrlKey)&&(e.keyCode!=17)){keyName = e.keyCode+"[Ctrl] + " + keyName;}
return keyName;
}
</script>
</body>
</html>

<script language="javascript">
   function window.document.onkeyup()
{
     alert(event.keyCode);
}
</script>
 

如果要使用组合键,则可以利用event.ctrlKey,event.shiftKey,event .altKey判断
是否按下了ctrl键、shift键以及alt键。 

您可能感兴趣的文章:
js去空格技巧 js去除字符串前后、左右空格
js 去除首尾空格的方法
js去掉字符串的空格或换行符(附相关正则介绍)
js 禁止文本框输入空格的代码
js去掉空格的代码
js去除空格的正则用法
JS去除字符串的中间空格的代码
js去掉字符串左右空格的方法(eval与trim函数应用举例)
js去除空格的实现代码
JS限制文本框中输入空格的方法
js去除字符串前后空格的多种方法
js去除空格的代码(前后空格、前空格、后空格)
Js过滤空格的代码(附演示截图)
截取两边空格的自定义js小函数
JavaScript去除字符串两端的空格