2016-12-16 76 views
0

我用onkeydown事件和event對象閱讀的按鍵:反斜槓鍵代碼被識別爲德國元音

function test(e) { 
    e = e || window.e; 
    var keyCode = e.which || e.keyCode; 

    alert(keyCode +' -> '+ String.fromCharCode(keyCode)); 
} 

以上功能的工作原理怪異,如果用戶按反斜線鍵(\) - 它總是返回Ü,而不是\e.keyCode的值是220這意味着一切正常,所以可能fromCharCode()是個問題。 我認爲這個問題是一個事實,反斜槓是一個特殊的字符。但是,我怎麼能省略它,並顯示220代碼適當的字符?

我在JSFiddle上做了一個實例。

+0

反斜槓中charCode是'92',不'220'。我得到了uml; aut也報告了我的,但我不知道爲什麼...(''\\'。charCodeAt(0)=== 92')。我非常懷疑'fromCharCode'的問題,因爲這是非常古老的,有人必須注意到某些時候..所以也許瀏覽器給你錯誤的charcode。 – somethinghere

+0

檢查Unicode表,220是U(有兩個點):http://www.utf8-chartable.de/unicode-utf8-table.pl?unicodeinhtml=dec – Justinas

+0

@Justinas,是的,但我也嘗試過,當只要按鍵盤上的'\',它就會報告我的charcode 220。這是意想不到的。 – somethinghere

回答

1

這裏的問題在於您試圖將鍵盤鍵碼與鍵盤佈局的實際字母匹配。

EG(test keyboard key code):

  1. 我的鍵盤佈局的en-US
  2. 我按2和關鍵代碼是50
  3. 我改變鍵盤佈局(或鍵盤語言)來LT -LT
  4. 我現在按相同的鍵,但現在它應該輸出č,但鍵碼仍然50

你應該有一些一些字母 - 鍵碼的地圖,但你永遠不會匹配所有鍵盤佈局。

