How can a Python 2.7 script detect the start/stop event of a Windows Screensaver?
You can actually do that with Pywin32. Pywin provides Python bindings for the Win32 API and for COM.
Regarding your question, it allows you to "listen" to windows events - such as start/stop event of a Windows ScreenSaver.
You should be able to get the current state of the screen saver by doing something like this:
import win32gui, win32con
def CheckScreenSaverState():
return win32gui.SystemParametersInfo(win32con.SPI_GETSCREENSAVERRUNNING)
In this example we use the win32gui which allows access to the GUI of a windows application. By calling win32gui.SystemParametersInfo (which is a function that actually belongs to the windows GUI messaging system - out of scope for this explanation, read more here), we are able to get the state of the screen saver, using the SPI_GETSCREENSAVERRUNNING constant (which is an internal constant of the Windows OS). This method I wrote should return a boolean value of the state of the screen saver (True is running False if not).
I didn't have time to test it, but tell me how it went I might be able to help you further.
Good luck,
Tom.
Related
I'm using squishtest library for manipulating Qt application from my Python code and attempting to use event handlers as follows:
import squishtest
def handle_mouse_event(event):
print 'Clicked!'
squishtest.startApplication('application')
squishtest.installEventHandler('QMouseEvent', handle_mouse_event)
Unfortunately this doesn't work, i.e. nothing happens on clicking elements inside the app, however it works in case I run equivalent of this code inside the Squish IDE in Squish runtime:
import squish
def handle_mouse_event(event):
print 'Clicked!'
squish.startApplication('application')
squish.installEventHandler('QMouseEvent', handle_mouse_event)
What is the difference and how to get event handlers working with squishtest?
Python 2.7.14, Squish 6.3.1, Ubuntu 16.04
(Reposting as answer as suggested by original poster.)
This works for me on Linux and Windows with Squish 6.3.x+, using the Python installation in the Squish package - but admittedly I have added a snooze(5) at the end of the script to have some time to "mouse around" over the application's window, then even the mouse movement based events trigger execution of the event handler function.
Another side effect of using snooze() is that the event loops keep being spun, which is not the case when using time.sleep() (which you later mentioned to have used before).
If it still does not work for you I recommend to contact the technical support of froglogic Squish.
I have recently started using win32api to simulate mouse events and was wondering if it was at all detectable?
For example, does the api follow the exact same process/run the exact same commands as if done when using a real mouse - or are there some slight differences which can be detected? Furthermore, is this the same case with win32com SendKeys (via Shell Script/Python)?
I ask, because in the past I have had a few applications detect the Java robot library - but they all seem to work fine when using the Python win32api. Thanks.
The SendInput function will insert input events into the same queue as a hardware device but the events are marked with a LLMHF_INJECTED flag that can be detected by hooks. To avoid this flag you probably have to write a custom driver.
Basically I would like to write small script that would allow me to have some sort of programmable keyboard emulation. Something similar to how autohotkey on Windows is able to work.
Lets say I would like to rebind arrow keys to 'wsad' or 'hjkl' but only when CapsLook is active. I was able to detect keyboard key press with pyinput(https://pypi.python.org/pypi/pynput ) I also can send easily various keyboard events to focused window with pyautogui (https://pyautogui.readthedocs.io) But I can't figure out a way to consume events before they are received by currently focused window.
Any hints?
THIS module is one of the available tools for capturing keyboard events:
https://pypi.python.org/pypi/keyboard/
but it is still in the development and doesn't (yet) provide a global hook capable of capturing keyboard events at their very origin and forwarding them (or not) to the target application.
Another tool worth to look into is:
myboard.py at code.google.com downloads
The above script is using Python ctypes and Xlib modules which makes it possible to work directly with the system libraries written in C. It catches the keyboard events quite deep and system wide to a degree that it had crashed my OS while testing it a bit, so be warned ...
Consider also using XGrabKey and XGrabKeyboard from the X11 libX11.so system library:
import ctypes
libX11 = ctypes.CDLL('libX11.so')
XGrabKey = libX11.XGrabKey
XGrabKeyboard = libX11.XGrabKeyboard
print("XGrabKey: " , dir(XGrabKey))
print("XGrabKeyboard: ", dir(XGrabKeyboard))
I wrote a script using pyautogui that should start an program (an IDE) and then start using it.
This is the script so far:
#! python3
# mouseNow.py - Displays the mouse cursor's current position.
import pyautogui, sys, subprocess
from time import sleep
x,y = 1100,550
subprocess.call([r'C:\...exe', arg1, arg2])
pyautogui.click(x,y)
sleep(5) # 2 sec should suffice but this is for safety
pyautogui.typewrite(my_string)
pyautogui.press('enter')
This works well but I want to be portable. The x,y values were determined by where the program prompt appears on screen after I start the program, but this is not portable, I think. Is there a way to point the mouse to the prompt without giving const parameters? something like move_mouse_to_window_of_this_process_after_starting_it()
Also, I use sleep() so I would write the data to the window after it appears, but I guess it's not a good way (some PC will run this much slower, I guess), so is there a way to know when the prompt appeared and then do the pyautogui.typewrite(my_string)?
EDIT: I found a simple solution for the move_mouse_to_window_of_this_process_after_starting_it()
:
>>> pyautogui.hotkey('alt', 'tab')
If you need portable and reliable solution, you have to find a library that supports accessibility technologies to access GUI elements by text. Basic technologies are:
Win32 API, MS UI Automation (Windows)
AT-SPI (Linux)
Apple Accessibility API (MacOS)
There are several open-source GUI automation libraries supporting some of these technologies (usually 1 or 2). Python solutions:
pywinauto on Windows (both Win32 API & MS UIA, see Getting Started Guide)
pyatspi2 on Linux
pyatom on MacOS
There is also a thread on StackOverflow regarding hard sleeps vs flexible waiting.
Enjoy! :)
The way you are interacting with the .exe excludes alternatives to coordinates or blind firing (Tab, Tab, Enter etc..).
If the application has an API, you could interact with it programatically.
If it doesn't you can only try to match the location for x screen resolutions, and this only if the GUI is used in Fullscreen/windowed Fullscreen.
Using Python 2.7.10 and above, is there any way to retrieve and set the absolute mouse position with only the pre-installed libraries (no pygame, PyUserInput, Xlib, PyQt, pyautogui, PyMouse, etc.)?
This needs to work in all OSes as well (Linux, Mac, Windows).
The solution must work in the newest version of Python, as well as 2.7.10
I also can't have the user interact within a popup window, since it isn't relative, and the user still needs to be able to interact with other windows.
Also, if possible is there a way to cancel the default action triggered by the mouse event (like e.preventDefault() in JavaScript)?
Additional kudos if keyboard event interceptions/simulation is also possible with the same requirements.
sure assuming you can do it in an OS-Specific way first for each operating system
def getMouseCoords():
if "Windows" in platform.uname():
return get_windows_mouse()
elif os.path.exists("/dev/input/mouse"):
return parse_filesys_mouse()
elif some_otheros_condition:
return get_some_other_os_mouse()
you would of coarse have to figure out an os-specific way to accomplish the task for each os you want to cross support ...
setting them may be more complicated ...
vetoing the events will likely be very very hard
there is not universal way of doing input events as each OS implements it differently
of coarse this is a non-trivial task, luckily people have built libraries such as PyMouse that do this for you .... I suppose you could just copy and paste all their code, but how is that any different than just using the library as intended?