Discussion Locked

How to stuff the keyboard?

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to programitically stuff the keyboard under windows:
1> The program is guarenteed to be the active window.
2> The stuffing will have to be done by another program/process without user activity. In other words it is not a keyboard short-cut issue.
3> The program/process above is likely to be in java, but is not necessarily in Java.

SUNIL GUPTA>

4 replies

by c-wundram

in the win32 api you use the SendInput() api to simulate keystrokes or mouse-events. This will send the event to the active window, whatever process owns it. You can also use SendMessage to send keystrokes to a particular window, but you would need the window handle to be able to do that.

Here is an example in C that waits 5 seconds, then sends a bunch of keystrokes to whatever happens to be the active window at the time.

---------------

#define _WIN32_WINNT 0x500

#include
#include
#include

void main(int argc, char* argv[])
{
INT nCount;

Sleep(5000);

INPUT arInput[26];

for (nCount = 0; nCount < 26; nCount++)
{
CHAR cKey = 'A' + nCount;
INT nKey = cKey - 'A' + 65;

arInput[nCount].type =INPUT_KEYBOARD;
arInput[nCount].ki.wVk = nKey;
arInput[nCount].ki.time = 0;
arInput[nCount].ki.wScan = 0;
arInput[nCount].ki.dwExtraInfo = 0;
arInput[nCount].ki.dwFlags = 0;
}
SendInput(26,arInput,sizeof(INPUT));

}

--------------

This discussion is closed to new replies.