0

My background app needs to respond to several (3 or 4) system wide hotkeys.

I'm using the nim autome package (https://github.com/miere43/autome), but it doesn't respond well to the 2nd and 3rd registered hotkey. When pressing the first registered hotkey, the program is responsive, but I have to press the others several times before anything happens.

I think wauto (https://github.com/khchen/wAuto) might be better suited. It's more recent and also because it seems to be easier to use media keys with a modifier as hotkeys.

However the docs list functions for registering/unregistering hotkeys, but not how to check whether they are pressed or how to process the hotkey event. I'm not new to programming, but hardly did any windows specific stuff. How do I process the hotkey presses in nim ?

Here's the autome code:

import autome
import os

var
  hotkeyPrev: Hotkey
  hotkeyNext: Hotkey
  hotkeyStop: Hotkey
  hotkeyEvent: bool = false
  hotkeyRegistered: bool = false
  loopCounter: int = 0

proc hotkeysInit*(): bool =
  try:
    hotkeyNext = registerHotkey(0x43, {modAlt})   # Alt-c
    hotkeyPrev = registerHotkey(0x44, {modAlt})   # Alt-d
    hotkeyStop = registerHotkey(0x45, {modAlt})   # Alt-e
    hotkeyRegistered = true
    return true
  except:
    echo "Hotkey(s) already registered"
    return false

proc hotkeysCheck*(): int =
  # return which hotkey was pressed
  if hotkeyRegistered:
    if waitForHotkey(hotkeyNext, 10):
      echo "Pressed ALT next"
      return +1
    if waitForHotkey(hotkeyPrev, 10):
      echo "Pressed ALT prev"
      return -1
    if waitForHotkey(hotkeyStop, 10):
      echo "Pressed ALT stop"
      return 999999
    #echo "NOTHING PRESSED"
  return 0
  
proc hotkeysExit*() =
  unregisterHotkey(hotkeyNext)
  unregisterHotkey(hotkeyPrev)
  unregisterHotkey(hotkeyStop)
  hotkeyRegistered = false
  echo "Checklist hotkey released"


when ismainModule:
  var flag_hotkeys: bool = false
  echo "Hotkey test: Press Alt-c Alt-d for testing, or Alt-e to stop"
  flag_hotkeys = hotkeysInit()
  while flag_hotkeys:
    let hk = hotkeysCheck()
    if hk > 0:
      echo "Pressed hotkey. Result = ", hk
    if hk > 2:
      flag_hotkeys = false
    sleep(200)
  hotkeysExit()
  quit()

IvanS
  • 31
  • 4

0 Answers0