Scripting Windows GUI application with Python (SendKeys)

aholt1 at my-dejanews.com aholt1 at my-dejanews.com
Thu May 20 20:48:20 EDT 1999


In article <mt2.0-3294-927071760 at news.informatik.uni-bonn.de>,
  Christian Lemer <Christian.Lemer at usrconsult.be> wrote:
> Dear all,
>
> Currently we use WinBatch to automate some tests and productions
> application under Windows NT.
>
> One year ago, we switched from Perl to Pyton as script language under
> unix.
>
> I wonder if it is possible to drive a Windows GUI application (using
> keybord commands) like it is possible in Winbatch with Python under
> Windows?
>
> I installed also the windows extensions but couldn't find the correct
> way to send a key to an application.
>
> Thanks in advance for your help.
>
> Chris.
>
> ==
> Christian LEMER                                    tel +32.10.65.44.11
> UsrConsult S.P.R.L                                 fax +32.10.65.44.10
> rue Margot, 37
http://www.usrconsult.be
> B-1457 Nil St Vincent             mailto:Christian.Lemer at usrconsult.be
>

Hello,

(Got an error posting this the first time because I wasn't registered
with deja.com yet, so here it is again just to make sure ...)

Good news.  ActiveX wrappers for the Visual Basic SendKeys method can
be found at an elegant site for Windows Scripting Host files:

http://cwashington.netreach.net/site/downloads.asp

One is for Win95 and the other for Win98.  They're small -- only 11K
and 24K respectively.  The HTML doc describes how to use them with
VBScript and JScript, not Python, but the procedure is much the same.
Here's what you do:

1.  After unzipping file Send_Key_Strokes_DLL_for_vb5_runtimes.zip,
register the library so that the COM Makepy utility can find it by
running regsvr32:

c:\ regsvr32 C:\Windows\System\SKey.dll (or wherever you put the file)

2.  From the Python Tools menu, select "COM Makepy utility".  From the
list of type libraries, select "SendKeys ActiveXDLL (4.0)." (The 8.0
version next to it is for VB6.)  Press OK to make a COM interface
file.  After a few seconds, makepy will print the long class ID of the
library, but not the name needed by win32com.client to use it.  To find
it, open the file generated by makepy in \python\win32com\gen_py and
search for a phrase beginning with
"This CoClass is known by the name".  What follows is the name needed
by win32com.client.  As it turns out, the sendkeys name is 'SKey.SKeys'.

3.  Now, import the library so Python can use it:

>>> import win32com.client
>>> sendkeys = win32com.client.Dispatch("Skey.Skeys")

4.  Here's an example script for exercising WordPad:

import win32api, win32ui, win32con, win32com.client

sendkeys = win32com.client.Dispatch("Skey.Skeys")

def wait(seconds=1,message=None):
        """pause Windows for ? seconds and print an optional message """
        win32api.Sleep(seconds*1000)
        if message is not None: sendkeys.SendKeystrokes(message)


def test_sendkeys_using(target_app="c:\\Program
Files\\Accessories\\Wordpad"):
        """
        Test free sendkeys OCX 'Skey.Skeys' using WordPad:
                sendkeys = win32com.client.Dispatch("Skey.Skeys")
        """
        win32api.WinExec(target_app)
        if target_app == "c:\\Program Files\\Accessories\\Wordpad":
                target_class = "WordPadClass"
        else:
                target_class = target_app

        target = win32ui.FindWindow(target_class,None)
        target.ShowWindow(win32con.SW_NORMAL)

        import winsound
        # winsound.PlaySound("c:\\downloads\\zelda
music\\zelda_theme",winsound.SND_ASYNC|winsound.SND_LOOP)

        target_title = "Sendkeys Example"
        target.SetWindowText(target_title)
        # sendkeys.ActivateApp(target_title)

        wait(1)
 	wait(1,"SendKeys {(}Skey.dll{)}+{HOME}")

        # Format/Font dialog
 	wait(1,"%O")# Format menu
 	wait(2,"{ENTER}")# Font dialog
 	wait(1,"{TAB}")# Change style
 	wait(1,"Bold") # Enter Bold style
 	wait(1,"{TAB}")
 	wait(1,"{TAB}")
  	wait(2,"{ENTER}")

  	wait(1,"{END}{ENTER}")
  	wait(1,"{- 50}{ENTER}") # 50 dashes

        wait(1,"%O") # Format menu
        wait(1,"{DOWN}") # Bullet style
        wait(1,"{ENTER}")

  	wait(1,"ActiveX wrapper for the Visual Basic SendKeys method
{ENTER}")
  	wait(1,"Also includes ActivateApp{(}with_window_title{)} method
{ENTER}")
 	wait(1,"Uses VB library MSVBVM50.DLL or MSVBVM60.DLL{ENTER}")
 	wait(1,"Written by Christopher Bradford{ENTER}")
 	wait(1,"Can be downloaded at an elegant Windows Scripting Host
web site:{ENTER}")
 	wait(1,"%OB{TAB}
http://cwashington.netreach.net/site/downloads.asp{ENTER}")

        # wait(1,"{ENTER 5}Zelda's Theme courtesy of Nintendo")

 	# winsound.PlaySound("c:\\downloads\\zelda
music\\zelda_theme",winsound.SND_PURGE)


(The Zelda theme is at www.zelda64.com).

Hope this helps.  At least this will work until someone builds an
extension that doesn't require the runtime VB.


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---




More information about the Python-list mailing list