Execute a keybind on a different application from python script - python

It's kind of hard to explain. Maybe the result is out there but I couldn't find it.
Simply put; I would like to run hotkeys on a program running on my desktop (Windows) via Python.
I have a 3rd party program that I have assigned certain hotkeys/keybindings to. For example when ctrl+Fn5 keys are pressed a certain command executes on that program.
What I want to do via Python scripts is to execute the mentioned hotkey (like ctrl+Fn5) on that program. (as if that program had focus on it and those keys were pressed)
I have been trying to do it via win32com.client. But the program itself doesn't allow that feature directly via its libraries. I also tried pywinauto but again it doesn't provide the libraries needed (tried analyzing it with other tools like swapy).
I have used catching hotkeys on python before. But they were for the Python application itself.
for example:
def keyPressEvent(self, e):
if e.modifiers() & Qt.ControlModifier and e.modifiers() & Qt.ShiftModifier and e.key() == Qt.Key_V:
# do somthing
What I need is to execute the hotkeys with the focus on the other program.
I looked into the os module to see if there was anything there that could help me but couldn't find anything.

Using pywinauto modules i got it to work. The trick was getting the focus on the program itself.
from pywinauto.application import Application
from pywinauto.keyboard import SendKeys
app = Application().connect(path='path_to_programs_exe_file')
app_dialog = app.top_window()
app_dialog.maximize()
app_dialog.set_focus()
SendKeys('^{F5}')

Related

How can I send keystrokes and mouse movement to a specific PID?

How can I send keystrokes and mouse movements to a specific running program through its PID. I've used both pywinauto and pynput, and they work great, but I want to send keys to a program that is not in focus. I found this question: How to I send keystroke to Linux process in Python by PID? but it never explains what filePath is a path to.
If you could help solve for this example, that would be great! I want to send the "d" key to an open Minecraft tab for 10 seconds, and then send the "a" key for the next 10 seconds and stop. I would need this to be able to run in the background, so it could not send the keys to the computer as a whole, but only to the Minecraft tab. I am on Windows 10 by the way.
Any help would be appreciated!
Pretty sure you won't be able to, at least not easily let me explain a little bit how all of this works.
Lets start with the hardware and os, the OS has certain functions to read the input you give the computer. This input goes into a "pipe", the OS is reading input, and putting into the pipe, on the other side of the pipe there may be an application running, or it may not. The OS typically manages this (which app to put on the pipe listening) by defining which app/window is active. Apps access this pipe with the API given by the OS, they read the input and decide on it.
The libraries you cited above, change the values of the keyboard and mouse, in other words, they make the OS read other values, not the real ones, then the OS puts them in the "pipe", and are read by the app that is listening on the pipe (the one active). Some apps have their own API's for this, but I would guess Minecraft doesn't. If they don't have an API, what can you do? well, as I said, nothing easy, first of all "hacking" the app, in other words change it to listen to some other input/output rather than the one given by the OS, (this would be you making your own API). The other one would be you changing the OS, which would also be extremely hard, but maybe a tiny bitty easier. It also depends on your OS, I think Microsoft does offer input injection api's
So, simple options, first, run a VM with a GUI and use pywinauto, pyautogui, etc. The other option would be if you can run it in the browser, do so, and use something like Selenium to automate the input.
Quick note, why does selenium works and the browser can read input in the background? Easy, it's not, it just executes the code it would execute if it would have read the input! javascript, cool isn't
With ahk you can do this with Python+AutoHotkey
pip install ahk
pip install "ahk[binary]"
from ahk import AHK
from ahk.window import Window
ahk = AHK()
win = Window.from_pid(ahk, pid='20366')
win.send('abc') # send keys directly to the window
Note that some programs may simply ignore inputs when they are not in focus. However, you can test this works in general even when not in focus by testing with a program like notepad
Full disclosure: I author the ahk library.

Possible to send commands to a wX GUI from a Python Script?

I have a wX application that I would like to control programmatically, from a Python Script. I tried to accomplish this by writing the following code:
Application = myApp()
myGUI= openGUI(Application)
This successfully opens an instance of my wX application, and I can then interact with this via its GUI, for instance by clicking Open File. However, I was hoping I could send commands by writing code like:
myGUI.OnFileOpen(myFile)
Unfortunately, inspecting "myGUI" in the debugger reveals the value of this variable to be "nothing". So I cannot access the methods of the running wX GUI via my proposed method.
If anyone could offer a suggestion for how to accomplish my goal, I would really appreciate it.

AutoKey: Switch to last active application like Alt-Tab by using AutoKey

is it possible to send the alt+tab for switch to last window with AutoKey ?
i tried without success:
keyboard.send_keys("<alt>+<shift>+<tab>")
Or forward window:
keyboard.press_key('<alt>')
keyboard.press_key('<tab>')
keyboard.release_key('<tab>')
keyboard.release_key('<alt>')
Or backward window:
keyboard.press_key('<alt>')
keyboard.press_key('<shift>')
keyboard.press_key('<tab>')
keyboard.release_key('<tab>')
keyboard.release_key('<shift>')
keyboard.release_key('<alt>')
result: no error but only moves the tab count inside the editor.
TL;DR: Not directly with our API.
The AutoKey API talks directly to the current active window. So, sending events targeted at the desktop (DTE) will only work if the current active window recognizes them as such and either forwards them to the DTE or emulates what they do.
However, since AutoKey scripts are written in full Python 3, if you can figure out how to do it yourself in Python, AutoKey can run it for you. And, if some other solution is available, you can run it from within an AutoKey script using the subprocess module.
Autokey's Window class allows you to activate a window by name (via wmctrl), among other functionality. Something in that class may be what you're looking for.

Capturing windows keyboard strokes while script isn't current used process

I am trying to make an application that redoes my exact movements on keyboard on a certain process, so I am attempting to register my keyboard strokes time-stamped, I tried to do it using msvcrt or pygame, however they both require keys to be entered to the command prompt itself, else it wont detect it.
Is it possible to create a time-stamped "keylogger" using PYTHON
P.S: I just want to learn more about python scripting.
this is the code i tried:
import msvcrt
import time
import datetime
while True:
char = msvcrt.getch()
print char
print datetime.datetime.now().time()
What you've written is a program that captures inputs to itself. What you need, at least for Windows, are global input hooks. This will allow your application to capture all input to the machine.
Take a look at the following resources:
Windows Hooks Overview
Python for Windows Extensions
PyHook, a Python wrapper for global input hooks

python, write a code to automatically execute an external program and use the method to execute the program's task

I want to run my external (.exe) program on window using python. in order to run a specific task of that program(the .exe) , i need to know the method and call in inside my test.py (my python file), how do i call a function in my python?
import subprocess
subprocess.call(['C:\\EnergyPlusV7-2-0\EP-Launch.exe', 'C:\\modelo_0001.idf'])
now i need to call a method through python to run a specific task on the external program(the .exe file), but how? can someone give me an example format? there s a button say 'simulate', i need to get the method for that button so that i can execute it through python!
You should try to use pywinauto for that: http://code.google.com/p/pywinauto/
Install it and read docs.
example:
from pywinauto import application
app = application.Application.start("C:\\EnergyPlusV7-2-0\\EP-Launch.exe")
# open dialog here with pywinauto (see docs), it's bound to interface can't help here
# press next button
Also look into SWAPY it will generate automation python code for pywinauto.
http://code.google.com/p/swapy/

Categories