Last active
December 14, 2015 05:29
-
-
Save asika32764/5035753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 判斷按下的按鈕是哪一個 | |
* @param (event) e keypress event. | |
* @param (mix) targetKeyChar 按鍵的編號或名稱,如果是這個按鍵,就執行 callback | |
* @param (function) callBack 送入callback的function,不用加()括號 | |
* | |
*/ | |
var detectKeyPress = function(e, targetKeyChar, callBack){ | |
var keynum | |
var keychar | |
var numcheck | |
if(window.event) // For IE | |
{ | |
keynum = e.keyCode | |
}else if(e.which) // For Netscape/Firefox/Opera | |
{ | |
keynum = e.which | |
} | |
keychar = String.fromCharCode(keynum) | |
if( typeOf(targetKeyChar) == 'string' ) { | |
if(targetKeyChar == keychar) { | |
callBack() ; | |
} | |
}else{ | |
if(targetKeyChar == keynum) { | |
callBack() ; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input onkeypress="detectKeyPress(event, 'a', callBackFunction );" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment