0

我只是使用 Node.js 和 Angular.js 构建了一个 AppJs 应用程序,但我无法使键盘快捷键正常工作。

我有一个菜单栏工作,但“&”技巧在我的 Mac 上不起作用:

  var menubar = appjs.createMenu([{
     label:'&File',
     submenu:[{
        label:'&Quit',
        action: function(){
          window.close();
        }
      }]
    },
    {
      label:'&Window',
      submenu:[
        {
          label:'&Fullscreen',
          action:function(item) {
            window.frame.fullscreen();
            console.log(item.label+" called.");
          }
        },
        {
          label:'&Minimize',
          action:function(){
            window.frame.minimize();
          }
        },
        {
          label:'Maximize',
          action:function(){
            window.frame.maximize();
          }
        },
        {
          label:''//separator
        },
        {
          label:'Restore',
          action:function(){
            window.frame.restore();
          }
        }
      ]
    }
  ]);

我正在尝试做的另一件事是允许使用 CMD+C、CMD+V 和 CMD+A 进行复制/粘贴和全选……但我找不到这样做的方法……</p>

我的“就绪”事件(服务器端)中有这段代码,女巫捕获键盘事件,但我不知道如何处理它们:(

window.on('ready', function(){
  window.require = require;
  window.process = process;
  window.module = module;
  window.addEventListener('keydown', function(e){
    // SELECT ALL (CMD+A)
    if (e.keyCode == 65) {
      console.log('SELECT ALL');
    }
    // COPY (CMD+C)
    if (e.keyCode == 67) {
      console.log('COPY');
    }
    // PASTE (CMD+V)
    if (e.keyCode == 86) {
      console.log('PASTE');
    }
    if (e.keyIdentifier === 'F12' || e.keyCode === 74 && e.metaKey && e.altKey) {
      window.frame.openDevTools();
    }
  });
});

请,如果您对这个主题有任何了解,您将不胜感激:)

4

1 回答 1

0

I found a way to make the keyboard shortcuts work using "execCommand".

In the "ready" event, I just added the commands, as following:

window.on('ready', function(){
  window.require = require;
  window.process = process;
  window.module = module;
  window.addEventListener('keydown', function(e){
    // console.log(e.keyCode);
    // SELECT ALL (CMD+A)
    if (e.keyCode == 65) {
      window.document.execCommand('selectAll');
    }
    // COPY (CMD+C)
    if (e.keyCode == 67) {
      window.document.execCommand('copy');
    }
    // EXIT (CMD+M)
    if (e.keyCode == 77) {
      window.frame.minimize();
    }
    // EXIT (CMD+Q or CMD+W)
    if (e.keyCode == 81 || e.keyCode == 87) {
      window.close();
    }
    // PASTE (CMD+V)
    if (e.keyCode == 86) {
      window.document.execCommand('paste');
    }
    // CUT (CMD+X)
    if (e.keyCode == 88) {
      window.document.execCommand('cut');
    }
    if (e.keyIdentifier === 'F12' || e.keyCode === 74 && e.metaKey && e.altKey) {
      window.frame.openDevTools();
    }
  });
});

Hope this help someone!

于 2015-01-29T11:43:48.847 回答