Use Python win32api module to control Steam games - python

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.

Related

How can I send direct input to games, specifically Roblox, using Python?

I've been trying to use Python to send inputs to Roblox, but to no avail. What I know so far is that Roblox requires DirectInput, as most games do.
I've seen people successfully send keystrokes, but haven't seen any example of mouse movements or mouse clicks; to complete my goal, I need to be able to move the mouse, send clicks, as well as keyboard inputs, to an active Roblox window.
I'm currently using Python 3.9, if that's of any help.

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 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?

win32: moving mouse with SetCursorPos vs. mouse_event

Is there any difference between moving the mouse in windows using the following two techniques?
win32api.SetCursorPos((x,y))
vs:
nx = x*65535/win32api.GetSystemMetrics(0)
ny = y*65535/win32api.GetSystemMetrics(1)
win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_MOVE,nx,ny)
Does anything happen differently in the way Windows processes the movements?
I believe that mouse_event works by inserting the events into the mouse input stream where as SetCursorPos just moves the cursor around the screen. I don't believe that SetCursorPos generates any input events either (though I may be wrong).
The practical implications are that when you use SetCursorPos, it just moves the cursor around. Where as when you use mouse_event, it inserts the events in the input stream which will in turn generate input events for any programs that are listening. This has implications with programs that listen for lower level mouse events rather than just cursor clicks; games for instance. Also, if you're using mouse_event to move the cursor around and have cursor/pointer acceleration on, than the resulting mouse motion should be subject to whatever acceleration curves windows is using.

Precise response to tablet/mouse events in Windows

How can I tell Windows not to do unhelpful pre-processing on tablet pen events?
I am programming in Python 2.6, targetting tablet PCs running Windows 7 (though I would like my program to work with little modification on XP with a SMART interactive whiteboard, and for mouse users on Linux/Mac). I've written a program which hooks into the normal Windows mouse events, WM_MOUSEMOVE etc., and writes on a canvas.
The problem is that the mouse messages are being fiddled with before they reach my application. I found that if I make long strokes and pause between strokes then the mouse messages are sent properly. But if I make several rapid short strokes, then something is doing unhelpful pre-processing. Specifically, if I make a down-stroke about 10 pixels long, and then make another downstroke about five pixels to the right of the first, then the second WM_MOUSEDOWN reports that it comes from exactly the same place as the first.
This looks like some sort of pre-processing, perhaps so that naive applications don't get confused about double-clicks. But for my application, where I want very faithful response to rapid gestures, it's unhelpful.
I found a reference to the MicrosoftTabletPenServiceProperty atom, and to CS_DBLCLKS window style, and I turned them both off with the following piece of Python code:
hwnd = self.GetHandle()
tablet_atom = "MicrosoftTabletPenServiceProperty"
atom_ID = windll.kernel32.GlobalAddAtomA(tablet_atom)
windll.user32.SetPropA(hwnd,tablet_atom,1)
currentstyle = windll.user32.GetClassLongA(hwnd, win32con.GCL_STYLE)
windll.user32.SetClassLongA(hwnd, win32con.GCL_STYLE, currentstyle & ~win32con.CS_DBLCLKS)
But it has no effect.
I tried writing a low-level hook for the mouse driver, with SetWindowsHookEx, but it doesn't work -- obviously the mouse messages are being pre-processed even before they are sent to my low-level Windows hook.
I would be very grateful for advice about how to turn off this pre-processing. I do not want to switch to RealTimeStylus -- first because it won't work on Windows XP plus SMART interactive whiteboard, second because I can't see how to use RealTimeStylus in CPython, so I would need to switch to IronPython, and then my code would no longer run on Linux/Mac.
Damon.
For raw mouse messages, you can use WM_INPUT on XP and later. Seven added some touch specific stuff: WM_GESTURE and WM_TOUCH

Categories