Javascript 用Chrome处理夏威夷键盘的Unicode转义序列

Javascript 用Chrome处理夏威夷键盘的Unicode转义序列,javascript,google-chrome,google-chrome-extension,keyboard,Javascript,Google Chrome,Google Chrome Extension,Keyboard,阿罗哈,我正试图修改我在GitHub上找到的Chrome操作系统键盘布局,以便为夏威夷语工作。我已经好几年没有编写Javascript了,即使在那时,我的技能也相当初级 我的问题是,这个键盘是从键盘上只用的单个字符派生出来的,对于夏威夷元音宏字符,我需要使用Unicode转义序列,这会使键盘崩溃。我假设在键盘上调用Unicode之前,我需要以某种方式处理它,我只是不确定在哪里或如何处理它。您将在lut变量中看到转义序列。提前谢谢 /* Copyright 2014 Google Inc

阿罗哈,我正试图修改我在GitHub上找到的Chrome操作系统键盘布局,以便为夏威夷语工作。我已经好几年没有编写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;
});