gpt4 book ai didi

javascript - 显示与谷歌浏览器中键入的不同的键盘字符

转载 作者:行者123 更新时间:2023-12-01 15:53:44 25 4
gpt4 key购买 nike

我正在开发一个旨在使用户能够输入各种非洲语言的 javascript 键盘。目前,这在 IE8 和 firefox 中运行良好,但在 google 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;
}
}

有没有办法实现我在 chrome 中想要做的事情?或者,我的方法中是否缺少一些东西?我很高兴得到任何帮助和任何想法。

最佳答案

不创建新事件会更容易,正如您所观察到的那样,它并没有得到普遍支持,而是取消事件并插入与键入的字符相对应的映射字符。

假设用户正在输入一个 id 为“ta”的 textarea,下面将处理所有主要浏览器中该 textarea 的键盘输入,映射 qɛ和“X”的所有其他字符用于说明目的。

请注意,在 IE <= 8 中,用于查找与换行符相关的选择的代码存在问题,为简洁起见,以下代码未处理这些问题。您可以在此处获取我的跨浏览器功能以正确处理此问题:Is there an Internet Explorer approved substitute for selectionStart and selectionEnd?

var charMap = {
"q": "ɛ"
};

document.getElementById("ta").onkeypress = function(evt) {
var val = this.value;
evt = evt || window.event;

// Ensure we only handle printable keys, excluding enter and space
var charCode = typeof evt.which == "number" ? evt.which : evt.keyCode;
if (charCode && charCode != 13 && charCode != 32) {
var keyChar = String.fromCharCode(charCode);

// Get the mapped character (default to "X" for illustration purposes)
var mappedChar = charMap[keyChar] || "X";

var start, end;
if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
// Non-IE browsers and IE 9
start = this.selectionStart;
end = this.selectionEnd;
this.value = val.slice(0, start) + mappedChar + val.slice(end);

// Move the caret
this.selectionStart = this.selectionEnd = start + 1;
} else if (document.selection && document.selection.createRange) {
// For IE up to version 8
var selectionRange = document.selection.createRange();
var textInputRange = this.createTextRange();
var precedingRange = this.createTextRange();
var bookmark = selectionRange.getBookmark();
textInputRange.moveToBookmark(bookmark);
precedingRange.setEndPoint("EndToStart", textInputRange);
start = precedingRange.text.length;
end = start + selectionRange.text.length;

this.value = val.slice(0, start) + mappedChar + val.slice(end);
start++;

// Move the caret
textInputRange = this.createTextRange();
textInputRange.collapse(true);
textInputRange.move("character", start - (this.value.slice(0, start).split("\r\n").length - 1));
textInputRange.select();
}

return false;
}
};

关于javascript - 显示与谷歌浏览器中键入的不同的键盘字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3579219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com