Giter Club home page Giter Club logo

Comments (14)

spyoungtech avatar spyoungtech commented on August 15, 2024 1

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.

from ahk.

spyoungtech avatar spyoungtech commented on August 15, 2024 1

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.

from ahk.

trajano avatar trajano commented on August 15, 2024

I also tried this

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

no luck

from ahk.

spyoungtech avatar spyoungtech commented on August 15, 2024

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.

from ahk.

trajano avatar trajano commented on August 15, 2024

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

from ahk.

trajano avatar trajano commented on August 15, 2024

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.

from ahk.

trajano avatar trajano commented on August 15, 2024

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)

from ahk.

trajano avatar trajano commented on August 15, 2024

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

from ahk.

trajano avatar trajano commented on August 15, 2024

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.

from ahk.

spyoungtech avatar spyoungtech commented on August 15, 2024

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.

from ahk.

trajano avatar trajano commented on August 15, 2024

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.

from ahk.

trajano avatar trajano commented on August 15, 2024

Also

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

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

from ahk.

trajano avatar trajano commented on August 15, 2024

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)

from ahk.

trajano avatar trajano commented on August 15, 2024

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.

from ahk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.