[python-win32] Re: Help on using win32api.SendMessage to send keystrokes

Daniel F nanotube at gmail.com
Thu Mar 31 03:46:43 CEST 2005


Well... i figured it out - turns out sending the keystrokes to the top
window of notepad didnt work, but sending them to the Edit child
window of notepad did the trick.

But this brings me to another question, although of a less urgent
manner. i had to send WM_CHAR messages, rather than WM_KEYDOWN/KEYUP
in order to get it to work. I have nothing against WM_CHAR, as long as
everything works, but i am just curious why i was not able to achieve
the same effect with the WM_KEYDOWN/KEYUP pair? any takers?

Thanks,
Daniel

On Wed, 30 Mar 2005 10:28:36 -0500, Daniel F <nanotube at gmail.com> wrote:
> Hi,
> 
> I am trying to use win32gui.SendMessage API (or PostMessage), and
> cannot figure out why
> it is not working. I would appreciate any help! Simple test script I
> am using is included below.
> 
> I am using pywin32-203 and python 2.4, on winxp pro sp2.
> 
> I am a total newbie to python, so if this is a really dumb question,
> please do not be surprised. :) I thank you all in advance for any help
> and guidance you can provide.
> 
> Daniel
> ---------------
> #### Script to try to write something down in notepad
> import win32api
> import win32gui
> import win32con
> import time
> 
> # get the window handle of the blank, minimized notepad window
> hwnd = win32gui.FindWindowEx(0, 0, 0, "Untitled - Notepad")
> 
> # print it just for kicks
> print hwnd
> 
> win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
> #this restores the proper window, so we know we have correct handle
> 
> #just to give it a little pause
> time.sleep(2)
> 
> print "trying to post message"
> 
> #try to send it a return key
> win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
> win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
> 
> #the above generates absolutely no effect on the notepad window.
> #same effect no matter what vk code i use (e.g. 65 for A, VK_SPACE for
> space, etc)
> 
> #### end of script
>


More information about the Python-win32 mailing list