Hook Mouse clicks and intercept them - python

I am currently trying to stop mouse clicks while my Script is running and still work with them.
If you are confused about the use case I will elaborate at the end of the question.
Currently I can get clicks like this:
import mouse
import time
def mouseHook(event):
if type(event) == mouse.ButtonEvent:
print(event)
mouse.hook(mouseHook)
while 1:
time.sleep(0.25)
But this still lets the clicks go through, how would I intercept them?
use case: simulate a monitor and while mouse is on that monitor send all movements, clicks and keypresses to MacBook (similar to Synergy, Mouse without Borders or Share Mouse)

The hook used by the application only 'hooks' into the process, which means it gets information from it, but can't insert or modify it's code.
For reference on windows that would use (https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644988(v=vs.85))
The easiest way would be to have your application ignore the mouseclicks.

Related

send mouse/keyboard inputs to active/inactive windows

I use python and pynput to automate my mouse.
But obviously, it is impossible to use the computer at the same time for other things. So I'm looking for a solution to either automate a "second" virtual mouse or to just send mouse clicks to a specific window (active or inactive) on Windows 10 without actually using the real mouse.
You can use pyautogui to automate keyboard and mouse actions; but if you are using the keyboard and mouse it will interfere with these commands. the same could be said about adding another mouse; it doesn't add a second OS pointer; it only creates another (at times conflicting) control over that pointer.

How can I disable the mouse for a given amount of time?

I'm developing a rather big GUI with Tkinter for my cocktail-robot and now I'm trying to disable the mouse for a few seconds, so that no click on the screen will cause any event.
The reason why I need to do this is that the programm crashes if there're to much incoming events. Because I'm using time.sleep() for a few seconds, Windows thinks that the programm crashed and asks whether it should be closed.
Is there any possibilty to do that? Can I just bind the whole screen to "< Button-1 >"
Btw. the GUI is based on this structure:
Switch between two frames in tkinter
I'm looking forward to your help! Thanks

Use Python win32api module to control Steam games

I am making a gyro mouse. The driver script reads sensor input and moves mouse accordingly with win32api commands.
win32api.SetCursorPos((xStart-int(dh*xsensitivity),ypos))
When I open a full screen game such as Counter Strike Source, the mouse wont work at all. Only the click inputs function, but they cause to gun to point straight down and perform a kind of seizure.
Is there some way I can interface with whatever controls the mouse inside the game?
I'm not terribly familiar with Windows programming, but my best guess is that the video game (Counter Strike) is using the DirectInput (from DirectX) methods to read mouse travel. That is, it's using DirectInput to get mouse motion events, and the Python win32api.SetCursorPos is "warping" the cursor to the given location and not generating ANY intermediate movement messages.
You'll most likely need to use the MS Win32 API call SendInput to construct mouse movement messages and push them into the event queue at the OS level.
If you're familiar with .NET technologies, you might try using IronPython (a Python interpreter that can interact with the .NET runtime). In such case, the Input Simulator project at Codeplex has methods defined for pushing mouse movement events into the input queue.

How do I listen to mouse position in Python using the module Xlib

I know how to get the mouse position in Python with this code calling display.
from Xlib import display
data = display.Display().screen().root.query_pointer()._data
x = data["root_x"]
y = data["root_y"]
but I have an idea for an application that requires me to act on the mouse when it gets close to the edge of the screen, and its not practical to constantly query those functions, and I can't seem to find any type of mouse listener which would be ideal.
Do you have any idea how to either listen to the mouse events in real-time or another better way to accomplish this task?

Creating a new input event dispatcher in Pyglet (infra red input)

I recently asked this question in the pyglet-users group, but got response, so I'm trying here instead.
I would like to extend Pyglet to be able to use an infra red input device supported by lirc. I've used pyLirc before ( http://pylirc.mccabe.nu/ ) with PyGame and I want to rewrite my application to use Pyglet instead.
To see if a button was pressed you would typically poll pyLirc to see if there is any button presses in its queue.
My question is, what is the correct way in Pyglet to integrate pyLirc?
I would prefer if it works in the same was as the current window keyboard/mouse events, but I'm not sure where to start.
I know I can create a new EventDispatcher, in which I can register the
new types of events and dispatch them after polling, like so:
class pyLircDispatcher(pyglet.event.EventDispatcher):
def poll(self):
codes = pylirc.nextcode()
if codes is not None:
for code in codes:
self.dispatch_event('on_irbutton', code)
def on_irbutton(self, code):
pass
But how do I integrate that into the application's main loop to keep on calling poll() if I use pyglet.app.run() and how do I attach this eventdispatcher to my window so it works the same as the mouse and keyboard dispatchers?
I see that I can set up a scheduler to call poll() at regular intervals with pyglet.clock.schedule_interval, but is this the correct way to do it?
It's probably too late for the OP, but I'll reply anyway in case it's helpful to anyone else.
Creating the event dispatcher and using pyglet.clock.schedule_interval to call poll() at regular intervals is a good way to do it.
To attach the event dispatcher to your window, you need to create an instance of the dispatcher and then call its push_handlers method:
dispatcher.push_handlers(window)
Then you can treat the events just like any other events coming into the window.
The correct way is whatever works. You can always change it later if you find a better way.

Categories