Send keys to background application - python

I am trying to send keys to an application running in the background. I want to send 'Alt+f+a' to the background application. I tried type_keys('%fa') from pywinauto but that brings the window to the foreground, I want it to remain in the background.
Here is a bit of what I currently have:
app = Application(backend="win32").connect(path=exePath)
# Open Save As
win = app.window(title_re=appTitle)
win.wait("enabled", timeout=60)
win.type_keys('%fa') #Sends keys, but brings window to foreground
Using type_keys('%fa', set_foreground=False) will send the command to the active window. So, I am using VsCode as my text editor and 'Alt+f+a' gets sent there instead of the target application.
I have tried using send_keystrokes() instead of type_keys(), but this does not complete the command. It does send 'Alt+f' in the background but is not sending the 'a'. I have tried calling 'Alt+f' then 'Alt+a' immediately after (and with a time delay) but sending another 'Alt' just closes the opened file menu.
During my search I have seen PostMessage() from win32gui but have not been able to get that working.

You may try win.send_keystrokes("{VK_MENU DOWN}fa{VK_MENU UP}") to hold Alt for 2 symbols and then release it.

try sending the type_keys parameter set_foreground to False
something like this:
app = Application(backend="win32").connect(path=exePath)
# Open Save As
win = app.window(title_re=appTitle)
win.wait("enabled", timeout=60)
win.type_keys('%fa', set_foreground=False)
or try this method instead: send_keystrokes
like the description says it send keystrokes silently ^^

Related

How to make KeyboardInterrupt outside of console?

I'm using PyAutoGui for automation of adding data to certain fullscreen app. Problem is, when I try to Alt+Tab back to my IDE it constantly brings app back to front and messes up my code by pasting data there. I need a way to either prevent script from working when I press Alt+Tab or when window comes out of focus. How do I do that?
I tried:
Ctrl-C
Ctrl-Pause
import _thread
def input_thread(a_list):
raw_input() # use input() in Python3
a_list.append(True)
def do_stuff():
a_list = []
_thread.start_new_thread(input_thread, (a_list,))
while not a_list:
stuff()

using wait in pywinauto times out but sleep works fine

I am trying to automate some GUI stuff in Windows. I open a settings window but when waiting for it to open before continuing, it times out:
from pywinauto import Application
app = Application()
app.start(r"explorer shell:::{05d7b0f4-2121-4eff-bf6b-ed3f69b894d9}")
app.window(title_re=".*Notification Area Icons*").wait("exists", timeout=20)
app = Application(backend="uia").connect(title_re=".*Notification Area Icons*")
main_dlg = app.window(title_re=".*Notification Area Icons*")
main_dlg.print_control_identifiers()
Even with the timeout at 20 seconds, it times out. However, if I replace app.window(title_re=".*Notification Area Icons*").wait("exists", timeout=20) with sleep(5) (and add the appropriate import) it works fine. I know the window title is correct because it will print out the control ids when using sleep. I've tried using "exists" and "visible" for the wait as well. I think the other options are more strict so I'm not sure why the wait is not working.
It's possible there are few explorer.exe processes. You can try process-agnostic way:
from pywinauto import Desktop
window_wrapper = Desktop(backend="uia").window(title_re=".*Notification Area Icons*").wait("exists", timeout=20)
print(window_wrapper.process_id())
# and compare with print(app.process)

pywinauto with a popup window

Im working through pywinauto, not a great developer but I can write some of the basics in python. Im getting hung up though. I have a popup that is causing me to not be able to press ok on and really not sure what direction I need to look
Really a two part question:
How can I move over to this popup IF it occurs, this wont always be the case as sometimes those files may not exist.
I tried using app.Confirm.Ok.click() and also app.Confirm.type('{ENTER}') neither worked.
How can I add in variables that I could call from an external .txt file for things like the "localhost" I included there in my code?
Code here:
from pywinauto import application
import time
app = application.Application()
app.start("Install.exe")
app.SelectLanguage.Ok.click()
app.Platform.Iacceptthetermsinthelicenseagreement.click()
app.Platform.Next.click()
app.Platform.Next.click()
app.PlatformInstallationOptions.Next.click()
app.PlatformSpecifyCertifcate.comboBox.select(0)
app.PlatformSpecifyCertifcate.Next.click()
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('localhost')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('{SPACE}')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('Config')
app.PlatformConfigurationDatabaseOptions.Next.click()
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{TAB}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{TAB}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{RIGHT}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{SPACE}')
app.PlatformSpecifyHTTPSBindingCertifcate.Next.click()
app.PlatformAdvancedWorkflowSettings.Next.click()
app.PlatformPlatformLanguage.Next.click()
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('localhost')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('{SPACE}')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('Instance')
app.PlatformInstanceDatabaseOptions.Next.click()
app.PlatformTimeZone.Next.click()
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{UP}')
app.WebApplicationOptions.Next.click()
app.WebApplicationOptions.type_keys('{ENTER}')
confirmWin = .app.window(title_re = u'Confirm') #Check your window header object name.
# Use timeout based on average pop up time in your application.
if confirmWin.exists(timeout=10, retry_interval=1):
confirmWin.set_focus()
yesBtn = confirmWin[u'&Yes']
# Check the object name of the Yes button. You can use Swapy tool(It is deprecated but it works, else you can use inspect.exe)
yesBtn.click()
else:
print('Confirmation pop up did not appear')
This should work :)

