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

Thomas Heller theller at python.net
Fri Apr 1 14:09:33 CEST 2005


Roel Schroeven <rschroev_nospam_ml at fastmail.fm> writes:

> Tim Roberts wrote:
>
>> On Wed, 30 Mar 2005 20:46:43 -0500, Daniel F <nanotube at gmail.com> wrote:
>> 
>>> 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?
>>>
>> It depends entirely on what the application expects.  When the keyboard
>> driver sends keystrokes, the generic keyboard driver translates the key
>> codes to characters, if possible.  It will send WM_KEYDOWN, then WM_CHAR
>> (if an ASCII translation exists), then WM_KEYUP.  Applications can
>> choose which ones they want to handle.
>
> Not really: AFAIK this is not done by the keyboard driver; it is done by
> the TranslateMessage function which is normally called in the message loop.
>
> MSDN on WM_CHAR: "The WM_CHAR message is posted to the window with the
> keyboard focus when a WM_KEYDOWN message is translated by the
> TranslateMessage function"
>

Since TranslateMessage, which is typically called in the application's
message pump, creates the WM_CHAR messages, the OP would probably get
what he expects when he uses PostMessage for WM_KEYDOWN/WM_KEYUP instead
of SendMessage.

Thomas



More information about the Python-win32 mailing list