We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Faking keyboard input

Sinclair asked
Medium Priority
Last Modified: 2013-12-03
Is there a way I can create a program that will "fake" keyboard input to a window not owned by that program ? For example, can I make a program that will launch Notepad (with ShellExecute, or something), and then cause it to type "Foo" ?
Watch Question

You could try and find the window handle and send messages to it.  Rather clunky I know.

The best way to fake keyboard input is keybd_event API.

Of course you need to make sure that the target window is the active window before using keybd_event.

I humbly suggest using a journal playback hook as a more foolproof method.


   Sorry, I am quite new at this. I saw the Journal Playback hook in the API docs, but it looks like I need to RECORD the keys first, somehow. I can't use that since I need to send an arbitrary sequence of keys.
    I also tried sending WM_KEYUP and WM_KEYDOWN messages, but they had absolutely no effect. What could I be doing wrong ?

did you try keybd_event()?

Using Visual Basic, you can create a program that will shell execute a program and then using the SendKey command you can send keystrokes to the application as if they were coming from the keyboard.  This is not as easy as it sounds.  You have to delay your sendkeys until the app is completely up.  You can also check the return code from the Shell command to close the app once you are done.

>> I saw the Journal Playback hook in the API docs, but it looks like I need to RECORD the keys first, somehow.

Not really.  You just have to fill the EVENTMSG structure with correct values.


Ah, sorry, I still don't get it :-(
What's an EVENTMSG structure ? I was just using standard Win API. I know about the VB SendKeys function, that's what I am trying to replicate.

The EVENTMSG structure is what you use with a journalling playback hook.  It is usually filled by a journalling recording hook but you can do it yourself.  Check the SetWindowsHookEx() API.

You could try a program called WinBatch.  There's a trial version available somewhere on the web.


Thanks, I looked up the EVENTMSG structure. However, I still could not make the thing work. How do I prepare the lparam and wparam for the WM_KEY... messages ? And how do I get the "time" member of the structure ? Should I just use the current system time ? I understand that WinBatch works, but I would still like to implement the code myself. Is there any way this can be done ?

>> How do I prepare the lparam and wparam for the WM_KEY... messages ?
Check the documentation of the WM_KEYxxx messages.  It is comprehensive.

>> Should I just use the current system time ?
Sounds good enough.

Unlock this solution and get a sample of our free trial.
(No credit card required)


  How to simulate "ctrl+alt+del" sequence to start winnt login with user intervention. Above piece of code works for all key combination except this one.
Hi Shankar_ms! WIN works in the next sequence:
kbd interrupt -> kbd driver -> keybd_event() -> kbd message

"ctrl+alt+del" is processed in kbd driver only (system level) and
doesn't go to keybd_event() (there is no kbd message for this
key combination). I think to simulate "ctrl+alt+del" you have
to use kbd driver service.

Hi vladmir

Do you know how to doit? I am new to keyboard driver?

I work in Win95/98 and have no info about NT "ctrl+alt+del".
But I think you will not have a big problem to find anything
suitable in Internet. In the other side if you can find WIN NT
API call that have at the same result as "ctrl+alt+del"- this is
a better way.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.