AppJs 键盘快捷键(复制、粘贴、剪贴板、退出、全选...

AppJs keyboard shortcuts (copy, paste, clipboard, quit, select all…)

本文关键字:粘贴 剪贴板 退出 全选 复制 键盘 快捷键 AppJs      更新时间:2023-09-26

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

我有一个菜单栏在工作,但"&"技巧在我的 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选择所有内容...但是我找不到办法...

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

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();
    }
  });
});

拜托,如果你在这个主题上有任何想法,你会非常感激:)

我找到了一种使用"execCommand"使键盘快捷键工作的方法。

在"ready"事件中,我只是添加了命令,如下所示:

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();
    }
  });
});

希望这对某人有所帮助!