javascript 使用 initKeyboardEvent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11857524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:34:28  来源:igfitidea点击:

Working with initKeyboardEvent

javascripthtmljavascript-events

提问by jason328

I'm trying to find a basic example of the use of initKeyboardEvent(). Ideally, I want to create a keyboard event where when the enter button is pressed when a user is typing in one input field, another input field will be shown using jQuery. Does anyone know where I can find a basic understandable example of using initKeyboardEvent()?

我试图找到一个使用 initKeyboardEvent() 的基本示例。理想情况下,我想创建一个键盘事件,当用户在一个输入字段中输入时按下 Enter 按钮时,将使用 jQuery 显示另一个输入字段。有谁知道我在哪里可以找到使用 initKeyboardEvent() 的基本可理解示例?

回答by RobG

initKeyboardEventwas originallly initKeyEvent, there is a working example at MDN: https://developer.mozilla.org/en-US/docs/DOM/event.initKeyEventand here: http://www.howtocreate.co.uk/tutorials/javascript/domevents.

initKeyboardEvent最初是initKeyEvent,MDN 上有一个工作示例:https: //developer.mozilla.org/en-US/docs/DOM/event.initKeyEvent和这里:http: //www.howtocreate.co.uk/tutorials /javascript/domevents

I'm sure you can find many examples through a web search: "javascript initkeyevent" and "javascript initKeyboardEvent". Likely you will need to test for support to determine which to use.

我相信您可以通过网络搜索找到许多示例:“javascript initkeyevent”和“javascript initKeyboardEvent”。您可能需要测试支持以确定使用哪种支持。

Note also that initKeyboardEventwill be deprecated in DOM 4 (draft)in favour of Event constructor syntax, suggested in DOM 3and implemented in DOM 4 (draft).

另请注意,initKeyboardEvent将在DOM 4 (draft)中弃用,以支持 Event 构造函数语法,在DOM 3 中建议并在DOM 4 (draft) 中实现

A simple feature test might be something like:

一个简单的功能测试可能类似于:

// Test for Event constructor
if (typeof Event != 'undefined') {
  var x, evt;
  try {
    x = new Event('keyboardEvent');
  } catch(e) {
    x = void 0;
  }
}

// test to see which method to use
if (typeof x != 'undefined') {
  alert('Use Event constructor');

} else if (document.createEvent) {
  evt = document.createEvent('KeyboardEvent');

  if (evt && evt.initKeyboardEvent) {
    alert('Use initKeyboardEvent');

  } else if (evt && evt.initKeyEvent) {
    alert('Use initKeyEvent');
  }
}

But for the time being it will likely be best to test for and use DOM 2 createEventwith initKeyEventor initKeyboardEventas appropriate.

但暂时它可能会是最好的测试和使用DOM 2 createEventinitKeyEventinitKeyboardEvent适当。

Note that Firefox 14 doesn't seem to support initKeyboardEventbut does support the Eventconstructor and initKeyEvent, IE 9 is the opposite and doesn't support initKeyEventor the Eventconstructor, but does support initKeyboardEvent.

请注意,Firefox 14 似乎不支持initKeyboardEvent但支持Event构造函数和initKeyEvent,IE 9 则相反,不支持initKeyEventEvent构造函数,但支持initKeyboardEvent

回答by termi

See my answer here

在这里看到我的答案

In short:

简而言之:

Here Cross-browser initKeyboardEvent

这里是跨浏览器的initKeyboardEvent

Here Keyboard Event Level 3 polyfill

Here Keyboard Event Level 3 polyfill

Example:

例子:

var a = new KeyboardEvent("keypress", {"key": 1, "char": "!", shiftKey: true})
alert(a.type + " | " + a.key + " | " + a.char + " | " + a.shiftKey)