modifiers.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. export type ModifierKey = 'shift' | 'command' | 'control' | 'option'
  2. let prewarmed = false
  3. /**
  4. * Pre-warm the native module by loading it in advance.
  5. * Call this early to avoid delay on first use.
  6. */
  7. export function prewarmModifiers(): void {
  8. if (prewarmed || process.platform !== 'darwin') {
  9. return
  10. }
  11. prewarmed = true
  12. // Load module in background
  13. try {
  14. // eslint-disable-next-line @typescript-eslint/no-require-imports
  15. const { prewarm } = require('modifiers-napi') as { prewarm: () => void }
  16. prewarm()
  17. } catch {
  18. // Ignore errors during prewarm
  19. }
  20. }
  21. /**
  22. * Check if a specific modifier key is currently pressed (synchronous).
  23. */
  24. export function isModifierPressed(modifier: ModifierKey): boolean {
  25. if (process.platform !== 'darwin') {
  26. return false
  27. }
  28. try {
  29. // Dynamic import to avoid loading native module at top level
  30. const { isModifierPressed: nativeIsModifierPressed } =
  31. // eslint-disable-next-line @typescript-eslint/no-require-imports
  32. require('modifiers-napi') as { isModifierPressed: (m: string) => boolean }
  33. return nativeIsModifierPressed(modifier)
  34. } catch {
  35. return false
  36. }
  37. }