globalShortcut.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  8. *
  9. * The APIs must be added to [`tauri.allowlist.globalShortcut`](https://tauri.app/v1/api/config/#allowlistconfig.globalshortcut) in `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. * @example
  30. * ```typescript
  31. * import { register } from '@tauri-apps/api/globalShortcut';
  32. * await register('CommandOrControl+Shift+C', () => {
  33. * console.log('Shortcut triggered');
  34. * });
  35. * ```
  36. *
  37. * @param shortcut Shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
  38. * @param handler Shortcut handler callback - takes the triggered shortcut as argument
  39. * @returns
  40. */
  41. async function register(
  42. shortcut: string,
  43. handler: ShortcutHandler
  44. ): Promise<void> {
  45. return invokeTauriCommand({
  46. __tauriModule: 'GlobalShortcut',
  47. message: {
  48. cmd: 'register',
  49. shortcut,
  50. handler: transformCallback(handler)
  51. }
  52. })
  53. }
  54. /**
  55. * Register a collection of global shortcuts.
  56. * @example
  57. * ```typescript
  58. * import { registerAll } from '@tauri-apps/api/globalShortcut';
  59. * await registerAll(['CommandOrControl+Shift+C', 'Ctrl+Alt+F12'], (shortcut) => {
  60. * console.log(`Shortcut ${shortcut} triggered`);
  61. * });
  62. * ```
  63. *
  64. * @param shortcuts Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
  65. * @param handler Shortcut handler callback - takes the triggered shortcut as argument
  66. * @returns
  67. */
  68. async function registerAll(
  69. shortcuts: string[],
  70. handler: ShortcutHandler
  71. ): Promise<void> {
  72. return invokeTauriCommand({
  73. __tauriModule: 'GlobalShortcut',
  74. message: {
  75. cmd: 'registerAll',
  76. shortcuts,
  77. handler: transformCallback(handler)
  78. }
  79. })
  80. }
  81. /**
  82. * Determines whether the given shortcut is registered by this application or not.
  83. * @example
  84. * ```typescript
  85. * import { isRegistered } from '@tauri-apps/api/globalShortcut';
  86. * const isRegistered = await isRegistered('CommandOrControl+P');
  87. * ```
  88. *
  89. * @param shortcut Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
  90. * @returns A promise resolving to the state.
  91. */
  92. async function isRegistered(shortcut: string): Promise<boolean> {
  93. return invokeTauriCommand({
  94. __tauriModule: 'GlobalShortcut',
  95. message: {
  96. cmd: 'isRegistered',
  97. shortcut
  98. }
  99. })
  100. }
  101. /**
  102. * Unregister a global shortcut.
  103. * @example
  104. * ```typescript
  105. * import { unregister } from '@tauri-apps/api/globalShortcut';
  106. * await unregister('CmdOrControl+Space');
  107. * ```
  108. *
  109. * @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
  110. * @returns
  111. */
  112. async function unregister(shortcut: string): Promise<void> {
  113. return invokeTauriCommand({
  114. __tauriModule: 'GlobalShortcut',
  115. message: {
  116. cmd: 'unregister',
  117. shortcut
  118. }
  119. })
  120. }
  121. /**
  122. * Unregisters all shortcuts registered by the application.
  123. * @example
  124. * ```typescript
  125. * import { unregisterAll } from '@tauri-apps/api/globalShortcut';
  126. * await unregisterAll();
  127. * ```
  128. *
  129. * @returns
  130. */
  131. async function unregisterAll(): Promise<void> {
  132. return invokeTauriCommand({
  133. __tauriModule: 'GlobalShortcut',
  134. message: {
  135. cmd: 'unregisterAll'
  136. }
  137. })
  138. }
  139. export { register, registerAll, isRegistered, unregister, unregisterAll }