function displayKeyCode(evt) { 
 
    var textBox = getObject('txtChar'); 
 
    var charCode = (evt.which) ? evt.which : event.keyCode 
 
    textBox.value = String.fromCharCode(charCode); 
 
    if (charCode == 8) textBox.value = "backspace"; // backspace 
 
    if (charCode == 9) textBox.value = "tab"; // tab 
 
    if (charCode == 13) textBox.value = "enter"; // enter 
 
    if (charCode == 16) textBox.value = "shift"; // shift 
 
    if (charCode == 17) textBox.value = "ctrl"; // ctrl 
 
    if (charCode == 18) textBox.value = "alt"; // alt 
 
    if (charCode == 19) textBox.value = "pause/break"; // pause/break 
 
    if (charCode == 20) textBox.value = "caps lock"; // caps lock 
 
    if (charCode == 27) textBox.value = "escape"; // escape 
 
    if (charCode == 33) textBox.value = "page up"; // page up, to avoid displaying alternate character and confusing people \t   
 
    if (charCode == 34) textBox.value = "page down"; // page down 
 
    if (charCode == 35) textBox.value = "end"; // end 
 
    if (charCode == 36) textBox.value = "home"; // home 
 
    if (charCode == 37) textBox.value = "left arrow"; // left arrow 
 
    if (charCode == 38) textBox.value = "up arrow"; // up arrow 
 
    if (charCode == 39) textBox.value = "right arrow"; // right arrow 
 
    if (charCode == 40) textBox.value = "down arrow"; // down arrow 
 
    if (charCode == 45) textBox.value = "insert"; // insert 
 
    if (charCode == 46) textBox.value = "delete"; // delete 
 
    if (charCode == 91) textBox.value = "left window"; // left window 
 
    if (charCode == 92) textBox.value = "right window"; // right window 
 
    if (charCode == 93) textBox.value = "select key"; // select key 
 
    if (charCode == 96) textBox.value = "numpad 0"; // numpad 0 
 
    if (charCode == 97) textBox.value = "numpad 1"; // numpad 1 
 
    if (charCode == 98) textBox.value = "numpad 2"; // numpad 2 
 
    if (charCode == 99) textBox.value = "numpad 3"; // numpad 3 
 
    if (charCode == 100) textBox.value = "numpad 4"; // numpad 4 
 
    if (charCode == 101) textBox.value = "numpad 5"; // numpad 5 
 
    if (charCode == 102) textBox.value = "numpad 6"; // numpad 6 
 
    if (charCode == 103) textBox.value = "numpad 7"; // numpad 7 
 
    if (charCode == 104) textBox.value = "numpad 8"; // numpad 8 
 
    if (charCode == 105) textBox.value = "numpad 9"; // numpad 9 
 
    if (charCode == 106) textBox.value = "multiply"; // multiply 
 
    if (charCode == 107) textBox.value = "add"; // add 
 
    if (charCode == 109) textBox.value = "subtract"; // subtract 
 
    if (charCode == 110) textBox.value = "decimal point"; // decimal point 
 
    if (charCode == 111) textBox.value = "divide"; // divide 
 
    if (charCode == 112) textBox.value = "F1"; // F1 
 
    if (charCode == 113) textBox.value = "F2"; // F2 
 
    if (charCode == 114) textBox.value = "F3"; // F3 
 
    if (charCode == 115) textBox.value = "F4"; // F4 
 
    if (charCode == 116) textBox.value = "F5"; // F5 
 
    if (charCode == 117) textBox.value = "F6"; // F6 
 
    if (charCode == 118) textBox.value = "F7"; // F7 
 
    if (charCode == 119) textBox.value = "F8"; // F8 
 
    if (charCode == 120) textBox.value = "F9"; // F9 
 
    if (charCode == 121) textBox.value = "F10"; // F10 
 
    if (charCode == 122) textBox.value = "F11"; // F11 
 
    if (charCode == 123) textBox.value = "F12"; // F12 
 
    if (charCode == 144) textBox.value = "num lock"; // num lock 
 
    if (charCode == 145) textBox.value = "scroll lock"; // scroll lock 
 
    if (charCode == 186) textBox.value = ";"; // semi-colon 
 
    if (charCode == 187) textBox.value = "="; // equal-sign 
 
    if (charCode == 188) textBox.value = ","; // comma 
 
    if (charCode == 189) textBox.value = "-"; // dash 
 
    if (charCode == 190) textBox.value = "."; // period 
 
    if (charCode == 191) textBox.value = "/"; // forward slash 
 
    if (charCode == 192) textBox.value = "`"; // grave accent 
 
    if (charCode == 219) textBox.value = "["; // open bracket 
 
    if (charCode == 220) textBox.value = "\\"; // back slash 
 
    if (charCode == 221) textBox.value = "]"; // close bracket 
 
    if (charCode == 222) textBox.value = "'"; // single quote 
 
    var lblCharCode = getObject('spnCode'); 
 
    lblCharCode.innerHTML = 'KeyCode: ' + charCode; 
 
    return false; 
 
} 
 

 
function getObject(obj) { 
 
    var theObj; 
 
    if (document.all) { 
 
    if (typeof obj == 'string') { 
 
     return document.all(obj); 
 
    } else { 
 
     return obj.style; 
 
    } 
 
    } 
 
    if (document.getElementById) { 
 
    if (typeof obj == 'string') { 
 
     return document.getElementById(obj); 
 
    } else { 
 
     return obj.style; 
 
    } 
 
    } 
 
    return null; 
 
}
<input onkeypress="javascript:return false;" id="txtChar" onkeydown="javascript:return displayKeyCode(event)" name="txtChar" type="text"/><br/> 
 
<span id="spnCode"></span>

+0

我不確定是否理解正確:您提供的代碼和網站 - 兩者均在鍵盤上顯示反斜槓代碼「220」。對於我已將鍵盤佈局更改爲en-US的測試,反斜槓仍會返回代碼220.更重要的是:我默認使用pl-PL語言,而我們的語言中沒有變音符號。我檢查了關鍵代碼表(http://www.foreui.com/articles/Key_Code_Table.htm)和'\\'有220值:| –