There is another window in the main Notepad block, you need to send it your messages. You can see this βhiddenβ window using the Microsoft Spy ++ tool or you can get all child windows as follows:
def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): hwnds[win32gui.GetClassName(hwnd)] = hwnd return True hwnds = {} win32gui.EnumChildWindows(whndl, callback, hwnds)
The window we are looking for is named Edit, and it is the only available and visible child window for Notepad. Thus, your code will work as follows:
import win32api, win32con, win32gui, win32ui, win32service, os, time def f_click(pycwnd): x=300 y=300 lParam = y <<15 | x pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam); pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam); def get_whndl(): whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad') return whndl def make_pycwnd(hwnd): PyCWnd = win32ui.CreateWindowFromHandle(hwnd) return PyCWnd def send_input_hax(pycwnd, msg): f_click(pycwnd) for c in msg: if c == "\n": pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0) else: pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0) pycwnd.UpdateWindow() whndl = get_whndl() def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): hwnds[win32gui.GetClassName(hwnd)] = hwnd return True hwnds = {} win32gui.EnumChildWindows(whndl, callback, hwnds) whndl = hwnds['Edit'] pycwnd = make_pycwnd(whndl) msg = "It works !\n" send_input_hax(pycwnd,msg)
lParam is an int, and you see a trick here that allows you to pass more than one value through a single argument. Let's say that we need to pass two digits to a function that takes only one argument. We can send them as a two-digit number and divide it inside the function. Similar operations of bitwise shift (<<) and bitwise or (|) can also be reversed in your case:
>>> x = 300 >>> y = 300 >>> lParam = y << 15 | x >>> lParam & 0x7FFF
Read more about bitwise operations in the Wikipedia and Python Wiki .