Reacting to Keyboard Shortcuts

Desktop applications can react to keyboard shortcuts that are triggered by the user. While the ToDesktop Builder UI allows you to use a keyboard shortcut to toggle window visibility, the ToDesktop API opens up a wider range of behaviour.

A user interface checkbox for toggling window visibility

In this short guide, we'll use the API to replicate the behaviour for toggling window visibility. Install the API in your target project:

        npm install @todesktop/client-core

      

With that installed, we'll use the globalShortcut namespace to listen to the CommandOrControl+Shift+. shortcut:

        import { globalShortcut, nativeWindow } from '@todesktop/client-core';

globalShortcut.register('CommandOrControl+Shift+.', async () => {
  // ...
});

      
INFO

Visit Electron's accelerator documentation for a complete list of valid shortcuts.

Inside the body of the function, we'll use the imported nativeWindow namespace to handle changing window visibility.

        import { globalShortcut, nativeWindow } from '@todesktop/client-core';

globalShortcut.register('CommandOrControl+Shift+.', async () => {
  if (await nativeWindow.isVisible()) {
    await nativeWindow.hide();
  } else {
    await nativeWindow.show();
  }
});

      

And that's essentially it! While this was a simple example, you have access to the entire todesktop API to experiment with. For a more complex example, visit our tutorial that uses keyboard shortcuts to extract selected text.