I'm trying to use SendMessage
to send keyboard input to another window. I know the drawbacks, but I have to do it since I have to send several keys and I can't guarantee that the window will have focus - so this has to work when the window doesnt have focus.
I'm testing it by trying to send keys to a notepad window. I've tried the following variations, and none have worked:
def post_keys1(hwnd):
win32api.SendMessage(
hwnd, win32con.WM_KEYDOWN, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.SendMessage(
hwnd, win32con.WM_CHAR, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.SendMessage(
hwnd, win32con.WM_KEYUP, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0xC0 << 24))
def post_keys2(hwnd):
win32api.PostMessage(
hwnd, win32con.WM_KEYDOWN, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.PostMessage(
hwnd, win32con.WM_CHAR, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.PostMessage(
hwnd, win32con.WM_KEYUP, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0xC0 << 24))
def post_keys3(hwnd):
win32api.SendMessage(hwnd, win32con.WM_CHAR,
ord('A'), 0)
def post_keys4(hwnd):
win32api.PostMessage(hwnd, win32con.WM_CHAR,
ord('A'), 0)
def post_keys5(hwnd):
win32api.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('A'), 0)
win32api.PostMessage(hwnd, win32con.WM_CHAR, ord('A'), 0)
win32api.PostMessage(hwnd, win32con.WM_KEYUP, ord('A'), 0)
def post_keys6(hwnd):
win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, ord('A'), 0)
win32api.SendMessage(hwnd, win32con.WM_CHAR, ord('A'), 0)
win32api.SendMessage(hwnd, win32con.WM_KEYUP, ord('A'), 0)
See Question&Answers more detail:
os