When keybrd_event and SendMessage(WM_KEYDOWN don't work???

I am trying to control Google Earth.  I would like to use SendMessage(handle, WM_KEYDOWN, VK_?, 0) to control it without having to set focus using BringWindowToTop(handle), but for some reason it only works if it is on top.  The real problem is that I can't get it to accept a Shift-?.  I can press CTRL/ALT key combinations, but it when I do VK_SHIFT then VK_UP, it just interperates it as an up, not shift-up.  If I hold shift on my keyboard while it sends up it does a shift-up.  I have tried using WM_SYSKEYDOWN, VK_LSHIFT, VK_RSHIFT, and even keybrd_event(), but they all have the same problem.  What is going on here?
rpmccormi77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robert_marquardtCommented:
Show some code.
BigRatCommented:
>>but for some reason it only works if it is on top

Strictly speaking these messages can only be send/processed when the control has focus. Most programs don't bother to check (like me), but some do. Do a SetFocus(hWnd) before sending anything.

Secondly to simulate input the *correct* way is to use the SendInput() procedure which actually sets the input through the keyboard driver/buffer system. I suspect that the GetKeyState() function is NOT returning the shift status and so not seeing Shift+Up.

See Simulating Input at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/aboutkeyboardinput.asp

HTH
rpmccormi77Author Commented:
I have never seen any code using SendInput.  Could you post some?  I will look into it anyway.  keybrd_event is the old version though and it did not work.  FYI:  SetFocus() does not work, only BringWindowToTop() works.

Here my code as requested:

var
  Handle: hWND;
 begin
  Handle := Windows.GetWindow(Nil, 'Google Earth');
  Windows.BringWindowToTop(Handle);
  Windows.SendMessage(Handle, WM_KEYDOWN, VK_SHIFT, 0);
  Windows.SendMessage(Handle, WM_KEYDOWN, VK_LEFT, 0);
  Sleep(1000);
  Windows.SendMessage(Handle, WM_KEYUP, VK_LEFT, 0);
  Windows.SendMessage(Handle, WM_KEYUP, VK_SHIFT, 0);
 end;

The above code simulates LEFT, not Shift-LEFT?  If I change VK_SHIFT to VK_CONTROL, it does CTRL-LEFT fine.  I have tried using WM_SYSKEYUP/DOWN for the VK_SHIFT with no luck.  I have also tried using keybrd_event(VK_SHIFT,0,0,0) with no luck.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

rpmccormi77Author Commented:
I found this SendInput code.  It didn't work at all.  It doesn't even send UP.

procedure TForm1.Button1Click(Sender: TObject);
var
  KeyInputs: array of TInput;
  KeyInputCount: Integer;

  procedure KeybdInput(VKey: Byte; Flags: DWORD);
   begin
    Inc(KeyInputCount);
    SetLength(KeyInputs, KeyInputCount);
    KeyInputs[KeyInputCount - 1].Itype := INPUT_KEYBOARD;
    with  KeyInputs[KeyInputCount - 1].ki do
     begin
      wVk := VKey;
      wScan := MapVirtualKey(wVk, 0);
      dwFlags := KEYEVENTF_EXTENDEDKEY;
      dwFlags := Flags or dwFlags;
      time := 0;
      dwExtraInfo := 0;
     end;
   end;
 begin
  Windows.BringWindowToTop(FindWindow(Nil, 'Google Earth'));
  KeybdInput(VK_SHIFT, 0);
  KeybdInput(VK_UP, 0);
  Sleep(1000);
  KeybdInput(VK_UP, KEYEVENTF_KEYUP);
  KeybdInput(VK_SHIFT, KEYEVENTF_KEYUP);
  SendInput(KeyInputCount, KeyInputs[0], SizeOf(KeyInputs[0]));
 end;
rpmccormi77Author Commented:
Ok, here is my latest code using SendInput:

[code]
procedure TForm1.Button1Click(Sender: TObject);
var
  intGEHandle, intOldHandle: hWND;
  GInput: array [0..0] of tagINPUT; //GENERALINPUT;
  // doesn't have to be array :)
 begin
  intOldHandle := GetForegroundWindow;
  intGEHandle := FindWindow(Nil, 'Google Earth');
  SetForegroundWindow(intGEHandle);

  GInput[0].Itype          := INPUT_KEYBOARD;
  GInput[0].ki.wVk         := VK_RSHIFT;
  GInput[0].ki.wScan       := 0;
  GInput[0].ki.dwFlags     := 0;
  GInput[0].ki.time        := 0;
  GInput[0].ki.dwExtraInfo := 0;
  SendInput(1, GInput[0], SizeOf(GInput));

  GInput[0].Itype          := INPUT_KEYBOARD;
  GInput[0].ki.wVk         := VK_LEFT;
  GInput[0].ki.wScan       := 0;
  GInput[0].ki.dwFlags     := 0;
  GInput[0].ki.time        := 0;
  GInput[0].ki.dwExtraInfo := 0;
  SendInput(1, GInput[0], SizeOf(GInput));

  Sleep(1000);

  GInput[0].Itype          := INPUT_KEYBOARD;
  GInput[0].ki.wVk         := VK_LEFT;
  GInput[0].ki.wScan       := 0;
  GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;
  GInput[0].ki.time        := 0;
  GInput[0].ki.dwExtraInfo := 0;
  SendInput(1, GInput[0], SizeOf(GInput));

  GInput[0].Itype          := INPUT_KEYBOARD;
  GInput[0].ki.wVk         := VK_RSHIFT;
  GInput[0].ki.wScan       := 0;
  GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;
  GInput[0].ki.time        := 0;
  GInput[0].ki.dwExtraInfo := 0;
  SendInput(1, GInput[0], SizeOf(GInput));

  SetForegroundWindow(intOldHandle);
 end;
[/code]




This actually does send RSHIFT-LEFT for 1000ms as it should.  It still has a bug though...  the RSHIFT is not released down.  If I return to Google Earth and press the arrow keys it acts as if shift is down.  If I press & release my left-shift button it does not fix it.  If I press & release my right shift button it does fix it.

I don't think RSHIFT-LEFT worked with the SendMessage() technique, but the keybrd_event and SendInput methods both work sending RSHIFT, they just won't release it.  using VK_SHIFT or VK_LSHIFT does not work with any method.

What is going on here?
rpmccormi77Author Commented:
Note:  I just tested the code out on note-pad.  I found out that it has the exact same problem, so it is not just Google Earth.  I also found out that the problem does not exist with SHIFT+[letter] combinations, it only exists with SHIFT+[arrow-key] combinations.

Hasn't anybody encountered this problem before?  What is the work-around?
rpmccormi77Author Commented:
Another follow up:

If I add the KEYEVENTF_EXTENDEDKEY:
    GInput.ki.dwFlags   := KEYEVENTF_EXTENDEDKEY
...then VK_SHIFT and VK_LSHIFT work just like VK_RSHIFT (that is the press but dont release).  Wait, it gets even stranger...

So, I can virtually-press shift using VK_RSHIFT with or without the flag, or by using VK_SHIFT or VK_LSHIFT but it has to be with the flag.  Then, if I virtually-release the shift using:
    GInput.ki.dwFlags   := KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY
...the the left-shift key gets stuck down (I have to press the left-shift on my keyboard to restore normality).  If I don't use the flag like this:
    GInput.ki.dwFlags   := KEYEVENTF_KEYUP
...then it is the right-shift key that gets stuck down.

What the heck is going on here?
robert_marquardtCommented:
For arrow keys you may have to add the KEYEVENTF_EXTENDEDKEY flag. That flag distinguishes between numpad arrow keys and separate arrow keys.
Also set the wScan element aka second parameter of keybd_event. The documentation telling that it can be set to 0 is wrong.
To convert virtual keycode <-> scancode use MapVirtualKey.
rpmccormi77Author Commented:
SOLUTION:  Posting above gave me an idea...  do both!

This is a totally lame solution, but unless I find any other "special-cases" like the Shift+Arrow case, it works.

[code]
procedure TForm1.Button1Click(Sender: TObject);
var
  intGEHandle, intOldHandle: hWND;
 begin
  intOldHandle := GetForegroundWindow;
  intGEHandle := FindWindow('QWidget', 'Google Earth');
  SetForegroundWindow(intGEHandle);

  SimulateKeyDown(VK_SHIFT);
  SimulateKeyDown(VK_LEFT);
  Sleep(1000);
  SimulateKeyUp(VK_LEFT);
  SimulateKeyUp(VK_SHIFT);

  SetForegroundWindow(intOldHandle);
 end;

procedure SimulateKeyDown(MyKey: Cardinal);
var
  MyInput: tagINPUT;
 begin
  MyInput.Itype          := INPUT_KEYBOARD;
  MyInput.ki.wVk         := MyKey;
  MyInput.ki.wScan       := MapVirtualKey(MyKey, 0);
  if (MyKey = VK_SHIFT) then
    MyInput.ki.dwFlags   := KEYEVENTF_EXTENDEDKEY
  else
    MyInput.ki.dwFlags   := 0;
  MyInput.ki.time        := 0;
  MyInput.ki.dwExtraInfo := 0;
  SendInput(1, MyInput, SizeOf(MyInput));
 end;

procedure SimulateKeyUp(MyKey: Cardinal);
var
  MyInput: tagINPUT;
 begin
  MyInput.Itype          := INPUT_KEYBOARD;
  MyInput.ki.wVk         := MyKey;
  MyInput.ki.wScan       := MapVirtualKey(MyKey, 0);
  MyInput.ki.dwFlags     := KEYEVENTF_KEYUP;
  MyInput.ki.time        := 0;
  MyInput.ki.dwExtraInfo := 0;
  SendInput(1, MyInput, SizeOf(MyInput));

  if (MyKey = VK_SHIFT) then
   begin
    MyInput.ki.dwFlags     := KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY;
    SendInput(1, MyInput, SizeOf(MyInput));
   end;
 end;
[/code]



I'm still interested in knowing why I have to use "KEYEVENTF_EXTENDEDKEY" for [Shift]+[Arrow] combinations, and why I have to send both "KEYEVENTF_KEYUP" and "KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY" to get an extended [Shift] to release.  Points will be awarded for anything that will make my code cleaner or a list of other "special cases" and their work arounds.
robert_marquardtCommented:
You send Shift  Down with KEYEVENTF_EXTENDEDKEY so logically you have to send the Shift Up with the same Flag.
KEYEVENTF_EXTENDEDKEY distinguishes between left and right keys.
So it is logical that the OS gets confused if it gets left Shift down and right Shift up.
BigRatCommented:
Sorry I had to go away yesterday afternoon. It seems like we've done quite a lot. I would however like to mention a Tip at Torry's for this :-

http://www.swissdelphicenter.ch/torry/showcode.php?id=220

rpmccormi77Author Commented:
Thanks for the replies...

I tried all of the functions on that Torry page before, but none of them would sucessfully send a Shift+Arrows (again, CTRL-Arrow, ALT-Arrow, and Shift-Letter all work fine using all methods I have tried, it is just a Shift+Arrow problem).

Even if I use the KEYEVENTF_EXTENDEDKEY with VK_LSHIFT or VK_RSHIFT, I still have to send a WM_KEYUP both with and without the KEYEVENTF_EXTENDEDKEY flag in order for the key to actually be released.  Why?

It seems that all if these:
1) GInput[0].ki.wVk         := VK_RSHIFT;
    GInput[0].ki.dwFlags     := 0;
