How to send keystrokes to a window?

后端 未结 2 1015
难免孤独
难免孤独 2020-11-30 12:38

im using keybd_event(); and i want use SendMessage(); to send keystroke to notepad, can this be done?

2条回答
  •  难免孤独
    2020-11-30 13:15

    using SendMessage to insert text into the edit buffer (which it sounds like you want):

    HWND notepad = FindWindow(_T("Notepad"), NULL);
    HWND edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL);
    SendMessage(edit, WM_SETTEXT, NULL, (LPARAM)_T("hello"));
    

    if you need keycodes and arbitrary keystrokes, you can use SendInput() (available in 2k/xp and preferred), or keybd_event()` (which will end up calling SendInput in newer OSs) some examples here:

    http://www.codeguru.com/forum/showthread.php?t=377393

    there's also WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR events for SendMessage which you might be interested in.

提交回复
热议问题