我的工作,旨在使用戶能夠在不同的非洲languages.Currently鍵入一個javascript鍵盤上鍵入的一個不同的鍵盤字符,這工作正常在IE8和Firefox,但不是谷歌Chrome,和我(例如,鍵入我的物理鍵盤)'q'(keyCode = 113)並獲得'''(keyCode = 603)作爲輸出但目前,我的代碼在谷歌瀏覽器中沒有任何作用。我的代碼的相關部分如下:??顯示在谷歌瀏覽器
var k_layouts = {};
k_layouts.Akan = {88:390,113:603};//keyCode mappings for Akan language
k_layouts.Ga = {120:596,81:400};//keyCode mappings for Ga language
var current_layout = "";
//function that maps the keyCode of a **typed** key to that of the **expected** key
function map_key_code(keycode){
if(k_layouts[current_layout] && k_layouts[current_layout][keycode])
return k_layouts[current_layout][keycode];
return keycode;
}
//function that actually changes the keyCode of a **typed** key to the **expected** value
function handle_keypress(ev){
var ev = ev || window.event;
if(ev.bubbles != null ||!ev.bubbles)
return true;
var target = ev.target || ev.srcElement;
var keyCode = window.event? ev.keyCode: ev.which;
if(keyCode == 0)
return true;
var newKeyCode = map_key_code(keyCode);
if(newKeyCode == keyCode)
return true;
if(target.addEventListener){ //for chrome and firefox
//cancel event
ev.preventDefault();
ev.stopPropagation();
//create new event with the keycode changed
var evt = document.createEvent("KeyboardEvent");
try{//for firefox(works fine)
evt.initKeyEvent("keypress",false,true,document.defaultView,ev.ctrlKey,ev.altKey,ev.shiftKey,ev.metaKey,newKeyCode,newKeyCode);
}
catch(e){// for google chrome(does not work as expected)
evt.initKeyboardEvent("keydown",false,true,document.defaultView,ev.ctrlKey,ev.altKey,ev.shiftKey,ev.metaKey,newKeyCode,newKeyCode);
}
//dispatch new event
target.dispatchEvent(evt);
}
else if(target.attachEvent){// works for IE
ev.keyCode = newKeyCode;
}
}
有沒有實現我所追求的鍍鉻做或一種方式,是有我丟失的東西在我的方法,我會很高興的任何幫助和任何想法。
恭喜!你發現了一個錯誤! https://bugs.webkit.org/show_bug.cgi?id=16735和http://code.google.com/p/chromium/issues/detail?id=27048 – syockit 2010-08-30 04:29:38