2) GInput[0].ki.wVk         := VK_RSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_EXTENDEDKEY;
3) GInput[0].ki.wVk         := VK_LSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_EXTENDEDKEY;
4) GInput[0].ki.wVk         := VK_SHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_EXTENDEDKEY;
but BOTH shift buttons down.

All of these raise the left shift button:
1) GInput[0].ki.wVk         := VK_RSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;
2) GInput[0].ki.wVk         := VK_LSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;
3) GInput[0].ki.wVk         := VK_SHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;

All of these raise the right shift button:
1) GInput[0].ki.wVk         := VK_RSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;
2) GInput[0].ki.wVk         := VK_LSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;
3) GInput[0].ki.wVk         := VK_SHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP;

All of these raise the left shift button:
1) GInput[0].ki.wVk         := VK_RSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY;
2) GInput[0].ki.wVk         := VK_LSHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY;
3) GInput[0].ki.wVk         := VK_SHIFT;
    GInput[0].ki.dwFlags     := KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY;

And these don't do anything:
1) GInput[0].ki.wVk         := VK_SHIFT;
    GInput[0].ki.dwFlags     := 0;
2) GInput[0].ki.wVk         := VK_LSHIFT;
    GInput[0].ki.dwFlags     := 0;

Why?

Also, I tried plaing around with ThreadID stuff and got it to work sending keys to my program after binding the thread inputs, but the Shift+Arrow thing still didn't work (and GE still needed focus to recieve the keys, so it is no better than SendInput).  My only working solution is by using SendInput and KEYEVENTF_EXTENDEDKEY, but this requires me to use 2 SendInputs to release the shift key (one with and and without the KEYEVENTF_EXTENDEDKEY).

