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

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').

Related

How do I send key presses and mouse movements to an application running in the background? (In Python)

Ok, so what I want to do is allow my program to send mouse movements and keypresses (both mouse and keyboard) to a particular task without having to be tabbed into the task so I can do other things on my computer with the mouse and keyboard while the task runs. Is it possible to do this relatively simply with existing python modules?
example of what I want to do:
task='application name'
task.leftclick
task.moveX(int)
task.moveY(int)
task.keypress(r)
All while being able to use my mouse and keyboard normally on a different application.
Thanks for the help in advance!
(Python3.6)
For anyone who happens upon this post, I found https://pypi.org/project/ahk/ I haven't looked into it fully but either you can use this or create an auto hotkey script and just call that with you python code to achieve what I wanted to do in the question.

Python - control application started by the different user in Windows

I am making application that controls a browser with SendKeys. But as SendKeys get the full control over the keyboard, I want to run this app under the different user. This way I will be working, the application will do what it have to do, and we will not make problems for each other).
The simplest code is
import time
import SendKeys
time.sleep(10)
SendKeys.SendKeys('hello')
I run it, focus on the field where I want to insert my text "hello", and wait. If I don't change the user, all is done as expected.
But when I run it, change the user and return after 10 seconds, I see that SendKeys sent nothing to the program.
How to send keystrokes to the program under the different user?
(I was trying to do the same with pywinauto, but the result was almost the same - all is good if I don't change the user, and error if I change it. So I thought that it is much simplier to resolve this problem with only SendKeys).
Just to summarize our discussion in comments and in the chat. Your wishes are very wide. I'm just trying to show you some directions to learn.
If you want to use SendKeys/TypeKeys/ClickInput methods (working as a real user), you need to run your automation script in the remote session, not locally. This is explained in details in my other answer: SetCursorPos fail with "the parameter is incorrect" after rdp session terminated.
If you want to run the automation on the same machine silently (in minimized state), there is an example for dealing with hidden windows: Python - Control window with pywinauto while the window is minimized or hidden. Just minimize the window and use silent methods (almost all except ClickInput and TypeKeys).
Feel free to ask more detailed questions about pywinauto and GUI automation.

Grab keyboard output

I want to find a way in Python for detecting "Keyboard event" like PyUserInput. I dont want the keyboard to type any character after pressing a button, and ONLY call me event occured.
For example, after pressing the "s" key run the event handler but not typing "S"! this action should be applied in all over the windows not frames such as "Tkinter"
(I want to grab keyboard output signal by python)
What you're trying to do isn't feasible with python.
What you want to do is essentially make a new driver for your mouse/keyboard.
This is not system independent and there's no good/nice way of doing this with python.
PyUserInput attempts to abstract out the system dependency - but this is not complete, and it cannot override the keyboard/mouse completely. What it can do is it can mimic actions on your mouse or keyboard only if the application allows emulated actions. A lot of programs like games and a bunch of other software disable emulated actions.
The most stable and significant tool which is not a driver level tool are autoit and autohotkey - you could try making an autoit/autohotkey script which is executed as a python subprocess and use pipes to send and get info from the script.

Python 3, capturing key combinations

I am writing long running program with simple GUI, the 99% of time I would like the program to work only as process, however sometimes I want to check the status, so is it possible to capture the keypress event in python?
For example I want to show the program window when I press Ctrl+Shift+Alt+Q, I expect to use app on Windows
Thank you
There are tutorials on how to create a key-logger with Python. They should help. But I do not know if that is the right way to go.
Also you could register shortcuts under a key combination on Windows.
You should be aware that Ctrl+Shift+Alt are handled independent of the keyboard layout and Q changes with the language.
With pywin32 you should be able to do that using Ctrl+Shift+Alt+F1 for example.

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

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.

Categories

Resources