zuzu-typ / winput Goto Github PK
View Code? Open in Web Editor NEWwinput Python extention - capture and send keyboard and mouse input on Windows
License: zlib License
winput Python extention - capture and send keyboard and mouse input on Windows
License: zlib License
Hi @Zuzu-Typ ,
winput.move_mouse(dx, dy) moves mouse way more than it should, with set_DPI_aware(per_monitor=True).
Here's a simple script to test it:
first_move = 10
second_move = 10
winput.set_DPI_aware(per_monitor=True)
mouse_pos = winput.get_mouse_pos()
def move_and_measure(delta: int) -> None:
nonlocal mouse_pos
winput.move_mouse(delta, delta)
new_mouse_pos = winput.get_mouse_pos()
print(
f"Mouse moved dx={new_mouse_pos[0] - mouse_pos[0]}, dy={new_mouse_pos[1] - mouse_pos[1]}"
)
mouse_pos = new_mouse_pos
time.sleep(0.1)
[move_and_measure(first_move) for _ in range(5)]
print("Setting to 0, 0")
winput.set_mouse_pos(0, 0)
mouse_pos = winput.get_mouse_pos()
[move_and_measure(second_move) for _ in range(5)]
Which results in
Mouse moved dx=19, dy=19
Mouse moved dx=25, dy=25
Mouse moved dx=26, dy=26
Mouse moved dx=25, dy=26
Mouse moved dx=26, dy=25
Setting to 0, 0
Mouse moved dx=26, dy=26
Mouse moved dx=25, dy=25
Mouse moved dx=26, dy=26
Mouse moved dx=25, dy=26
Mouse moved dx=26, dy=25
And if I set moves to 50:
Mouse moved dx=147, dy=148
Mouse moved dx=241, dy=240
Mouse moved dx=240, dy=241
Mouse moved dx=241, dy=240
Mouse moved dx=240, dy=210
Setting to 0, 0
Mouse moved dx=241, dy=240
Mouse moved dx=240, dy=241
Mouse moved dx=241, dy=240
Mouse moved dx=240, dy=241
Mouse moved dx=241, dy=117
Extra weird is the fact that it moves less the first time.
Cheers!
Hi @Zuzu-Typ ,
I wonder if winput can do this, or maybe it can be added relatively easily:
In hook functions for mouse and keyboard return bool, and if false, the event would not propagate further, and will be suppressed for the rest of the system.
I'm basing this on pynput's ability to do so on windows via suppress=True globally, and via win32_event_filter as a callback like I described.
I like winput better for simplicity and responsiveness, but without this ability I can't use it properly.
Hi @Zuzu-Typ ,
I'm trying to use set_mouse_pos and getting a really weird bug.
Supplying it 0 0 works well, but for 100 100 it moves the mouse to 125 125. Same applies to any coords, x=1000 moves it to 1250, 500 to 625 etc.
I see there's not much going on in winput itself, but bug exists and is easily reproducible for me (tried rebooting and shutting down everything else).
Debug confirms that coords that go into winput function are the ones I expect, not 25% higher. I confirmed coords wih ahk tooltip, and also visually - I know x and y max, I see cursor going into the corner at coords less than max, and trying to move to center results in mouse being off center.
just hook_mouse alone and simply store the event.position and the mouse becomes unusably slow.
Hi again @Zuzu-Typ ,
Would be cool if winput could allow to detect current keyboard layout for active window, and (though less relevant) change it.
Here's some code I've found for detection (another) and list of codes.
The idea for me is that I'll create e.g. "en-" and "fr-" keyboard layouts, and then if user wants text in french, I'll use this detect function, and if its language tag isn't "fr-XX", I'll switch until fr- is the language, and after that start typing.
Here switching can be done either by another low-level function, or by combo such as win+space (or user-specified one), so for this it's not actually that important to have a switch functionality.
If you want, I'll translate that table of languages to enum and make and MR with it, so winput can give not just the code but all other data as well.
Hi @Zuzu-Typ ,
This is probably out of scope for winput, but: window tracking and info!
Here's the example that logs window changes. Would be nice to hook window much like keyboard and mouse now and track the changes.
With it also functions to get all opened windows, currently active window.
Hi @Zuzu-Typ ,
found a bug:
alt press (code 164) and altGr(165) pressed don't register via winput. Releases are registered.
Furthermore, when I try to press a key with one of those pressed, the key is also not registered.
Alt itself is not very critical - on hotkey alt+U, I could check alt separately via win32api when U is pressed; but keys not being detected is, as it disables any hotkeys with alt essentially.
Here's the test code:
import winput
def keyboard_callback(event: winput.KeyboardEvent) -> int:
if event.action == 256: # press ; 257 - release
print(f" pressed VK code = {event.key}")
return 0
winput.hook_keyboard(keyboard_callback)
winput.wait_messages()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.