Open e-r-n-i-e opened 1 year ago
It looks like this bug only appears when a normal Autohotkey script with at least one Hotstring is running.
Example script hello1.ahk
:
::xxx::Hello World{Enter}
If this script is running, Hotstrings in Autohotkey.py do not work. If this script is suspended (or exited) the do work fine. A normal Autohotkey script without any Hotstring does not cause this error.
I hope this helps to fix this bug.
Hello @e-r-n-i-e!
I believe here's what happens:
You should get the same result if instead of AutoHotkey.py you run the following regular AutoHotkey script alongside hello1.ahk
:
:SI:hw::Hello World
Here are a few workarounds you could do:
mode="event"
to the hotstring call.Other options that require changes in AutoHotkey.py:
BTW I didn't think about using the constants like CTRL = '^'
. This is cool, thanks!
You are right, I get the same result using :SI:hw::Hello World
in normal Autohotkey.
But I still would call it a bug, because it does not work as (I) expected. I would expect the following scripts to behave equally even when they are running both at the same time (or with another script using keyboard hooks):
hello.ahk
MsgBox % "Autohotkey " . A_AhkVersion . "`nSendMode = " . A_SendMode
; Output:
; Autohotkey 1.1.33.10
; SendMode = Event
SendMode Input
MsgBox % "SendMode = " . A_SendMode
; Output:
; SendMode = Input
::hwa::Hello World (AHK, mode unset)
; always works, even with multiple scripts using keyboard hooks
:SE:hwae::Hello World (AHK, mode=event)
; always works, even with multiple scripts using keyboard hooks
:SI:hwai::Hello World (AHK, mode=input)
; does NOT work if there is another script using a keyboard hook
:SP:hwap::Hello World (AHK, mode=play)
; NEVER works (tested in Windows Notepad and Visual Studio Code)
hello.py
print("SendMode =", ahkpy.Settings.send_mode)
# Output:
# SendMode = input
# ::hwa::Hello World (AHK, mode unset)
ahkpy.hotstring("hwp", "Hello World (Python, mode unset)")
# [BUG] SHOULD always work, even with multiple scripts using keyboard hooks, but does NOT
# :SE:hwae::Hello World (AHK, mode=event)
ahkpy.hotstring("hwpe", "Hello World (Python, mode=event)", mode='event')
# [OK] always works, even with multiple scripts using keyboard hooks
# :SI:hwai::Hello World (AHK, mode=input)
ahkpy.hotstring("hwpi", "Hello World (Python, mode=input)", mode='input')
# [OK] does NOT work if there is another script using a keyboard hook
# :SP:hwap::Hello World (AHK, mode=play)
ahkpy.hotstring("hwpp", "Hello World (Python, mode=play)", mode='play')
# [OK] NEVER works (tested in Windows Notepad and Visual Studio Code)
This behaviour is caused by class Hotstring
always setting options even if called without options. You might think that this should not be a problem as it will set the default options, but SI
is not a default option.
The Autohotkey documentation says:
If none of the above options [SI, SE, SP] are used, the default mode in v1.0.43 and later is SendInput. However, unlike the SI option, SendEvent is used instead of SendPlay when SendInput is unavailable. Source: https://www.autohotkey.com/docs/v1/Hotstrings.htm#Options
I was able to get the expected behaviour for hotstrings without the SI
option with a quick hack. I simply disabled line 311
in hotstring.py
:
[...]
if mode == "input":
pass #options.append("SI")
[...]
I leave it to you to find the right spot to really fix this. ;-)
When I start the following script with
py -m ahkpy hello-world.py
the hotkey[Ctrl+Shift+H]
works as expected but both hotstrings don't. When typinghw
followed by a space the space is ommited but nothing else happens. When typingwtf
followed by a space the space is replaced byHello World
which results inwtfHello World
.hello-world.py:
I'm using Autohotkey 1.1.33.10 and Python 3.11.1 on Windows 10.