globalShortcut.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { invokeTauriCommand } from './helpers/tauri.js';
  2. import { transformCallback } from './tauri.js';
  3. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  4. // SPDX-License-Identifier: Apache-2.0
  5. // SPDX-License-Identifier: MIT
  6. /**
  7. * Register global shortcuts.
  8. *
  9. * 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`.
  10. *
  11. * The APIs must be added to [`tauri.allowlist.globalShortcut`](https://tauri.app/v1/api/config/#allowlistconfig.globalshortcut) in `tauri.conf.json`:
  12. * ```json
  13. * {
  14. * "tauri": {
  15. * "allowlist": {
  16. * "globalShortcut": {
  17. * "all": true // enable all global shortcut APIs
  18. * }
  19. * }
  20. * }
  21. * }
  22. * ```
  23. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  24. * @module
  25. */
  26. /**
  27. * Register a global shortcut.
  28. * @example
  29. * ```typescript
  30. * import { register } from '@tauri-apps/api/globalShortcut';
  31. * await register('CommandOrControl+Shift+C', () => {
  32. * console.log('Shortcut triggered');
  33. * });
  34. * ```
  35. *
  36. * @param shortcut Shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
  37. * @param handler Shortcut handler callback - takes the triggered shortcut as argument
  38. *
  39. * @since 1.0.0
  40. */
  41. async function register(shortcut, handler) {
  42. return invokeTauriCommand({
  43. __tauriModule: 'GlobalShortcut',
  44. message: {
  45. cmd: 'register',
  46. shortcut,
  47. handler: transformCallback(handler)
  48. }
  49. });
  50. }
  51. /**
  52. * Register a collection of global shortcuts.
  53. * @example
  54. * ```typescript
  55. * import { registerAll } from '@tauri-apps/api/globalShortcut';
  56. * await registerAll(['CommandOrControl+Shift+C', 'Ctrl+Alt+F12'], (shortcut) => {
  57. * console.log(`Shortcut ${shortcut} triggered`);
  58. * });
  59. * ```
  60. *
  61. * @param shortcuts Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
  62. * @param handler Shortcut handler callback - takes the triggered shortcut as argument
  63. *
  64. * @since 1.0.0
  65. */
  66. async function registerAll(shortcuts, handler) {
  67. return invokeTauriCommand({
  68. __tauriModule: 'GlobalShortcut',
  69. message: {
  70. cmd: 'registerAll',
  71. shortcuts,
  72. handler: transformCallback(handler)
  73. }
  74. });
  75. }
  76. /**
  77. * Determines whether the given shortcut is registered by this application or not.
  78. * @example
  79. * ```typescript
  80. * import { isRegistered } from '@tauri-apps/api/globalShortcut';
  81. * const isRegistered = await isRegistered('CommandOrControl+P');
  82. * ```
  83. *
  84. * @param shortcut Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
  85. *
  86. * @since 1.0.0
  87. */
  88. async function isRegistered(shortcut) {
  89. return invokeTauriCommand({
  90. __tauriModule: 'GlobalShortcut',
  91. message: {
  92. cmd: 'isRegistered',
  93. shortcut
  94. }
  95. });
  96. }
  97. /**
  98. * Unregister a global shortcut.
  99. * @example
  100. * ```typescript
  101. * import { unregister } from '@tauri-apps/api/globalShortcut';
  102. * await unregister('CmdOrControl+Space');
  103. * ```
  104. *
  105. * @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
  106. *
  107. * @since 1.0.0
  108. */
  109. async function unregister(shortcut) {
  110. return invokeTauriCommand({
  111. __tauriModule: 'GlobalShortcut',
  112. message: {
  113. cmd: 'unregister',
  114. shortcut
  115. }
  116. });
  117. }
  118. /**
  119. * Unregisters all shortcuts registered by the application.
  120. * @example
  121. * ```typescript
  122. * import { unregisterAll } from '@tauri-apps/api/globalShortcut';
  123. * await unregisterAll();
  124. * ```
  125. *
  126. * @since 1.0.0
  127. */
  128. async function unregisterAll() {
  129. return invokeTauriCommand({
  130. __tauriModule: 'GlobalShortcut',
  131. message: {
  132. cmd: 'unregisterAll'
  133. }
  134. });
  135. }
  136. export { isRegistered, register, registerAll, unregister, unregisterAll };