使用tab键在javascript中触发事件

时间:2017-07-18 06:45:16

标签: javascript

我正在使用空格键翻译使用Google API输入的文字。我希望使用tab键实现相同的任务。

<script>
function OnLoad() {
  var options = {
    sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH,
    destinationLanguage: [google.elements.transliteration.LanguageCode.HINDI],
    shortcutKey: 'ctrl+g',
    transliterationEnabled: true
  };

  var control = new google.elements.transliteration.TransliterationControl(options);
  control.makeTransliteratable(["txtHindi"]);
  var keyVal = 32; // Space key
  //var keyTab = 9;  //tab key
  $("#txtEnglish").on('keydown', function(event) {
    if (event.keyCode === 32 || event.keyCode === 9) {
      var engText = $("#txtEnglish").val() + " ";
      var engTextArray = engText.split(" ");
      $("#txtHindi").val($("#txtHindi").val() + engTextArray[engTextArray.length - 2]);
      document.getElementById("txtHindi").focus();
      $("#txtHindi").trigger({
        type: 'keypress',
        keyCode: keyVal,
        which: keyVal,
        charCode: keyVal
      });
    }
    if (event.keyCode == 8) {
      $("#txtHindi").val("");
      $("#txtEnglish").val("");
      return false;
    }
  });

  $("#txtHindi").bind("keyup", function(event) {
    setTimeout(function() {
      $("#txtEnglish").val($("#txtEnglish").val() + " ");
      document.getElementById("txtEnglish").focus()
    }, 0);
  });
} //end onLoad function

google.setOnLoadCallback(OnLoad);

0 个答案:

没有答案