Re: SendInput() : failed to simulate keyboard input
From: Chris Tacke, eMVP (ctacke_at_spamfree-opennetcf.org)
Date: 06/23/04
- Next message: Michael J. Salamone [eMVP]: "Re: Passing arguments to a running application"
- Previous message: Bruce Eitman \(eMVP\): "Re: Intel x86 and PocketPC"
- In reply to: carol: "SendInput() : failed to simulate keyboard input"
- Next in thread: carol: "Re: SendInput() : failed to simulate keyboard input"
- Reply: carol: "Re: SendInput() : failed to simulate keyboard input"
- Reply: Mohit Gupta: "Re: SendInput() : failed to simulate keyboard input"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 23 Jun 2004 10:02:54 -0400
Here's a snip from an app I wrote that does exactly what you're trying to
do:
<snip>
DWORD WINAPI CommReadThreadFunc(LPVOID lpParam)
{
enum { FIRST, SECOND, THIRD } state;
char ch;
DWORD dwBytesRead;
DWORD fdwCommMask;
TCHAR data[10];
state = FIRST;
SetCommMask (hCommPort, EV_RXCHAR);
while(hCommPort != INVALID_HANDLE_VALUE)
{
WaitCommEvent (hCommPort, &fdwCommMask, 0);
SetCommMask (hCommPort, EV_RXCHAR);
do {
SetLastError(ERROR_TIMEOUT);
if (!ReadFile(hCommPort, &ch, 1, &dwBytesRead, NULL)) {
if (GetLastError() != ERROR_INVALID_HANDLE)
RETAILMSG(1, (TEXT("Reading comms port.\r\n")));
ExitThread(0);
}
else if(dwBytesRead > 0) {
switch (state) {
case FIRST:
if (ch == VK_ESCAPE)
state = SECOND;
else {
switch (ch) {
case VK_RETURN:
// Process Enter
if(g_keytomouse)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
g_mousedown = FALSE;
}
else
{
keybd_event(VK_RETURN, 0, 0, 0);
}
break;
case 0x0a:
// CTRL-ENTER - this begins/ends drag and drop
if(g_keytomouse)
{
g_mousedown = !g_mousedown;
if(g_mousedown)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
else
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
}
break;
case VK_BACK:
// Process BackSpace
keybd_event(VK_BACK, 0, 0, 0);
break;
case VK_TAB:
// Process Tab
keybd_event(VK_TAB, 0, 0, 0);
break;
case VK_SPACE:
keybd_event(VK_SPACE, 0, 0, 0);
break;
case ',':
keybd_event(VK_COMMA, 0, 0, 0);
break;
case '.':
keybd_event(VK_DECIMAL, 0, 0, 0);
break;
case '/':
keybd_event(VK_SLASH, 0, 0, 0);
break;
case ';':
keybd_event(VK_SEMICOLON, 0, 0, 0);
break;
case ':':
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_SEMICOLON, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
break;
case '-':
keybd_event(VK_SUBTRACT, 0, 0, 0);
break;
default:
if((ch >= 65) && (ch <= 90)) // cap A - Z
{
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(ch, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
}
else if((ch >= 97) && (ch <= 122)) // a - z
{
keybd_event((ch - 32), 0, 0, 0);
}
else if((ch >= 48) && (ch <= 57)) // 0 - 9
{
keybd_event(ch, 0, 0, 0);
}
else
{
RETAILMSG(1, (TEXT("unmapped FIRST key 0x%x\r\n"), ch));
}
break;
}
}
break;
case SECOND:
if ((ch == VK_LWIN) || (ch == 0x4F))
state = THIRD;
else
state = FIRST;
break;
case THIRD:
if (ch == 0x41)
{
if(g_keytomouse)
{
mouse_event(MOUSEEVENTF_MOVE, 0, g_precision * -1, 0, 0);
}
else
{
keybd_event(VK_UP, 0, 0, 0);
}
}
else if (ch == 0x42)
{
if(g_keytomouse)
{
mouse_event(MOUSEEVENTF_MOVE, 0, g_precision, 0, 0);
}
else
{
keybd_event(VK_DOWN, 0, 0, 0);
}
}
else if (ch == 0x43)
{
if(g_keytomouse)
{
mouse_event(MOUSEEVENTF_MOVE, g_precision, 0, 0, 0);
}
else
{
keybd_event(VK_RIGHT, 0, 0, 0);
}
}
else if (ch == 0x44)
{
if(g_keytomouse)
{
mouse_event(MOUSEEVENTF_MOVE, g_precision * -1, 0, 0, 0);
}
else
{
keybd_event(VK_LEFT, 0, 0, 0);
}
}
else if (ch == 0x50)
{
// F1
keybd_event(VK_LWIN, 0, 0, 0);
// To give delay.
WaitForSingleObject(dummy, 100);
RETAILMSG(1, (TEXT("WIN Key Pressed\r\n")));
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
}
else if (ch == 0x51)
{
// F2
keybd_event(VK_LMENU, 0, 0, 0);
RETAILMSG(1, (TEXT("Menu Key Pressed\r\n")));
WaitForSingleObject(dummy, 100);
keybd_event(VK_LMENU, 0, KEYEVENTF_KEYUP, 0);
}
else if (ch == 0x52)
{
// F3
keybd_event(VK_LMENU, 0, 0, 0);
RETAILMSG(1, (TEXT("Task List Key Pressed\r\n")));
WaitForSingleObject(dummy, 100);
keybd_event(VK_TAB, 0, 0, 0);
WaitForSingleObject(dummy, 100);
keybd_event(VK_LMENU, 0, KEYEVENTF_KEYUP, 0);
}
else if (ch == 0x53)
{ //F4
g_keytomouse = !g_keytomouse;
g_mousedown = FALSE;
SetKeyToMouse(g_keytomouse);
}
else
{
RETAILMSG(1, (TEXT("unmapped THIRD key %i\r\n"), ch));
}
state = FIRST;
break;
}
}
else if ((dwBytesRead == 0) && (GetLastError() == ERROR_TIMEOUT) && (ch
== 0x1b)) {
// Process ESC
RETAILMSG(1, (TEXT("Processing ESC\r\n")));
keybd_event(VK_ESCAPE, 0, 0, 0);
state = FIRST;
}
} while(dwBytesRead == 1);
}
return 0;
}
</snip>
-- Chris Tacke, eMVP Co-Founder and Advisory Board Member www.OpenNETCF.org --- --- Principal Partner OpenNETCF Consulting www.OpenNETCF.com "carol" <carol@discussions.microsoft.com> wrote in message news:980BEBB4-0922-433F-8098-9AD6E1DE2B2D@microsoft.com... > Hi, guys > > i am doing a program useing EVC++ 4.0 for HP iPaq > > i received data from the com port, and i want to input the data like keyboard input to the foreground window, etc word, notepad. > > in my own program, i used SendInput() to convert the com port data to keyboard input, the return value showed that it has sent successfully, but the word screen, (the focus is set on it), didn't show up any data. > > i also tried keybd_event, same situation happened. :~( > > should i do anything more to let my program know where is the active window? It really confused me, thank you in advance for any suggestion. > > :)
- Next message: Michael J. Salamone [eMVP]: "Re: Passing arguments to a running application"
- Previous message: Bruce Eitman \(eMVP\): "Re: Intel x86 and PocketPC"
- In reply to: carol: "SendInput() : failed to simulate keyboard input"
- Next in thread: carol: "Re: SendInput() : failed to simulate keyboard input"
- Reply: carol: "Re: SendInput() : failed to simulate keyboard input"
- Reply: Mohit Gupta: "Re: SendInput() : failed to simulate keyboard input"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|