shortcut.js 1.1 KB

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