No control identifiers using pywinauto

I have a problem with print_control_identifiers() using pywinauto.
I start my test by opening test app.
window1 = Application(backend="uia")
window1.start("C:/Program Files (x86)/.../App.exe")
window1.Dialog.print_control_identifiers()
window1.Dialog.Run.click()
This is ok, I have output with all elements in this dialog - all buttons, boxes, labels etc.
After click() my dialog disappears and i need to connect to new window, so:
window2 = Application().connect(title="new-window")
window2.AppDialog.print_control_identifiers()
And there is my problem: output not contains any elements, just simple:
HwndWrapper[App.exe;App;c002ffc1-d144-4cd1-8ab0-afe5031cb9ea] - 'new-window' (L-8, T-8, R1928, B1058)
['new-window', 'HwndWrapper[App.exe;c002ffc1-d144-4cd1-8ab0-afe5031cb9ea]', 'new-windowHwndWrapper[App.exe;c002ffc1-d144-4cd1-8ab0-afe5031cb9ea]']
child_window(title="new-window", class_name="HwndWrapper[App.exe;c002ffc1-d144-4cd1-8ab0-afe5031cb9ea]")
How to print all control identifiers of this new window?
Whatever I try to find, eg. window2.AppDialog.Login.click() i have pywinauto.findwindows.ElementNotFoundError.
Looks like you forgot to use backend="uia" for Application object in this line:
window2 = Application().connect(title="new-window")
It should look so:
window2 = Application(backend="uia").connect(title="new-window")
If the first window spawns a child process, you have to connect to this process which contains new window. We have plans to implement child process detection by Application object in the future. Now it should be handled manually.
Try this method:
pid= application.process_from_module('toolName.exe')
app= Application(backend="uia").connect(process=pid)
handle = app.window(title_re="new-windowName")
handle.print_control_identifiers()
Or just use Inspect.exe to get all the details if you dont need print it explicitly.
Inspect.exe can get you all the details of any element you focus on..

Python 2.7: Close a dialog without exiting the entire application

I'm running a script coded in python from a scripts menu in a desktop application. It's basically a giant macro that I wrote and added a GUI to. I'm pretty sure the GUI is a really old one that my desktop app uses called dialogKit from MIT.
GitHub still has it here.
The problem is the word "stop" at the very end of the dialog code.
I keep getting a "stop is undefined" message, which I understand, but I've tried everything to close the dialog and if I use exit(), sys.exit(), I don't get an error, but it also closes my entire desktop app.
I need to close the dialog and keep the software open.
The limited dialog documentation for what I'm using can be found here.
(you might have to click on the Dialog section. Their site uses frames.)
class MyDialog:
def __init__(self):
self.d = Dialog(self)
self.d.size = Point(300, 340)
self.d.Center()
self.d.title = "Halftone" #<----- Title of the dialogue
self.d.AddControl(STATICCONTROL, Rect(aIDENT, aIDENT, aIDENT, aIDENT), "frame", STYLE_FRAME)
# more controls and methods..
def on_ok(self, code):
return 1
def on_cancel(self, code):
print "blah"
def Run(self):
return self.d.Run()
d = MyDialog()
if d.Run()!= 1:
stop
I just need a way to change stop to something that 1) will prevent the script from running, and 2) close the dialog without quitting the entire application. This is the functionality of a typical "cancel" button, which is what I want.
Another option is the method called on_cancel(), which I also tried and could get the event itself to work, but still the entire application quits with any kind of exit().
The docs show a method called End(), which claims to terminate the dialog object, but I've tried and failed to get that to work either.
Okay, I'm posting an answer because I think I have a handle on your problem.
Try replacing stop with:
d.d.End()
If that works, you might want to try putting:
self.d.End()
inside of the on_cancel function in your class. That should close the dialogue without closing your program.

Categories

Resources