How to emulate mouse/keyboard events in a unfocused/minimized window? - python

I am trying to do some automation with Python, but I want to execute it and still being able to use my machine freely. So I am using PyWin32 to emulate some clicks and typing but it only works if I run the script while the window is open and focused.
There is a way to make my script only focus on a window, and still be able to click on that window without taking control over the mouse, even if the window is not focused (if it works when is minimized, is best!)?

i do not know the PyWin32 package but from a win32 api point of view the thing should be easy.
get a HWND of that window and post (PostMessage) the events you want to the window.
eg: WM_LBUTTONDOWN & WM_LBUTTONUP, WM_RBUTTONDOWN & WM_RBUTTONUP, WM_MOUSEMOVE...
look at the win32 help how to set the wParam & lParam data for the specific events.
i controlled diablo 3 this way for example ;)
Edit:
there is no need to be in focus or maximized for this
Edit Edit:
may be you should look after autoit, a widely used scrip language for window automation. I never used it but read the name very often in this context. it may also be usable from python.

Related

Open new window - tkinter/tk taskbar/dock

How can I can a function and create a function for the icon context menu? I want to make the iconic Open New Window function pop-up when you right click on the icon menu in the taskbar/dock. See this question for more info on this kind of question.
Places I have looked but found nothing:
https://www.tcl.tk/man/tcl8.5/TkCmd/tk_mac.html#M15
https://pyinstaller.org/en/stable/feature-notes.html#open-event-handling-in-a-tkinter-based-gui-application
While there are a tiny subset of apple events that you can benefit from the DockMenu is not accessible directly via tkinter. Turns out mac is not really open for modifications and even smaller tasks can be hard. You could consider to use a system tray icon instead.

Auto refresh window to using python

I would like to make a program for make my window refresh every 10 min. i'm new to programming and currently exploring python technology
My machine is connected with pc based software which will test the product
The software tend to be freeze and system hang-- we need to restart the pc to solve this issue
idea:
I want to make a python program to auto run and keep click refresh button every 10min
my idea is to keep window always ready to receive data
I just beginning to explore below code ( very simple)
import mouse
#left click
mouse.click("right")
mouse.wheel(-3)
# right click
mouse.click("left")
How do I access the "refresh" option on the right click?
is my idea in not particle?
Your question is very unclear, so I'm just guessing you are in a browser window and want to refresh the site (right?)
Using the mouse package to do so will be very hard, therefore I suggest you use the keyboard-package, which is even from the same author.
import keyboard
keyboard.send("F5")

Bring terminal to the front in Python

Is there a way to bring the Linux terminal to the front of your screen from a python script? Possibly using some kind of os. command
I.e - Your python script opens up a GUI that fills the screen, but if a certain event happens that you want to see printed in the terminal to be viewed, but don't want to / can't show this information on the GUI (so please don't suggest that)
And if possible, hide it back behind your other windows again, if needed.
(Python 2, by the way)
Any suggestions greatly appreciated.
Not in any generally supported way.
Some terminal applications may support the following control sequences. However, these sequences are not standardized, and most terminals do not implement them.
\e[5t - move window to front
\e[6t - move window to back
\e[2t - minimize ("iconify") window
\e[1t - un-minimize window
— from http://rtfm.etla.org/xterm/ctlseq.html
That "bring the Linux terminal to the front of your screen" is likely talking about terminal emulators running in an X Window environment. Ultimately this is accomplished by making a request to the window manager. There is more than one way to do this.
xterm (and some other terminal emulators) implement the Sun control sequences for window manipulation (from the 1980s) which were reimplemented in dtterm during the early 1990s. xterm has done this since 1996 (patch #18).
Python: Xlib — How can I raise(bring to top) windows? mentions wmctl, a command-line tool which allows you to make various requests to the window manager.
xdotool is another command-line tool which performs similar requests.
finally, Can a WM raise or lower windows? points out that you can write your own application (and because python can use shared libraries written in C, you could write a script using the X library).

How to make a clickable control that is always on top, in Windows using Python?

The goal is to have an application that runs on top of all other applications and windows in the desktop, and display a clickable control (say, an image button) that moves around the screen.
The application must run as a service in the background and show thebutton (let's say) each hour, once clicked it disappears until the next hour.
This application has to be written in Python.
It looks like PyQt is one of the better options, but I'm not sure if it does support this sort of functionality and if it is a good alternative for modern Windows applications.
What packages or frameworks are appropriate for this scenario? I have seen Pygl and PyGame but they seem to be limited to a window, is this correct?
You actually don't need to create the program as a service. You can just start the application and not show the window immediately. You can use PyQt or wxPython. I'm more familiar with wxPython, so if you went that route, you could use a wx.Frame style flag such as wx.STAY_ON_TOP to get the functionality you want.
I have created applications that load up in the system tray with just an icon. When you click the icon, it shows the frame. The rest of the time, the frame is hidden. I would try that route before looking at doing a service.

How to send a keyboard event to a different Window in Pywin32?

I have to write a script to emulate some keyboard event in a different program in background.This is my code:
pwin = win32ui.FindWindow(None,r'someprograme')
pwin.SendMessage(win32con.WM_KEYDOWN,18)
pwin.SendMessage(win32con.WM_KEYDOWN,68)
pwin.SendMessage(win32con.WM_KEYUP,18)
pwin.SendMessage(win32con.WM_KEYUP,68)
pwin.SendMessage(win32con.WM_KEYDOWN,13)
pwin.SendMessage(win32con.WM_KEYUP,13)
But it seems nothing happened.So what should i do?I've tried PostMessage func it seems it still can not do it.
After a quick look at the WM_KEYDOWN docs:
Posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed.
But looking up your keycodes, you're trying to send ALT-D (followed by ENTER, which is fine). It sounds like you're trying to drive the menus; if that's what you want to do, WM_KEYDOWN is not the way to do it.
The problem is that keyboard menu navigation is driven by Windows, not by the app (except for a handful of apps that override normal menu processing, like some versions of Visual Studio). When you're looking at Notepad, and you hit ALT-F to open the File menu, the Notepad code gets a bunch of menu-related messages (WM_INITMENU, etc.), not the keystrokes.
If you use a WM spy program (I think the free Visual Studio Express still comes with Spy++ and ManagedSpy, but if not, search for an equivalent), you can see what the application is actually seeing when you drive it with the keyboard, and then you can figure out how to emulate that from your Python script.
On top of everything else, depending on how the program is written, it may not accept keystrokes when it doesn't have focus.
By the way, if you're just getting started with Windows GUI automation, you might want to look at something higher level, like pywinauto. That way, you don't have to work out what menu-related messages to send to open the Data menu; you just do something like app.Foo.MenuSelect('Data').

Categories

Resources