0

I just build an AppJs app using Node.js and Angular.js but I can't manage to make the keyboard shortcuts to work.

I have a menubar working, but the "&" trick doesn't work on my 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();
          }
        }
      ]
    }
  ]);

The other thing I'm trying to do is to allow copy/paste and select all using CMD+C, CMD+V and CMD+A… but I can't find a way to do that…

I have this code in my "ready" event (server side) , witch captures the keyboard events, but I don't know what to do with them :(

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

Please, if you have any light in this subject, you'll be very appreciated :)

Eduardo Russo
  • 4,121
  • 2
  • 22
  • 38

1 Answers1

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!

Eduardo Russo
  • 4,121
  • 2
  • 22
  • 38