globalShortcut.cjs 4.0 KB

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