簡體   English   中英

將Javascript密鑰代碼轉換為Char,反之亦然

[英]Converting Javascript Key Code to Char and vice versa

我正在處理HTML輸入過濾,並且遇到以下情況:

我需要將keyCodes轉換為chars,但是當我使用Numpad鍵時,我得到了奇怪的結果:

String.fromCharCode(96) //should be 0 but I recieve "`"
String.fromCharCode(97) //should be 1 but I recieve "a"
String.fromCharCode(98) //should be 2 but I receive "b"
String.fromCharCode(99) //should be 3 but I recieve "c"
String.fromCharCode(100) //should be 4 but I recieve "d"
String.fromCharCode(101) //should be 5 but I recieve "e"
String.fromCharCode(102) //should be 6 but I recieve "f"
String.fromCharCode(103) //should be 7 but I recieve "g"

從文檔和事件調試中,我可以看到以下映射:

numpad 0    96
numpad 1    97
numpad 2    98
numpad 3    99
numpad 4    100
numpad 5    101
numpad 6    102
numpad 7    103 

鏈接: https//www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

我在這里想念什么?

使用ASCII表獲取字符代碼。

可能只是一個愚蠢的錯誤。 您應該看到的字符編號是48-57。

for ( let i = 0; i < 10; i++ ) {
  console.log( String.fromCharCode( 48 + i ) );
}

見男人ascii

好的,因此,根據網絡上的多個來源,我應該執行以下邏輯:

if(keyCode >= 96 && keyCode <= 105){
    keyCode -= 48;
}

知道String.fromCharCode函數文檔沒有對此說什么:(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM