Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SendInput

0 views
Skip to first unread message

Tim Orr

unread,
Oct 28, 2002, 3:00:50 PM10/28/02
to
I'm trying to generate the keystroke sequence SHIFT-RIGHTARROW using the
SendInput api. It appears that Windows gets in the way by posting a WM_KEYUP
message on the VK_SHIFT key before posting the WM_KEYDOWN for VK_RIGHT -
which means that the app sees an unshifted arrow key, not a shifted one.

Does anyone know how to make this work?

Tim Orr


Ryan Schmitz

unread,
Oct 29, 2002, 12:05:07 PM10/29/02
to
I've tried to do similar and I can't seem to make it work right for any key
that needs shift to be held...if anyone has an answer to this I'd like to
hear it as well. I've tried doing it using the virtual key codes and
scancodes at the same time, or just one or the other at once, but none seem
to matter. It never acknowledges the shift key it seems...

Ryan


Ritchie

unread,
Oct 29, 2002, 12:59:30 PM10/29/02
to
"Tim Orr" <timo...@hotmail.com> wrote in message news:SHgv9.147060$zE6.4...@rwcrnsc51.ops.asp.att.net...

Tim,
Not what you asked, but I can do this using keybd_event()

keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_RIGHT, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

Note the use KEYEVENTF_EXTENDEDKEY as the cursor keys send two characters.
If you run this example below and press various keys (including the cursors)
you'll see what I mean:-

do {
printf("%d\n", c = getch());
} while (c != '\r');

My guess is that you need to find the equivalent of KEYEVENTF_EXTENDEDKEY in
SendInput().

HTH
--
Ritchie
Undo address for mail


Tim Orr

unread,
Oct 30, 2002, 4:53:29 PM10/30/02
to
Ritchie, this is way cool, thanks. Only problem I had is that we need
KEVEEVENTF_EXTENDEDKEY in the keyup call as well:

keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

I got SendInput to work as well, here's the whole routine:

void handleKey(WORD vkKeyCode, bool keyIsDown)
{
INPUT key;
key.type = INPUT_KEYBOARD;
key.ki.wVk = vkKeyCode;
key.ki.wScan = 0;
key.ki.dwFlags = keyIsDown ? 0 : KEYEVENTF_KEYUP;
key.ki.time = 0;
key.ki.dwExtraInfo = 0;

// Need to mark the following keys as extended. Otherwise, SHIFT
// behaves very strangely with theses keys.
switch(vkKeyCode)
{
case VK_RIGHT:
case VK_LEFT:
case VK_UP:
case VK_DOWN:
case VK_END:
case VK_HOME:
case VK_INSERT:
case VK_DELETE:
case VK_NEXT:
case VK_PRIOR:
key.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
break;
}

UINT nEvents = SendInput(1, &key, sizeof(key));
}

"Ritchie" <rit...@commanddoline.co.uk> wrote in message
news:3dbecc9f$0$1288$cc9e...@news.dial.pipex.com...

Tim Orr

unread,
Oct 30, 2002, 4:56:45 PM10/30/02
to
Ryan,
Ritchie led me to the correct solution, the whole routine follows:

void handleKey(WORD vkKeyCode, bool keyIsDown)
{
INPUT key;
key.type = INPUT_KEYBOARD;
key.ki.wVk = vkKeyCode;
key.ki.wScan = 0;
key.ki.dwFlags = keyIsDown ? 0 : KEYEVENTF_KEYUP;
key.ki.time = 0;
key.ki.dwExtraInfo = 0;

// Need to mark the following keys as extended. Otherwise, SHIFT
// behaves very strangely with theses keys.
switch(vkKeyCode)
{
case VK_RIGHT:
case VK_LEFT:
case VK_UP:
case VK_DOWN:
case VK_END:
case VK_HOME:
case VK_INSERT:
case VK_DELETE:
case VK_NEXT:
case VK_PRIOR:
key.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
break;
}

UINT nEvents = SendInput(1, &key, sizeof(key));
}

void doIt()
{
// fake a user pressing SHIFT-right arrow
handleKey(VK_SHIFT, true);
handleKey(VK_RIGHT, true);
handleKey(VK_RIGHT, false);
handleKey(VK_SHIFT, false);
}

"Ryan Schmitz" <ry...@earthsignal.com> wrote in message
news:7dzv9.55256$Sk6.5...@news1.telusplanet.net...

Ritchie

unread,
Oct 30, 2002, 5:10:43 PM10/30/02
to
"Tim Orr" <timo...@hotmail.com> wrote in message news:txYv9.18438$wG.6...@rwcrnsc51.ops.asp.att.net...

> Ritchie, this is way cool, thanks. Only problem I had is that we need
> KEVEEVENTF_EXTENDEDKEY in the keyup call as well:
>
> keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

I'm glad you got it working. BTW, I use keybd_event() because try as I
might I cannot get anything to compile using SendInput! - way too many
structures for me and I can't quite get my head around the documention.

I've kept the code you posted, and I'll play with. Feel free to post
(to NG or email) any other examples.

Cheers,

0 new messages