如何通过JavaScript捕获Mac的Cmd键?


当前回答

下面是我在AngularJS中如何做到的

app = angular.module('MM_Graph')

class Keyboard
  constructor: ($injector)->
    @.$injector  = $injector
    @.$window    = @.$injector.get('$window')                             # get reference to $window and $rootScope objects
    @.$rootScope = @.$injector.get('$rootScope')

  on_Key_Down:($event)=>
    @.$rootScope.$broadcast 'keydown', $event                             # broadcast a global keydown event

    if $event.code is 'KeyS' and ($event.ctrlKey or $event.metaKey)       # detect S key pressed and either OSX Command or Window's Control keys pressed
      @.$rootScope.$broadcast '', $event                                  # broadcast keyup_CtrS event
      #$event.preventDefault()                                             # this should be used by the event listeners to prevent default browser behaviour

  setup_Hooks: ()=>
    angular.element(@.$window).bind "keydown", @.on_Key_Down              # hook keydown event in window (only called once per app load)
    @

app.service 'keyboard', ($injector)=>
  return new Keyboard($injector).setup_Hooks()

其他回答

基于Ilya的数据,我编写了一个香草JS库,用于在Mac上支持修改器键:https://github.com/MichaelZelensky/jsLibraries/blob/master/macKeys.js

就像这样用吧,例如:

document.onclick = function (event) {
  if (event.shiftKey || macKeys.shiftKey) {
    //do something interesting
  }
}

在Chrome, Safari, Firefox, Mac上的Opera上进行了测试。请检查是否适合您。

对于使用jQuery的人,有一个很好的插件来处理关键事件:

GitHub上的jQuery热键

对于捕获⌘+S和Ctrl+S,我使用这个:

$(window).bind('keydown.ctrl_s keydown.meta_s', function(event) {
    event.preventDefault();
    // Do something here
});

下面是我在AngularJS中如何做到的

app = angular.module('MM_Graph')

class Keyboard
  constructor: ($injector)->
    @.$injector  = $injector
    @.$window    = @.$injector.get('$window')                             # get reference to $window and $rootScope objects
    @.$rootScope = @.$injector.get('$rootScope')

  on_Key_Down:($event)=>
    @.$rootScope.$broadcast 'keydown', $event                             # broadcast a global keydown event

    if $event.code is 'KeyS' and ($event.ctrlKey or $event.metaKey)       # detect S key pressed and either OSX Command or Window's Control keys pressed
      @.$rootScope.$broadcast '', $event                                  # broadcast keyup_CtrS event
      #$event.preventDefault()                                             # this should be used by the event listeners to prevent default browser behaviour

  setup_Hooks: ()=>
    angular.element(@.$window).bind "keydown", @.on_Key_Down              # hook keydown event in window (only called once per app load)
    @

app.service 'keyboard', ($injector)=>
  return new Keyboard($injector).setup_Hooks()

我发现在最新版本的Safari(7.0: 9537.71)中,如果命令键与另一个键同时按下,就可以检测到它。例如,如果你想检测⌘+x:,你可以检测x键并检查if事件。metaKey设置为true。例如:

var key = event.keyCode || event.charCode || 0;
console.log(key, event.metaKey);

当单独按x时,输出120,false。当按⌘+x时,它将输出120,true

这似乎只适用于Safari浏览器,而不适用于Chrome浏览器

var element = //the DOM element to listen for the key on.
element.onkeyup = function(e) {
   if(e.metaKey) {
      //command key was pressed
   }
}