globalShortcut.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * Register global shortcuts.
  6. *
  7. * This package is also accessible with `window.__TAURI__.globalShortcut` when `tauri.conf.json > build > withGlobalTauri` is set to true.
  8. *
  9. * The APIs must be allowlisted on `tauri.conf.json`:
  10. * ```json
  11. * {
  12. * "tauri": {
  13. * "allowlist": {
  14. * "globalShortcut": {
  15. * "all": true // enable all global shortcut APIs
  16. * }
  17. * }
  18. * }
  19. * }
  20. * ```
  21. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  22. * @module
  23. */
  24. import { invokeTauriCommand } from './helpers/tauri'
  25. import { transformCallback } from './tauri'
  26. export type ShortcutHandler = (shortcut: string) => void
  27. /**
  28. * Register a global shortcut.
  29. *
  30. * @param shortcut Shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
  31. * @param handler Shortcut handler callback - takes the triggered shortcut as argument
  32. * @returns
  33. */
  34. async function register(
  35. shortcut: string,
  36. handler: ShortcutHandler
  37. ): Promise<void> {
  38. return invokeTauriCommand({
  39. __tauriModule: 'GlobalShortcut',
  40. message: {
  41. cmd: 'register',
  42. shortcut,
  43. handler: transformCallback(handler)
  44. }
  45. })
  46. }
  47. /**
  48. * Register a collection of global shortcuts.
  49. *
  50. * @param shortcuts Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
  51. * @param handler Shortcut handler callback - takes the triggered shortcut as argument
  52. * @returns
  53. */
  54. async function registerAll(
  55. shortcuts: string[],
  56. handler: ShortcutHandler
  57. ): Promise<void> {
  58. return invokeTauriCommand({
  59. __tauriModule: 'GlobalShortcut',
  60. message: {
  61. cmd: 'registerAll',
  62. shortcuts,
  63. handler: transformCallback(handler)
  64. }
  65. })
  66. }
  67. /**
  68. * Determines whether the given shortcut is registered by this application or not.
  69. *
  70. * @param shortcut Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
  71. * @returns A promise resolving to the state.
  72. */
  73. async function isRegistered(shortcut: string): Promise<boolean> {
  74. return invokeTauriCommand({
  75. __tauriModule: 'GlobalShortcut',
  76. message: {
  77. cmd: 'isRegistered',
  78. shortcut
  79. }
  80. })
  81. }
  82. /**
  83. * Unregister a global shortcut.
  84. *
  85. * @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
  86. * @returns
  87. */
  88. async function unregister(shortcut: string): Promise<void> {
  89. return invokeTauriCommand({
  90. __tauriModule: 'GlobalShortcut',
  91. message: {
  92. cmd: 'unregister',
  93. shortcut
  94. }
  95. })
  96. }
  97. /**
  98. * Unregisters all shortcuts registered by the application.
  99. *
  100. * @returns
  101. */
  102. async function unregisterAll(): Promise<void> {
  103. return invokeTauriCommand({
  104. __tauriModule: 'GlobalShortcut',
  105. message: {
  106. cmd: 'unregisterAll'
  107. }
  108. })
  109. }
  110. export { register, registerAll, isRegistered, unregister, unregisterAll }