javascript - 在 Chrome 中处理夏威夷键盘的 Unicode 转义序列
问题描述
Aloha,我正在尝试修改我在 GitHub 上找到的 Chrome OS 键盘布局以适用于夏威夷语。我已经好几年没有编写过 Javascript 代码了,即便如此,我的技能也相当初级。
我遇到的问题是,键盘仅源自键盘上使用的单个字符,而对于我们的夏威夷元音长音字符,我需要使用 Unicode 转义序列,这会使键盘崩溃。我假设我需要在键盘上调用 Unicode 之前以某种方式处理它,我只是不确定在哪里或如何做到这一点。您将在 lut 变量中看到转义序列。提前致谢。
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var AltGr = { PLAIN: "plain", ALTERNATE: "alternate" };
var Shift = { PLAIN: "plain", SHIFTED: "shifted" };
var contextID = -1;
var altGrState = AltGr.PLAIN;
var shiftState = Shift.PLAIN;
var lastRemappedKeyEvent = undefined;
var lut = {
"KeyA": { "plain": {"plain": "a", "shifted": "A"}, "alternate": {"plain": "\0101", "shifted":"\0100"}, "code": "KeyA"},
"Quote": { "plain": {"plain": "ə", "shifted": "Ə"}, "alternate": {"plain": "‘", "shifted":""}, "code": "Quote"},
};
chrome.input.ime.onFocus.addListener(function(context) {
contextID = context.contextID;
});
function updateAltGrState(keyData) {
altGrState = (keyData.code == "AltRight") ? ((keyData.type == "keydown") ? AltGr.ALTERNATE : AltGr.PLAIN)
: altGrState;
}
function updateShiftState(keyData) {
shiftState = ((keyData.shiftKey && !(keyData.capsLock)) || (!(keyData.shiftKey) && keyData.capsLock)) ?
Shift.SHIFTED : Shift.PLAIN;
}
function isPureModifier(keyData) {
return (keyData.key == "Shift") || (keyData.key == "Ctrl") || (keyData.key == "Alt");
}
function isRemappedEvent(keyData) {
// hack, should check for a sender ID (to be added to KeyData)
return lastRemappedKeyEvent != undefined &&
(lastRemappedKeyEvent.key == keyData.key &&
lastRemappedKeyEvent.code == keyData.code &&
lastRemappedKeyEvent.type == keyData.type
); // requestID would be different so we are not checking for it
}
chrome.input.ime.onKeyEvent.addListener(
function(engineID, keyData) {
var handled = false;
if (isRemappedEvent(keyData)) {
lastRemappedKeyEvent = undefined;
return handled;
}
updateAltGrState(keyData);
updateShiftState(keyData);
if (lut[keyData.code]) {
var remappedKeyData = keyData;
remappedKeyData.key = lut[keyData.code][altGrState][shiftState];
remappedKeyData.code = lut[keyData.code].code;
if (chrome.input.ime.sendKeyEvents != undefined) {
chrome.input.ime.sendKeyEvents({"contextID": contextID, "keyData": [remappedKeyData]});
handled = true;
lastRemappedKeyEvent = remappedKeyData;
} else if (keyData.type == "keydown" && !isPureModifier(keyData)) {
chrome.input.ime.commitText({"contextID": contextID, "text": remappedKeyData.key});
handled = true;
}
}
return handled;
});
解决方案
推荐阅读
- azure - 具有复制范围 USG 的租户的公共端点不支持机密客户端请求
- c++ - MSYS2 MinGW在构建大型应用程序时奇怪的多重定义boost::exception_detail::clone_impl
- kubernetes - 通过特定用户从远程服务器访问 K8S 主服务器
- sql-server - 在动态 SQL 中使用变量
- javascript - 编辑 Google Doc 然后转换为 PDF 的脚本
- flutter - 根据 Flutter 中的滚动位置更改容器的宽度
- scrapy - 带有“部署失败(400)”的scrapyd-deploy
- python - 如何在python中将行的每个元素转换为列?
- date - 在 DOM 中的日期之间变得不同
- html - h1、h2、h3 标签调整大小不同于 p 标签