Comments (14)
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.
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.
I also tried this
ahk.run_script('''
While GetKeyState("XButton2", "p") {
SendEvent "{WheelUp}"
Sleep 10
}
''')
no luck
from ahk.
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.
It did thanks, do you mind putting that in the documentation somewhere,
from ahk.
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.
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.
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.
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.
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.
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.
Also
print(ahk.key_wait("LCtrl", timeout=0.3))
is not valid as timeout is an integer according to the typings.
from ahk.
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.
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)
- Hide tray icon at runtime HOT 3
- Tray menu and tooltips don't appear unless AHK executor is "stimulated" HOT 5
- Window.from_pid() not working HOT 10
- Invalid Index when calling `key_state()` in v2 HOT 2
- After locating a window, unable to send key presses to window. HOT 3
- win_move does not work in AutoHotkey v2 unless width and height are specified
- Ability to use HWND to target controls in Control methods
- detect_hidden_windows does not work as expected in some cases when using AutoHotkey v2
- msg_box crashes with AHK v2 HOT 2
- Tooltip text and title mixed up HOT 2
- Traceback when adding hotkeys after hotkeys are started
- Modify traytip for Hotkey process
- Add numlock and scroll lock set states HOT 3
- ahk.key_down not working as expected HOT 3
- key_state fails when retrieving joystack name
- Wrong Type of keyDelay of send command HOT 4
- `win_get_id` and `win_get_idlast` should be returning integers HOT 3
- To send key press or release event to a ahk window? HOT 2
- Cyrillic characters do not work as a hotstring trigger HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ahk.