Keyboard output to multiple programs simultaneously? - python

My issue currently is that of emulating keystroke input to an arbitrary program (such as a running game).
Currently I am using win32 libraries on Windows to find windows (win32gui.FindWindow) and grab focus (via win32gui.SetForegroundWindow), then send keyboard input (win32api.keybd_event).
This works if I am only sending input to a single program, but I wish to parallelize, playing multiple games simultaneously. This does not work with my current method as both applications demand "focus" for the keys to go to the right application, thus interfering with each other.
Ideally I would like something that sends input to a given window, that does not require focus , and is independent of input given to other windows or the currently focused window.

My understanding is, only the foreground window get the focus and can handle keyboard input to play. Not sure in make sense to send input to background window or not....

Related

Send keystrokes to non-active GUI application without occupying the keyboard

As the title explain, i'm trying to use the terminal to send commands as keystrokes to a GUI application that's minimized.
There is a lot of similar questions here on Stack with some great answers, but i'm having, mainly, three problems with the solutions i saw: Most of the solutions need the automated application to be the active one. Or, i can't normally use my keyboard while the script/process is running. Or worse, the solution works only on Windows OS.
I need what this person asked 2 months ago: Send keystrokes to a specific window (in background), but do something else in the meantime
But i want it on Linux.
I'm using Kubuntu 18.10, if that helps.
xdotool was close, but i couldn't quite get it to send the commands to a specific window or PID. It also uses "my keyboard", so i can't, for example, write an essay/code/browse online while xdotool is running. Pexpect also have this last problem.
AutoHotKey looks like it would work, but it's only for Windows and i'm trying to not use Wine. Same with pywin32.
keyboard (https://github.com/boppreh/keyboard) seems nice, but it can't send a command to a specific application. Same with PyAutoGUI.
I selected the Python tag because most of the solutions i saw use Python, but i'm open to any language.
Use a nested X server to input keystrokes without changing focus or keyboard grab.
Proof of concept:
Xephyr -resizeable :13
export DISPLAY=:13
xterm
xdotool type rhabarber
The Xephyr nested X server is started and will listen on local X socket 13 (whereas :0 typically identifies the currently running X server, but when multiple sessions are ran concurrently, it could be higher).
Then we set DISPLAY environment variable to :13, so any X application we start will connect to Xephyr; xterm is our target application here. Using xdotool or any other tool we can send keystrokes.
As the target X server is identified through $DISPLAY, applications can be started or input events triggered from elsewhere as well. If needed, you might also run a lightweight window manager within Xephyr, e.g. to 'maximize' the application so that it fills the whole Xephyr window.

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.

How to read/edit a GUI/MFC application in Python?

I want to automate one of my tasks, by changing a third-party GUI/MFC application's properties as per my requirements. Every time I need to carry out any testing, I need to change the properties of the application to test my software.
I tried to automate it by using Python and IronPython. After Googling a lot I found IronPython, because the GUI is written C# and VB.NET.
Suppose when opening the GUI in its editor it gives me the option to edit the properties, MFC contains lots of controls.. e.g.:
Enter Time |__| //Need to enter the value in the box
Enter the dealy |__| //Need to enter the value in the box
Want to display |_| //Check box , check or uncheck
some Radio buttons.
Some more controls.
....
....
I want to control all the changes from my Python script. I will just enter the value from my script and it will update them in the GUI.
I wrote a script in IronPython to read the GUI:
fw = open("MyFile.vnb", 'r')
for line in fw.readlines():
print (line)
I found plenty of encrypted/encoded characters along with some of the C#/VB.NET codes in the console. So, I am completely stuck here.
I would like to know can we edit a third-party GUI with Python/IronPython or not? Do I need to use some special tools from Python to edit the GUI?
If you need classic GUI automation you can control MFC application by pywinauto library. You can send keyboard events, mouse clicks, moves etc. pywinauto has also limited support for .NET controls (simple automation like buttons, text boxes etc. is available). I guess this is realistic task (see sample video by the link above).
But if you need some binary instrumentation to change the GUI executable permanently (it sounds strange), this is completely another topic. Read about PIN tool. It's used for profilers development, for example. Collecting stack samples, unwinding call stacks and other tricky reverse engineering things. :)

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