Perlence / AutoHotkey.py

Write AutoHotkey scripts in Python.
https://ahkpy.readthedocs.io
BSD 3-Clause "New" or "Revised" License
80 stars 12 forks source link

Keys get stuck sometimes #14

Open trajano opened 2 years ago

trajano commented 2 years ago

I remapped the side buttons of my mouse to the mouse wheel but it sometimes locks.

@ahk.hotkey("XButton2")
def scroll_up():
    while ahk.is_key_pressed("XButton2"): # remains true forever sometimes
        ahk.send("{WheelUp}")
        ahk.sleep(0.010)

@ahk.hotkey("XButton1")
def scroll_down():
    while ahk.is_key_pressed("XButton1"): # remains true forever sometimes
        ahk.send("{WheelDown}")
        ahk.sleep(0.010)

Using is_key_pressed_logical does not work

Perlence commented 2 years ago

I didn't notice keys getting stuck with this script, until I started hammering all CPU cores with another script. The analogous AHK script worked fine under such pressure.

trajano commented 2 years ago

Yes I noticed the same as well. It is when I do a number of other things. But it is hard to pin down.

trajano commented 2 years ago

In your test harness, what would happen if you increased priority or level? Tried it on mine, for most of the day it was ok when I set it to 100 but got stuck again just now. :(

trajano commented 2 years ago

I am going to try a different approach and see if it pans out

@ahk.hotkey("XButton2")
def scroll_up():
    #while ahk.is_key_pressed("XButton2"):
    while not ahk.wait_key_released("XButton2", 0.010):
        ahk.send("{WheelUp}")

@ahk.hotkey("XButton1")
def scroll_down():
    while not ahk.wait_key_released("XButton1", 0.010):
        ahk.send("{WheelDown}")

I also took out priority for now to see if it needs it or not

No luck got stuck still, will try priority=1000 to see if it helps (no luck still)

trajano commented 2 years ago

For now, I'm running two AHKs the Python to do some OpenRGB integration and another one that will do the problematic states

#NoEnv
#InstallKeybdHook
#SingleInstance
SetCapsLockState, AlwaysOff
SetNumLockState, AlwaysOn
CapsLock::return
Launch_Mail::
While GetKeyState("Launch_Mail") {
    Send {LButton}
    Sleep, 100
}
return

Browser_Search::
While GetKeyState("Browser_Search") {
    Send {WheelUp}
    Sleep, 10
}
return

#If !WinActive("ahk_exe Overwatch.exe") and !WinActive("GUNDAM EVOLUTION")
XButton2::
While GetKeyState("XButton2", "p") {
    Send {WheelUp}
    Sleep, 10
}
return

XButton1::
While GetKeyState("XButton1", "p") {
    Send {WheelDown}
    Sleep, 10
}
return
#If