Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send keystrokes to a window?

Tags:

c++

windows

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

like image 265
H4cKL0rD Avatar asked Jan 21 '10 23:01

H4cKL0rD


People also ask

How do I put keystrokes on my screen?

Go to Start , then select Settings > Ease of Access > Keyboard, and turn on the toggle under Use the On-Screen Keyboard. A keyboard that can be used to move around the screen and enter text will appear on the screen. The keyboard will remain on the screen until you close it.

How do I send keystrokes to another app?

There are two methods to send keystrokes to an application: SendKeys. Send and SendKeys. SendWait. The difference between the two methods is that SendWait blocks the current thread when the keystroke is sent, waiting for a response, while Send doesn't.


1 Answers

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.

like image 170
jspcal Avatar answered Oct 08 '22 19:10

jspcal