KeyboardEvent.getModifierState() - Web APIs 编辑

The KeyboardEvent.getModifierState() method returns the current state of the specified modifier key: true if the modifier is active (that is the modifier key is pressed or locked), otherwise, false.

Syntax

var active = event.getModifierState(keyArg);

Returns 

A Boolean

Parameters

keyArg
A modifier key value. The value must be one of the KeyboardEvent.key values which represent modifier keys, or the string "Accel" This deprecated API should no longer be used, but will probably still work.. This is case-sensitive.

Modifier keys on Internet Explorer

IE9 uses "Scroll" for "ScrollLock" and "Win" for "OS".

Modifier keys on Gecko

When getModifierState() returns true on Gecko?
WindowsLinux (GTK)MacAndroid 2.3Android 3.0 or latter
"Alt"Either Alt key or AltGr key pressedAlt key pressed⌥ Option key pressedAlt key or option key pressed
"AltGraph"

Both Alt and Ctrl keys are pressed, or AltGr key is pressed

Level 3 Shift key (or Level 5 Shift key ) pressed⌥ Option key pressedNot supported
"CapsLock"During LED for ⇪ Caps Lock turned onNot supportedWhile CapsLock is locked
"Control"Either Ctrl key or AltGr key pressedCtrl key pressedcontrol key pressedmenu key pressed.Ctrl key, control key or menu key pressed.
"Fn"Not supportedFunction key is pressed, but we're not sure what key makes the modifier state active. Fn key on Mac keyboard doesn't cause this active.
"FnLock"Not supported
"Hyper"Not supported
"Meta"Not supportedMeta key pressed⌘ Command key pressedNot supported⊞ Windows Logo key or command key pressed
"NumLock"During LED for Num Lock turned onA key on numpad pressedNot supportedWhile NumLock is locked
"OS"⊞ Windows Logo key pressedSuper key or Hyper key pressed (typically, mapped to ⊞ Windows Logo key)Not supported
"ScrollLock"During LED for Scroll Lock turned onDuring LED for Scroll Lock turned on, but typically this isn't supported by platformNot supportedWhile ScrollLock is locked
"Shift"⇧ Shift key pressed
"Super"Not supported
"Symbol"Not supported
"SymbolLock"Not supported
  • On the other platforms, "Alt", "Control" and "Shift" may be supported.
  • All modifiers (except "FnLock", "Hyper", "Super" and "Symbol" which are defined after Gecko implements this) are always supported for untrusted events on Gecko. This doesn't depend on the platform.

"Accel" virtual modifier

Note: The "Accel" virtual modifier has been effectively deprecated in current drafts of the DOM3 Events specification.

getModifierState() also accepts a deprecated virtual modifier named "Accel". event.getModifierState("Accel") returns true when at least one of KeyboardEvent.ctrlKey or KeyboardEvent.metaKey is true.

In old implementations and outdated specifications, it returned true when a modifier which is the typical modifier key for the shortcut key is pressed. For example, on Windows, pressing Ctrl key may make it return true. However, on Mac, pressing ⌘ Command key may make it return true. Note that which modifier key makes it return true depends on platforms, browsers, and user settings. For example, Firefox users can customize this with a pref, "ui.key.accelKey".

Example

// Ignore if following modifier is active.
if (event.getModifierState("Fn") ||
    event.getModifierState("Hyper") ||
    event.getModifierState("OS") ||
    event.getModifierState("Super") ||
    event.getModifierState("Win") /* hack for IE */) {
  return;
}

// Also ignore if two or more modifiers except Shift are active.
if (event.getModifierState("Control") +
    event.getModifierState("Alt") +
    event.getModifierState("Meta") > 1) {
  return;
}

// Handle shortcut key with standard modifier
if (event.getModifierState("Accel")) {
  switch (event.key.toLowerCase()) {
    case "c":
      if (event.getModifierState("Shift")) {
        // Handle Accel + Shift + C
        event.preventDefault(); // consume the key event
      }
      break;
    case "k":
      if (!event.getModifierState("Shift")) {
        // Handle Accel + K
        event.preventDefault(); // consume the key event
      }
      break;
  }
  return;
}

// Do somethig different for arrow keys if ScrollLock is locked.
if ((event.getModifierState("ScrollLock") ||
       event.getModifierState("Scroll") /* hack for IE */) &&
    !event.getModifierState("Control") &&
    !event.getModifierState("Alt") &&
    !event.getModifierState("Meta")) {
  switch (event.key) {
    case "ArrowDown":
    case "Down": // hack for IE and old Gecko
      event.preventDefault(); // consume the key event
      break;
    case "ArrowLeft":
    case "Left": // hack for IE and old Gecko
      // Do something different if ScrollLock is locked.
      event.preventDefault(); // consume the key event
      break;
    case "ArrowRight":
    case "Right": // hack for IE and old Gecko
      // Do something different if ScrollLock is locked.
      event.preventDefault(); // consume the key event
      break;
    case "ArrowUp":
    case "Up": // hack for IE and old Gecko
      // Do something different if ScrollLock is locked.
      event.preventDefault(); // consume the key event
      break;
  }
}

Although this example uses .getModifierState() with "Alt", "Control", "Meta" and "Shift", using event.altKey, event.ctrlKey, event.metaKey and event.shiftKey may be more preferable.

Specifications

SpecificationStatusComment
Document Object Model (DOM) Level 3 Events Specification
The definition of 'getModifierState()' in that specification.
ObsoleteInitial definition (Modifier Keys spec)

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:114 次

字数:13089

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文