I still have not seen any code that can send even VK_LEFT alone to GoogleEarth without it having focus (not a requirement, but it would be nice).
rpmccormi77Author Commented:
I hate how you can not edit or preview your posts on this site :(

of
put
ignore the second list-group
BigRatCommented:
There are two things I don't understand.

1) Why is the flag KEYEVENTF_EXTENDEDKEY set? (*see below)
2) Why in SimulateKeyUp SendInput is done twice on VK_SHIFT?

The old way with keybd_event is deprecated, one should use SendInput, but there are plenty of examples on the web where the sequence is performed which are equivalent to SendInput, eg:

http://www.delphipages.com/tips/thread.cfm?ID=78
http://delphi.about.com/od/adptips2004/a/bltip0604_4.htm
 
even in VB

http://www.experts-exchange.com/Programming/Q_20879518.html


* This site implements a C++ class for this and defines the Extended keys :-

http://cvs.sourceforge.net/viewcvs.py/keepass/keepass/Util/SendKeys.cpp?rev=1.3

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rpmccormi77Author Commented:
Thanks for your comments.  First let me answer your questions:

1) Otherwise Shift+Left only sends Left.
2) Otherwise either the Left-Shift or Right-Shift is not released.  For some reason using KEYEVENTF_EXTENDEDKEY holds both Shift buttons, but it will only release one button (and not using it will release the other), therefore I have to do SendInput twice (once with the flag and once without).

