win32: moving mouse with SetCursorPos vs. mouse_event - python

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.

Related

How to speed up while loop with PyAutoGui?

I am making a very simple spambot for Discord just for pranking my friends. But the while True: command is very slow. Is there a faster alternative?
import PIL
import pyautogui, time
time.sleep(5)
pyautogui.FAILSAFE = True
while True:
pyautogui.hotkey("command", "v")
pyautogui.press("enter")
if (pyautogui.locateOnScreen("av.png")):
(pyautogui.click(pyautogui.locateCenterOnScreen("av.png")))
From the documentation:
Like the enchanted brooms from the Sorcerer’s Apprentice programmed to keep filling (and then overfilling) the bath with water, a bug in your program could make it go out of control. It’s hard to use the mouse to close a program if the mouse cursor is moving around on its own.
As a safety feature, a fail-safe feature is enabled by default. When a PyAutoGUI function is called, if the mouse is in any of the four corners of the primary monitor, they will raise a pyautogui.FailSafeException. There is a one-tenth second delay after calling every PyAutoGUI functions to give the user time to slam the mouse into a corner to trigger the fail safe.
You can disable this failsafe by setting pyautogui.FAILSAFE = False. I HIGHLY RECOMMEND YOU DO NOT DISABLE THE FAILSAFE.
The tenth-second delay is set by the pyautogui.PAUSE setting, which is 0.1 by default. You can change this value. There is also a pyautogui.DARWIN_CATCH_UP_TIME setting which adds an additional delay on macOS after keyboard and mouse events, since the operating system appears to need a delay after PyAutoGUI issues these events. It is set to 0.01 by default, adding an additional hundredth-second delay.
Therefore, if you want to "speed up" your loop, you can reduce the pyautogui.PAUSE value. However, keep in mind, this will prevent you from having time to activate the failsafe if you need it.

Is it Possible to use pyAutoGui and Dim the Screen in Windows

I am running an application that uses pyautogui.moveTo() and pyautogui.click() for a long period of time (an hour+). To decrease the battery drain on my laptop, I'd ideally want the screen to turn off in this period, but because the autogui functions mimic using the mouse, the computer will not turn the screen off. I'm using Windows 10, and I'm not sure if I could use some functions from PyWin32 or WMI as demonstrated in this post, because the screen will brighten again when the mouse moves each time.
Having the laptop turning off and on the screen repeatedly doesn't seem like it would save too much power, but I'm not sure.
I am not sure this is what you want but many to all laptops have a brightness setting and windows devices definitely do. This should save some power on your device.

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?

How to Change Mouse Cursor in PythonCard

How do I change the mouse cursor to indicate a waiting state using Python and PythonCard?
I didn't see anything in the documentation.
PythonCard builds on top of wx, so if you import wx you should be able to build a suitable cursor (e.g. with wx.CursorFromImage), set it (e.g. with wx.BeginBusyCursor) when your wait begins, and end it (with wx.EndBusyCursor) when your wait ends.

Categories