spyoungtech / ahk

Python wrapper for AutoHotkey with full type support. Harness the automation power of AutoHotkey with the beauty of Python.
MIT License
887 stars 66 forks source link

How to implement the behavior of the script above? #240

Closed xx299x closed 11 months ago

xx299x commented 1 year ago
e::
e::e
i::
msgBox i 
return
o::
msgBox o
return

How to implement the behavior of the script above? When pressing 'e,' it functions as a regular 'e,' but when 'e' is combined with 'i' or 'o,' it performs their respective actions.

This is a very useful approach. When developing a keyboard shortcut system, considering AHK's peculiar syntax, I'd like to rewrite my shortcut system in Python.

spyoungtech commented 1 year ago

Hmm. I'm not sure I fully understand this example.

The way I understand this, you've done three things in the shared AutoHotkey code:

  1. Add a remapping e to e (which seems to have no real practical effect)
  2. Add a remapping that triggers an action for the character i
  3. Add a remapping that triggers an action for the character o

Right now, AutoHotkey's "remapping" feature isn't directly implemented. But hotstrings with the * modifier (not requiring ending keys) should have a similar effect.

So, this Python code using hotstrings has the same effects as the code you shared:

from ahk import AHK
ahk = AHK()

def i_action():
    ahk.msg_box('i')
def o_action():
    ahk.msg_box('o')

ahk.add_hotstring('e', 'e', options='*')
ahk.add_hotstring('i', i_action, options='*')
ahk.add_hotstring('o', o_action, options='*')

ahk.start_hotkeys()
ahk.block_forever()

When I test this Python code and the AHK code you provided, the effect seems to be identical.