[python-win32] Using SendMessage to send an accelerator key
nanotube at gmail.com
Wed Apr 13 06:05:53 CEST 2005
> Daniel F wrote:
> >win32api.PostMessage(HWND, win32con.WM_KEYDOWN, win32con.VK_CONTROL, lParamBits)
> >win32api.PostMessage(HWND, win32con.WM_KEYDOWN, 0x53, lParamBits)
> >win32api.PostMessage(HWND, win32con.WM_KEYUP, 0x53, lParamBits)
> >win32api.PostMessage(HWND, win32con.WM_KEYUP, win32con.VK_CONTROL, lParamBits)
> Thank you; this message was very helpful. I will try this without the
> getkeyboardstate/setkeyboardstate api or ctypes first, then if necessary
> dig deeper.
> I'm a bit disappointed not to find a higher-level general solution to
> the problem, though. It seems to me at first glance that sending
> accelerator key combinations might be a common task for which people
> would want to use PyWin32. AutoHotKey does it and is open source; maybe
> some of their code could provide a starting point for a general solution.
ah, since you were asking specifically about sendmessage and the win
api, i didn't think you were looking for higher-level wrappers.
In that case you may want to look at the SendKeys module
(http://www.rutherfurd.net/python/sendkeys/). that might be closer to
what you are looking for, in terms of higher-level general solution.
you can send keypresses by just specifying the characters, and any
modifiers, without having to worry about any of that virtual key code
and other fancy function arguments stuff.
in case you care, sendkeys works by wrapping the keybd_event windows
api, which is marked as "superceded by SendInput" for win NT, 2k, and
XP, but should work just fine anyway. note that keybd_event (and thus
the SendKeys module, too) can only send keys to the window that has
focus, you cannot specify which window to send to like you can with
sendmessage and its hwnd argument.
More information about the Python-win32