api-global-shortcut-spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { globalShortcut } from 'electron/main';
  2. import { expect } from 'chai';
  3. import { ifdescribe } from './lib/spec-helpers';
  4. const modifiers = [
  5. 'CmdOrCtrl',
  6. 'Alt',
  7. process.platform === 'darwin' ? 'Option' : null,
  8. 'AltGr',
  9. 'Shift',
  10. 'Super',
  11. 'Meta'
  12. ].filter(Boolean);
  13. const keyCodes = [
  14. ...Array.from({ length: 10 }, (_, i) => `${i}`), // 0 to 9
  15. ...Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i)), // A to Z
  16. ...Array.from({ length: 24 }, (_, i) => `F${i + 1}`), // F1 to F24
  17. ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', ':', ';', ':', '+', '=',
  18. '<', ',', '_', '-', '>', '.', '?', '/', '~', '`', '{', ']', '[', '|', '\\',
  19. '}', '"', 'Plus', 'Space', 'Tab', 'Capslock', 'Numlock', 'Scrolllock',
  20. 'Backspace', 'Delete', 'Insert', 'Return', 'Enter', 'Up', 'Down', 'Left',
  21. 'Right', 'Home', 'End', 'PageUp', 'PageDown', 'Escape', 'Esc', 'PrintScreen',
  22. 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7', 'num8', 'num9',
  23. 'numdec', 'numadd', 'numsub', 'nummult', 'numdiv'
  24. ];
  25. ifdescribe(process.platform !== 'win32')('globalShortcut module', () => {
  26. beforeEach(() => {
  27. globalShortcut.unregisterAll();
  28. });
  29. it('can register and unregister single accelerators', () => {
  30. const singleModifierCombinations = modifiers.flatMap(
  31. mod => keyCodes.map(key => {
  32. return key === '+' ? `${mod}+Plus` : `${mod}+${key}`;
  33. })
  34. );
  35. const doubleModifierCombinations = modifiers.flatMap(
  36. (mod1, i) => modifiers.slice(i + 1).flatMap(
  37. mod2 => keyCodes.map(key => {
  38. return key === '+' ? `${mod1}+${mod2}+Plus` : `${mod1}+${mod2}+${key}`;
  39. })
  40. )
  41. );
  42. const combinations = [...singleModifierCombinations, ...doubleModifierCombinations];
  43. combinations.forEach((accelerator) => {
  44. expect(globalShortcut.isRegistered(accelerator)).to.be.false(`Initially registered for ${accelerator}`);
  45. globalShortcut.register(accelerator, () => { });
  46. expect(globalShortcut.isRegistered(accelerator)).to.be.true(`Registration failed for ${accelerator}`);
  47. globalShortcut.unregister(accelerator);
  48. expect(globalShortcut.isRegistered(accelerator)).to.be.false(`Unregistration failed for ${accelerator}`);
  49. globalShortcut.register(accelerator, () => { });
  50. expect(globalShortcut.isRegistered(accelerator)).to.be.true(`Re-registration failed for ${accelerator}`);
  51. globalShortcut.unregisterAll();
  52. expect(globalShortcut.isRegistered(accelerator)).to.be.false(`Re-unregistration failed for ${accelerator}`);
  53. });
  54. });
  55. it('can register and unregister multiple accelerators', () => {
  56. const accelerators = ['CmdOrCtrl+X', 'CmdOrCtrl+Y'];
  57. expect(globalShortcut.isRegistered(accelerators[0])).to.be.false('first initially unregistered');
  58. expect(globalShortcut.isRegistered(accelerators[1])).to.be.false('second initially unregistered');
  59. globalShortcut.registerAll(accelerators, () => {});
  60. expect(globalShortcut.isRegistered(accelerators[0])).to.be.true('first registration worked');
  61. expect(globalShortcut.isRegistered(accelerators[1])).to.be.true('second registration worked');
  62. globalShortcut.unregisterAll();
  63. expect(globalShortcut.isRegistered(accelerators[0])).to.be.false('first unregistered');
  64. expect(globalShortcut.isRegistered(accelerators[1])).to.be.false('second unregistered');
  65. });
  66. it('does not crash when registering media keys as global shortcuts', () => {
  67. const accelerators = [
  68. 'VolumeUp',
  69. 'VolumeDown',
  70. 'VolumeMute',
  71. 'MediaNextTrack',
  72. 'MediaPreviousTrack',
  73. 'MediaStop', 'MediaPlayPause'
  74. ];
  75. expect(() => {
  76. globalShortcut.registerAll(accelerators, () => {});
  77. }).to.not.throw();
  78. globalShortcut.unregisterAll();
  79. });
  80. });