spyoungtech/ahk

ahk.key_state("XButton2") does not return true in V2

Closed this issue · 14 comments

describe your issue

Trying to implement this

XButton2::
    {
        While GetKeyState("XButton2", "p") {
            SendEvent "{WheelUp}"
            Sleep 10 
        }
    }

But ahk.key_state("XButton2") never returns true

ahk.version

4.11.0

AutoHotkey version

v2

Code to reproduce the issue

from ahk import AHK

ahk = AHK(executable_path='C:\\Users\\trajano\\AppData\\Local\\Programs\\AutoHotkey\\v2\\AutoHotkey64.exe',
    version='v2')

def scroll_up():
    print("it goes here")
    #  also tried mode='P'
    while ahk.key_state("XButton2"):
        print("this does not appear")
        ahk.send_input("{WheelUp}", blocking=False)
        time.sleep(0.010)

ahk.add_hotkey("XButton2", callback=scroll_up)
ahk.start_hotkeys()
ahk.block_forever()

Traceback/Error message

No response

I have a Redragon Mouse which has two side buttons they're normally mapped to Forward and Backward (those are XButton1 and XButton2). Now to prevent wear and general fatigue of using mouse wheel for scrolling I want to map those two buttons as mouse wheel up and down. I also use Ctrl-WheelUp/Down to zoom in and out so I would like to have the same capability, and in addition have a different set of sleep timings when I am doing the zoom.

I also tried this

    ahk.run_script('''
        While GetKeyState("XButton2", "p") {
            SendEvent "{WheelUp}"
            Sleep 10 
        }
        ''')

no luck

Hmm. I will try to test this later and see if I can reproduce or confirm a fix, but my first thought would be to try prepending your hotkey with the ~ modifier so the key's native functionality is not blocked by the hotkey hook.

Yeah, so, when I test this, I can reproduce your problem.

I was able to fix it by making the change I described above:

- ahk.add_hotkey("XButton2", callback=scroll_up)
+ ahk.add_hotkey("~XButton2", callback=scroll_up)

Let me know if that works for you.

It did thanks, do you mind putting that in the documentation somewhere,

Now I just need to figure out how to make it NOT trigger the default behaviour because I want to swap it with wheel up

def scroll_up():
    while ahk.key_state("XButton2"):
        ahk.send_input("{WheelUp}")
        time.sleep(0.010)

def scroll_down():
    while ahk.key_state("XButton1"):
        ahk.send_input("{WheelDown}")
        time.sleep(0.010)

ahk.add_hotkey("~XButton2", callback=scroll_up)
ahk.add_hotkey("~XButton1", callback=scroll_down)
ahk.start_hotkeys()
ahk.block_forever()

These two buttons are the two side buttons of the mouse I am using which defaults to Forward and Backward on the web browser

Reopenning because the bug was about taking over XButton1/2 to replace with a different key. So if I do the ~ it keeps the behaviour as you had stated but I need it NOT to keep the behaviour.

So looking at the definition of ~ in https://www.autohotkey.com/docs/v2/Hotkeys.htm it is performing what you would expect, but it is something that won't work for me. So in the end the problem is ahk.key_state is not returning correctly

I also tried this but no luck due to Duplicate hotkey

def dead_key():
    pass

print(ahk._version)
ahk.add_hotkey("~XButton2", callback=scroll_up)
ahk.add_hotkey("~XButton1", callback=scroll_down)
ahk.add_hotkey("XButton2", callback=dead_key)
ahk.add_hotkey("XButton1", callback=dead_key)

I got it working in the end when I saw the Up keyword in the hotkey

from ahk import AHK
import time

ahk = AHK(
    executable_path="C:\\Users\\trajano\\AppData\\Local\\Programs\\AutoHotkey\\v2\\AutoHotkey64.exe",
    version="v2",
)

scrolling_up = False
scrolling_down = False

def scroll_up():
    global scrolling_up
    scrolling_up = True
    while scrolling_up:
        ahk.send_input("{WheelUp}")
        time.sleep(0.010)


