shortcut.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. const { globalShortcut } = require('electron');
  3. const storage = require('./storage');
  4. /**
  5. * 安装模块
  6. */
  7. exports.setup = function () {
  8. // default
  9. console.log('[electron-lib-shortcut] [setup]');
  10. storage.iniPreferences();
  11. }
  12. /**
  13. * 快捷键注册
  14. * @param {Object} shortcutObj - shortcut object
  15. * @param {Boolean} force - force register
  16. * @param {Function} fn - callback
  17. * @return {Boolean}
  18. */
  19. exports.register = function (shortcutObj, force = true, fn) {
  20. if (!shortcutObj['id'] || !shortcutObj['name'] || !shortcutObj['cmd']) {
  21. return false;
  22. }
  23. const isRegistered = this.isRegistered(shortcutObj['cmd']);
  24. if (isRegistered && !force) {
  25. return false;
  26. }
  27. storage.setShortcuts(shortcutObj);
  28. globalShortcut.register(shortcutObj['cmd'], fn)
  29. return true;
  30. }
  31. /**
  32. * 快捷键是否注册成功
  33. * @param {String} cmd - shortcut string
  34. * @return {Boolean}
  35. */
  36. exports.isRegistered = function (cmd) {
  37. return globalShortcut.isRegistered(cmd)
  38. }
  39. /**
  40. * 注销全局快捷键
  41. * @param {String} cmd - shortcut string
  42. * @return {Boolean}
  43. */
  44. exports.unregister = function (cmd) {
  45. globalShortcut.unregister(cmd)
  46. }
  47. exports = module.exports;