IE下可以用window.event物件,或是event物件偵測按鍵。但Netscape、Firefox不行。不過NS,FF確實是有event物件的,只是不是全域物件:
<script>function fxn (e) { if(e.shiftKey) .... }</script>
<a href="javascript:void(0);" onclick="fxn(event)">
上面這段程式碼在IE及FF上都是可以運作的。
屬性的使用可以參考下表:
性質 | 描述 |
altKey, ctrlKey, shiftKey |
這是 布林值,用來偵測是否當鍵盤事件發生時 Alt, Ctrl, Meta 與 Shift 鍵是按著的 |
metaKey |
這是 布林值,用來偵測是否 Meta 按鍵被按下 只支援 NS/Firefox |
charCode |
指出按下按鍵的 Unicode 碼,使用 String.fromCharCode(charCode) 將其轉為字串。 只支援 NS/Firefox |
keycode |
指出按下按鍵的 Unicode 碼,使用 String.fromCharCode(keyCode)將其轉為字串。 |
type |
用來代表 event 事件的字串,如:"mouseover", "click" 等. |
which |
Legacy
property indicating the Unicode for the key pressed. Identical
to "charCode", except this property works in NS4 as well. NS/Firefox only. |
方向鍵:左﹐上﹐右﹐下分別是 event.keyCode值:37﹐38﹐39﹐40
event.keycode==13代表Enter鍵
event.keycode==32代表空白鍵
event.keycode==27代表Esc鍵
event.keycode==16代表Shift鍵
event.keycode==17代表Ctrl鍵
event.keycode==18代表Alt鍵
A-Z分別是65~90
http://plog.longwin.com.tw/programming/2006/09/20/key_codes_2006
javascript只要透過overload事件處理函式即可完成鍵盤監聽(偵測)的動作,比起利用系統API做keylogger還要簡單,不過兩者應用方式不同,不能混為一談,廢話不多說直接看code:
<script language="javascript">
//偵測瀏覽器版本
var browser=navigator.appName;
if(browser=="Netscape"){ //如果瀏覽器為Netscape或者Firefox
//開始監聽鍵盤事件
document.captureEvents(Event.KEYDOWN)
document.onkeydown=function(event){
if(event.which==37){
//key code 37為→
alert("你按下了下一頁");
}
else if(event.which==39){
//key code 39為←
alert("你按下了上一頁");
}
}
}
else{ //假設瀏覽器不為Nescape則猜測為ie
//開始監聽鍵盤事件
document.onkeydown = function(){
if(event.whitch==37){
//key code 37為→
alert("你按下了下一頁");
}
else if(event.whitch==39){
//key code 39為←
alert("你按下了上一頁");
}
}
}
</script>
http://nsysumis94.pixnet.net/blog/post/21559708
Key Codes
This document is written by Helge Willum Larsen It contains a list of all "onkeypress", "onkeydown" and "onkeyup" KeyCodes in JavaScript, that can be typed with a DANISH KEYBOARD !!! Other keyboards probably have other KeyCodes.
The first section is tested with the Microsoft Internet Explore 5.0 and the Netscape Communicator 4.51. At this time, Netscape didn't support "onkeydown" and "onkeyup".
I strongly recommend that you use the "onkeypress" function. It has the greatest browser-compatibility. Otherwise, you can maybe use some of the codes in the bottom of the page:
____________________________________________________________________________________________
KeyCodes (ALL keyboards): onkeypress Tested with Internet Explore 5.0 By: Helge Willum Larsen and Netscape Communicator 4.51 on a DANISH keyboard ____________________________________________________________________________________________
SUPPORTS MULTIPLE KEYPRESSES: [A] = [a] [Shift] + [A] = [A] [CapsLock] + [A] = [A] [Shift] + [CapsLock] + [A] = [a]
ALL KEYS THAT CREATES THE SAME OUTPUT, HAVE THE SAME KEYCODES !
[NumPad 1] is the same as [1] (if NumLock) [NumPad +] is the same as [+]
DOES NOT SUPPORT ARROW-KEYS, DELETE, INSERT, SHIFT, ALT, CTRL, TAB, ESC...........! But it supports [Return / Enter] and [Backspace] ____________________________________________________________________________________________
Button: keyCode:
[BackSpace] 8
[Return / Enter] 13
[!] 33 ["] 34 [#] 35 [$] 36 [%] 37 [&] 38 ['] 39 [(] 40 [)] 41 [*] 42 [+] 43 [,] 44 [-] 45 [.] 46 [/] 47 [0] 48 [1] 49 [2] 50 [3] 51 [4] 52 [5] 53 [6] 54 [7] 55 [8] 56 [9] 57 [:] 58 [;] 59 [<] 60 [=] 61 [>] 62 [?] 63 [@] 64 [A] 65 [B] 66 [C] 67 [D] 68 [E] 69 [F] 70 [G] 71 [H] 72 [I] 73 [J] 74 [K] 75 [L] 76 [M] 77 [N] 78 [O] 79 [P] 80 [Q] 81 [R] 82 [S] 83 [T] 84 [U] 85 [V] 86 [W] 87 [X] 88 [Y] 89 [Z] 90 [[] 91 [\] 92 []] 93 [^] 94 [_] 95 [`] 96 [a] 97 [b] 98 [c] 99 [d] 100 [e] 101 [f] 102 [g] 103 [h] 104 [i] 105 [j] 106 [k] 107 [l] 108 [m] 109 [n] 110 [o] 111 [p] 112 [q] 113 [r] 114 [s] 115 [t] 116 [u] 117 [v] 118 [w] 119 [x] 120 [y] 121 [z] 122 [{] 123 [|] 124 [}] 125 [~] 126
[£] 163 [¤] 164
[§] 167 [¨] 168
[´] 180
[½] 189
[Å] 197 [Æ] 198
[Ø] 216
[å] 229 [æ] 230
[ø] 248
____________________________________________________________________________________________
KeyCodes (DANISH keyboard): onkeydown / onkeyup Tested with Internet Explore 5.0) By: Helge Willum Larsen ____________________________________________________________________________________________
DOES NOT SUPPORT MULTIPLE KEYPRESSES !
ALL KEYS HAVE THEIR OWN, INDIVIDUAL KEYCODE !
[NumPad 1] is NOT the same as [1] [NumPad +] is NOT the same as [+] ____________________________________________________________________________________________
Button: keyCode: NumLock: CapsLock: ScrollLock:
[BackSpace] 8 - - - [Tab] 9 - - -
[NumPad 5] 12 no - - [Return / Enter] 13 - - -
[Shift] 16 - - - [Ctrl] 17 - - - [Alt] 18 - - - [Pause / Break] 19 - - - [CapsLock] 20 - - -
[Esc] 27 - - -
[Space] 32 - - - [Page Up] 33 - - - [Page Down] 34 - - - [End] 35 - - - [Home] 36 - - - [LeftArrow] 37 - - - [UpArrow] 38 - - - [RightArrow] 39 - - - [DownArrow] 40 - - -
[PrintScreen / SysRq] 44 - - - [Insert] 45 - - - [Delete] 46 - - -
[0] 48 - - - [1] 49 - - - [2] 50 - - - [3] 51 - - - [4] 52 - - - [5] 53 - - - [6] 54 - - - [7] 55 - - - [8] 56 - - - [9] 57 - - -
[A] 65 - - - [B] 66 - - - [C] 67 - - - [D] 68 - - - [E] 69 - - - [F] 70 - - - [G] 71 - - - [H] 72 - - - [I] 73 - - - [J] 74 - - - [K] 75 - - - [L] 76 - - - [M] 77 - - - [N] 78 - - - [O] 79 - - - [P] 80 - - - [Q] 81 - - - [R] 82 - - - [S] 83 - - - [T] 84 - - - [U] 85 - - - [V] 86 - - - [W] 87 - - - [X] 88 - - - [Y] 89 - - - [Z] 90 - - -
[Turn Off] (special) 94 - - - [Sleep] (special) 95 - - - [NumPad 0] 96 yes - - [NumPad 1] 97 yes - - [NumPad 2] 98 yes - - [NumPad 3] 99 yes - - [NumPad 4] 100 yes - - [NumPad 5] 101 yes - - [NumPad 6] 102 yes - - [NumPad 7] 103 yes - - [NumPad 8] 104 yes - - [NumPad 9] 105 yes - - [NumPad *] 106 - - - [NumPad +] 107 - - - [NumPad -] 109 - - - [NumPad ,] 110 - - - [NumPad /] 111 - - - [F1] 112 - - - [F2] 113 - - - [F3] 114 - - - [F4] 115 - - - [F5] 116 - - - [F6] 117 - - - [F7] 118 - - - [F8] 119 - - - [F9] 120 - - - [F10] 121 - - - [F11] 122 - - - [F12] 123 - - -
[NumLock] 144 - - - [ScrollLock] 145 - - -
[+] (next to [0]) 187 - - - [,] 188 - - - [-] (next to [.]) 189 - - - [.] 190 - - -
[Æ] 192 - - -
[´] (next to [+]) 219 - - - [½] (next to [1]) 220 - - - [Å] 221 - - - [Ø] 222 - - -
[<] (next to [Z]) 226 - - -
[Wake Up] (special) 255 - - -
____________________________________________________________________________________________
|