def scroll_up_stop():
    global scrolling_up
    scrolling_up = False


def scroll_down_stop():
    global scrolling_down
    scrolling_down = False


def scroll_down():
    global scrolling_down
    scrolling_down = True
    while scrolling_down:
        ahk.send_input("{WheelDown}")
        time.sleep(0.010)

ahk.add_hotkey("XButton2", callback=scroll_up)
ahk.add_hotkey("XButton1", callback=scroll_down)
ahk.add_hotkey("XButton2 Up", callback=scroll_up_stop)
ahk.add_hotkey("XButton1 Up", callback=scroll_down_stop)
ahk.start_hotkeys()
ahk.block_forever()

But it seems a bit hacky and I'd rather just use key_state

In addition when I do

def scroll_up():
    global scrolling_up
    scrolling_up = True
    while scrolling_up:
        print(ahk.key_state("LCtrl"))
        # ahk.send_input("{WheelUp}")
        time.sleep(0.010)


ahk.add_hotkey("*XButton2", callback=scroll_up)
ahk.add_hotkey("*XButton2 Up", callback=scroll_up_stop)

The process of sending {WheelUp} seems to break the key state check. That may mean there's a concurrency issue with get_key_state.

The process of sending {WheelUp} seems to break the key state check.

@trajano If you mean the key state of a modifier key like LCtrl, that is due to an AutoHotkey behavior that affects the key state of modifier keys before sending input. You can address this by using blind mode which disables those behaviors. Specifically:

The Blind mode avoids releasing the modifier keys (Alt, Ctrl, Shift, and Win) if they started out in the down position, unless the modifier is excluded. For example, the hotkey +s::Send "{Blind}abc" would send ABC rather than abc because the user is holding down Shift.

        print(ahk.key_state("LCtrl"))
        ahk.send_input("{Blind}{WheelUp}")

Without {Blind} mode being on, the result is inconsistent because ahk.key_state may sometimes run before AutoHotkey has had a chance to properly restore the modifier key to its original state. So you get inconsistent output even if the key is physically held down the whole time:

1
0
0
1
0
1
...

With blind mode on, AHK never releases the modifier key to begin with, so the result is then consistent while the key is physically held down:

1
1
1
1
1
1
...

Alternatively, depending on what behavior you want from this, you can use another method, like using key_wait which will wait for AHK to restore the modifier key, though this may slow your loop down a little bit:

def scroll_up():
    global scrolling_up
    scrolling_up = True
    while scrolling_up:
        print(ahk.key_wait("LCtrl", timeout=0.3))
        ahk.send_input("{WheelUp}")
        time.sleep(0.010)

I might be able to help you come up with a better solution, but I'm not sure for what purpose you are using this key_state check.

I'll add it to the OP but basically it's this

I have a Redragon Mouse which has two side buttons they're normally mapped to Forward and Backward (those are XButton1 and XButton2). Now to prevent wear and general fatigue of using mouse wheel for scrolling I want to map those two buttons as mouse wheel up and down. I also use Ctrl-WheelUp/Down to zoom in and out so I would like to have the same capability, and in addition have a different set of sleep timings when I am doing the zoom.

Also

    print(ahk.key_wait("LCtrl", timeout=0.3)) 

is not valid as timeout is an integer according to the typings.

Given your suggestions though, I ended up with this and seems to work

def scroll_up():
    global scrolling_up
    scrolling_up = True
    while scrolling_up:
        ahk.send_input("{Blind}{WheelUp}")
        if ahk.key_state("LCtrl"):
            time.sleep(0.300)
        else:
            time.sleep(0.010)

Glad you were able to get something that works for you.

is not valid as timeout is an integer according to the typings.

You're right. The type hint is not quite correct since AutoHotkey will also accept a float. I will fix this in an upcoming release.

I'm closing this off, I do have a work around and the more I look at it, the more I think it's a limitation on the architecture working with AHK and nothing can really be done on the python end.