js判断键盘按键
2008-06-20 15:30:38| 分类: 工作整理 阅读227 评论0 字号:大中小 订阅
判断方法:
表 3. DHTML 键盘事件 |
名称 | 说明 |
onkeypress | 这个事件在用户按下并放开任何字母数字键时发生。系统按钮(例如,箭头键和功能键)无法得到识别。 |
onkeyup | 这个事件在用户放开任何先前按下的键盘键时发生。 |
onkeydown | 这个事件在用户按下任何键盘键(包括系统按钮,如箭头键和功能键)时发生。 |
示例:
function document.onkeydown()
{
if ( event.keyCode=='39' ) //->右箭头
{
window.open("http://www.163.com");
}
}
function document.onkeypress()
{
if ( event.keyCode=='43' )
{
alert( '你输入了键盘的 “ + ” 键');
}
}
在使用JavaScript做WEB键盘事件侦听捕获时,主要采用onkeypress、onkeydown、onkeyup三个事件进行出来。该三个事件的执行顺序如下:onkeydown -> onkeypress ->onkeyup。在一般情况下,采用三种键盘事件均可对键盘输入进行有效的响应。当在实际使用中,会发现这几者有些不同的差别。
onkeypress事件不能对系统功能键(例如:后退、删除等,其中对中文输入法不能有效响应)进行正常的响应,onkeydown和onkeyup均可以对系统功能键进行有效的拦截,但事件截获的位置不同,可以根据具体的情况选择不同的键盘事件。
由于onkeypress不能对系统功能键进行捕获,导致window.event对象的keyCode属性和onkeydown,onkeyup键盘事件中获取的keyCode属性不同,主要表现在onkeypress事件的keyCode对字母的大小写敏感,而onkeydown、onkeyup事件不敏感;onkeypress事件的keyCode无法区分主键盘上的数字键和付键盘数字键的,而onkeydown、onkeyup的keyCode对主付键盘的数字键敏感。
http://liu2liu2.spaces.live.com/blog/cns!54B12917375EF5A0!270.entry
屏蔽组合键ctrl+N
<script language=javascript>
function KeyDown(){
if ((window.event.altKey)&&
((window.event.keyCode==37)|| //屏蔽 Alt+ 方向键 ←
(window.event.keyCode==39))){ //屏蔽 Alt+ 方向键 →
alert("不准你使用ALT+方向键前进或后退网页!");
event.returnValue=false;
}
if ((event.keyCode==8)|| //屏蔽退格删除键
(event.keyCode==116)){ //屏蔽 F5 刷新键
event.keyCode=0;
event.returnValue=false;
}
if ((event.ctrlKey)&&(event.keyCode==78)){ //屏蔽 Ctrl+n
event.returnValue=false;
}
if ((event.shiftKey)&&(event.keyCode==121)){ //屏蔽 shift+F10
event.returnValue=false;
}
if (event.keyCode==122){ //屏蔽 F11
event.returnValue=false;
}
}
ASCII码 | 键盘 | ASCII 码 | 键盘 | ASCII 码 | 键盘 | ASCII 码 | 键盘 | 27 | ESC | 32 | SPACE | 33 | ! | 34 | " | 35 | # | 36 | $ | 37 | % | 38 | & | 39 | ' | 40 | ( | 41 | ) | 42 | * | 43 | + | 44 | ' | 45 | - | 46 | . | 47 | / | 48 | 0 | 49 | 1 | 50 | 2 | 51 | 3 | 52 | 4 | 53 | 5 | 54 | 6 | 55 | 7 | 56 | 8 | 57 | 9 | 58 | : | 59 | ; | 60 | < | 61 | = | 62 | > | 63 | ? | 64 | @ | 65 | A | 66 | B | 67 | C | 68 | D | 69 | E | 70 | F | 71 | G | 72 | H | 73 | I | 74 | J | 75 | K | 76 | L | 77 | M | 78 | N | 79 | O | 80 | P | 81 | Q | 82 | R | 83 | S | 84 | T | 85 | U | 86 | V | 87 | W | 88 | X | 89 | Y | 90 | Z | 91 | [ | 92 | \ | 93 | ] | 94 | ^ | 95 | _ | 96 | ` | 97 | a | 98 | b | 99 | c | 100 | d | 101 | e | 102 | f | 103 | g | 104 | h | 105 | i | 106 | j | 107 | k | 108 | l | 109 | m | 110 | n | 111 | o | 112 | p | 113 | q | 114 | r | 115 | s | 116 | t | 117 | u | 118 | v | 119 | w | 120 | x | 121 | y | 122 | z | 123 | { | 124 | | | 125 | } | 126 | ~ | |
键盘数字对应的ASCII码(keycode码)
2011-01-29 14:46
keycode 1 = 鼠标左键 keycode 2 = 鼠标右键 keycode 3 = Cancel keycode 4 = 鼠标中键 keycode 8 = BackSpace keycode 9 = Tab keycode 12 = Clear(未知) keycode 13 = Enter keycode 16 = Shift_L keycode 17 = Control_L keycode 18 = Alt_L keycode 19 = Pause keycode 20 = Caps_Lock keycode 27 = Escape keycode 32 = space keycode 33 = Prior(PageUp键) keycode 34 = Next(PageDown键) keycode 35 = End keycode 36 = Home keycode 37 = Left keycode 38 = Up keycode 39 = Right keycode 40 = Down keycode 41 = Select(未知) keycode 42 = Print(PrintScreen键) keycode 43 = Execute(未知) keycode 45 = Insert keycode 46 = Delete keycode 47 = Help(未知) keycode 48 = 0 equal braceright keycode 49 = 1 exclam onesuperior keycode 50 = 2 quotedbl twosuperior keycode 51 = 3 section threesuperior keycode 52 = 4 dollar keycode 53 = 5 percent keycode 54 = 6 ampersand keycode 55 = 7 slash braceleft keycode 56 = 8 parenleft bracketleft keycode 57 = 9 parenright bracketright keycode 65 = a A keycode 66 = b B keycode 67 = c C keycode 68 = d D keycode 69 = e E EuroSign keycode 70 = f F keycode 71 = g G keycode 72 = h H keycode 73 = i I keycode 74 = j J keycode 75 = k K keycode 76 = l L keycode 77 = m M mu keycode 78 = n N keycode 79 = o O keycode 80 = p P keycode 81 = q Q at keycode 82 = r R keycode 83 = s S keycode 84 = t T keycode 85 = u U keycode 86 = v V keycode 87 = w W keycode 88 = x X keycode 89 = y Y keycode 90 = z Z keycode 96 = KP_0 KP_0(小键盘数字0) keycode 97 = KP_1 KP_1(小键盘数字1) keycode 98 = KP_2 KP_2(小键盘数字2) keycode 99 = KP_3 KP_3(小键盘数字3) keycode 100 = KP_4 KP_4(小键盘数字4) keycode 101 = KP_5 KP_5(小键盘数字5) keycode 102 = KP_6 KP_6(小键盘数字6) keycode 103 = KP_7 KP_7(小键盘数字7) keycode 104 = KP_8 KP_8(小键盘数字8) keycode 105 = KP_9 KP_9(小键盘数字9) keycode 106 = KP_Multiply KP_Multiply(小键盘乘) keycode 107 = KP_Add KP_Add(小键盘加) keycode 108 = KP_Separator KP_Separator(小键盘回车键) keycode 109 = KP_Subtract KP_Subtract(小键盘减) keycode 110 = KP_Decimal KP_Decimal(小键盘小数点) keycode 111 = KP_Divide KP_Divide(小键盘除) keycode 112 = F1 keycode 113 = F2 keycode 114 = F3 keycode 115 = F4 keycode 116 = F5 keycode 117 = F6 keycode 118 = F7 keycode 119 = F8 keycode 120 = F9 keycode 121 = F10 keycode 122 = F11 keycode 123 = F12 keycode 124 = F13 keycode 125 = F14 keycode 126 = F15 keycode 127 = F16 keycode 128 = F17 keycode 129 = F18 keycode 130 = F19 keycode 131 = F20 keycode 132 = F21 keycode 133 = F22 keycode 134 = F23 keycode 135 = F24 keycode 136 = Num_Lock keycode 137 = Scroll_Lock(未知) keycode 187 = acute grave(未知) keycode 188 = comma semicolon(未知)(逗号 分号) keycode 189 = minus underscore (减号 下划线) keycode 190 = period colon (未知 冒号) keycode 192 = numbersign apostrophe(未知 单引号) keycode 210 = plusminus hyphen macron (未知 & ^) keycode 211 = keycode 212 = copyright registered(未知) keycode 213 = guillemotleft guillemotright(未知) keycode 214 = masculine ordfeminine(未知) keycode 215 = ae AE(未知) keycode 216 = cent yen(未知) keycode 217 = questiondown exclamdown(未知) keycode 218 = onequarter onehalf threequarters(未知) keycode 220 = less greater bar(小于号 大于号 未知) keycode 221 = plus asterisk asciitilde(加号 星号 未知) keycode 227 = multiply division(乘 除) keycode 228 = acircumflex Acircumflex(未知) keycode 229 = ecircumflex Ecircumflex(未知) keycode 230 = icircumflex Icircumflex(未知) keycode 231 = ocircumflex Ocircumflex(未知) keycode 232 = ucircumflex Ucircumflex(未知) keycode 233 = ntilde Ntilde(未知) keycode 234 = yacute Yacute(未知) keycode 235 = oslash Ooblique(未知) keycode 236 = aring Aring(未知) keycode 237 = ccedilla Ccedilla(未知) keycode 238 = thorn THORN(未知) keycode 239 = eth ETH(未知) keycode 240 = diaeresis cedilla currency(未知) keycode 241 = agrave Agrave atilde Atilde(未知) keycode 242 = egrave Egrave(未知) keycode 243 = igrave Igrave(未知) keycode 244 = ograve Ograve otilde Otilde(未知) keycode 245 = ugrave Ugrave(未知) keycode 246 = adiaeresis Adiaeresis(未知) keycode 247 = ediaeresis Ediaeresis(未知) keycode 248 = idiaeresis Idiaeresis(未知) keycode 249 = odiaeresis Odiaeresis(未知) keycode 250 = udiaeresis Udiaeresis(未知) keycode 251 = ssharp question backslash(未知) keycode 252 = asciicircum degree(未知) keycode 253 = 3 sterling(未知) keycode 254 = Mode_switch(未知) 如果要使用组合键,则可以利用event.ctrlKey,event.shiftKey,event .altKey值为true或false,判断是否按下了ctrl键、shift键以及alt键。 例:event.ctrlKey=true && event.keycode=65 |