Do the above answers make any logical since: No, but it is how I know it to work via much experimentation.

Now, your first two posts are things I have seen before and I don't find helpful.  However, the third post lists the "extended keys" which is very interesting.

    VK_UP,
    VK_DOWN,
    VK_LEFT,
    VK_RIGHT,
    VK_HOME,
    VK_END,
    VK_PRIOR, // PgUp
    VK_NEXT,  //  PgDn
    VK_INSERT,
    VK_DELETE

According to this list, shift is not an extended key, but the direction arrows all are.  Since I could always send LEFT and just had trouble sending Shift-LEFT, I assumed shift was the problem.  Let me try not setting shift as an extended key, but setting LEFT as an extended key instead.  Hopfully that will successfully send Shift-Left, and allow both to be released with one SendInput command for each (instead of needing two just to release shift).  I'll give it a try.
rpmccormi77Author Commented:
That worked!  Yay!

Here's my functions now:

[code]
procedure SimulateKeyDown(MyKey: Cardinal);
var
  MyInput: tagINPUT;
 begin
  MyInput.Itype          := INPUT_KEYBOARD;
  MyInput.ki.wVk         := MyKey;
  MyInput.ki.wScan       := MapVirtualKey(MyKey, 0);

  if (MyKey = VK_UP)
  or (MyKey = VK_DOWN)
  or (MyKey = VK_LEFT)
  or (MyKey = VK_RIGHT)
  or (MyKey = VK_HOME)
  or (MyKey = VK_END)
  or (MyKey = VK_PRIOR)
  or (MyKey = VK_NEXT)
  or (MyKey = VK_INSERT)
  or (MyKey = VK_DELETE) then
    MyInput.ki.dwFlags   := KEYEVENTF_EXTENDEDKEY
  else
    MyInput.ki.dwFlags   := 0;
  MyInput.ki.time        := 0;
  MyInput.ki.dwExtraInfo := 0;

  SendInput(1, MyInput, SizeOf(MyInput));
 end;

procedure SimulateKeyUp(MyKey: Cardinal);
var
  MyInput: tagINPUT;
 begin
  MyInput.Itype          := INPUT_KEYBOARD;
  MyInput.ki.wVk         := MyKey;
  MyInput.ki.wScan       := MapVirtualKey(MyKey, 0);

  if (MyKey = VK_UP)
  or (MyKey = VK_DOWN)
  or (MyKey = VK_LEFT)
  or (MyKey = VK_RIGHT)
  or (MyKey = VK_HOME)
  or (MyKey = VK_END)
  or (MyKey = VK_PRIOR)
  or (MyKey = VK_NEXT)
  or (MyKey = VK_INSERT)
  or (MyKey = VK_DELETE) then
    MyInput.ki.dwFlags   := KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY
  else
    MyInput.ki.dwFlags     := KEYEVENTF_KEYUP;

  MyInput.ki.time        := 0;
  MyInput.ki.dwExtraInfo := 0;

  SendInput(1, MyInput, SizeOf(MyInput));
 end;
[/code]

Ok, I'm awarding points for the list of actual extended keys ;)

I do have one more question though: There seems to be no VK_[code] for the equals key ("=").  Is VK_OEM_PLUS actually "=" and only "+" when shift is held?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.