Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send SendKeys to Windows form in python script?

I'm doing automation scripting in Python for my desktop application. In that I'm sending TAB key/any key to my windows form. But I'm not able to find handle of that Windows form in my Python script.

Here is the sample code snippet :

__author__ = 'juhis'

import SendKeys
import sys
import os
from Tkinter import *
import ctypes
import win32gui
import pywinauto

pwapp = pywinauto.application.Application()
whandle = pywinauto.findwindows.find_windows(title_re='Form1',class_name='WindowsForms10.Window.8.app.0.2bf8098_r13_ad1')[0]
window1 = pwapp.window_(handle=whandle)
window1.SetFocus()

SendKeys.SendKeys("""{PAUSE 2}""")
SendKeys.SendKeys("""{TAB 2}{PAUSE 2}{ENTER}""")

Please help me to figure out the issue.

-Thanks

like image 741
Ajit Medhekar Avatar asked Dec 03 '25 07:12

Ajit Medhekar


1 Answers

The code can be re-written simpler:

import pywinauto

app = pywinauto.application.Application().connect(title_re='Form1')
Form1 = app.Window_(title_re='Form1', class_name='WindowsForms10.Window.8.app.0.2bf8098_r13_ad1')
Form1.SetFocus()
Form1.TypeKeys("{PAUSE 2}")
Form1.TypeKeys("{TAB 2}{PAUSE 2}{ENTER}")

TypeKeys automatically sets a focus to the Form1 and types the keys. SendKeys doesn't set a focus because it's not aware about the window. That's probably why it doesn't work with SendKeys.

[EDIT] Of course you need to run the script as Administrator.

like image 72
Vasily Ryabov Avatar answered Dec 05 '25 21:12

Vasily Ryabov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!