3

I'm writing a Windows executable in Dephi 2007 (32 bit) which sends keystrokes to another application.

Pressing a hotkey or hotkey combination from within the other application causes my app to send keystrokes to that application. My approach works fine if the hotkey combination does not contain Ctrl: i.e., F11 or Shift+F11 work properly but Ctrl+F11 causes my first call to SendInput to fail after handling the hotkey in my app.

I've tried my app with several different target applications and in both 64-bit Windows 7 and 32-bit Windows XP. This same failure pattern occurs in all cases I've tried.

On processing the hotkey combination, the code below attempts to send two Shift+Tab keys, followed by a Tab key to the target application. However, when Ctrl is part of the hotkey combination the first Shift+Tab key is never seen in the target application. (Subsequent keys are seen just fine.)

What am I missing?

EDIT: I've modified the code below (per my comment to Sertac) at "THE SOLUTION"...it now works fine, even with Ctrl as part of the hotkey sequence.

unit mainTEST;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Menus;

type
  T_Mainform = class(TForm)
  private
    procedure WMHotkey(var msg: TWMHotkey); message WM_HOTKEY;
    procedure DoRegisterHotkey;
    procedure SendTabKey(bShifted: boolean);
  public
    procedure AfterConstruction; override;
  end;

var
  _Mainform: T_Mainform;

implementation

{$R *.dfm}

const
  OurHotkeyID = 10;

procedure T_Mainform.AfterConstruction;
begin
  inherited;
  DoRegisterHotkey;
end;


procedure T_Mainform.DoRegisterHotkey;
const
  sHOTKEY = 'Shift+Ctrl+F11';         //Fails!
  //sHOTKEY = 'Shift+F11';              //Succeeds
//  sHOTKEY = 'F11';                    //Succeeds
  //sHOTKEY = 'Shift+Ctrl+F9';          //Fails!
  //sHOTKEY = 'Shift+F9';               //Succeeds
var
  AHotkey : TShortCut;
  hkModifiers: UINT;
  hkText: string;
  hkKey: Word;
  hkShiftState: TShiftState;
begin
  AHotkey := TextToShortcut(sHOTKEY);
  hkModifiers := 0;
  hkText := sHOTKEY;
  if Pos('Shift', hkText) > 0 then
    hkModifiers := hkModifiers or MOD_SHIFT;
  if Pos('Ctrl', hkText) > 0 then
    hkModifiers := hkModifiers or MOD_CONTROL;
  if Pos('Alt', hkText) > 0 then
    hkModifiers := hkModifiers or MOD_ALT;
  ShortCutToKey(AHotkey, hkKey, hkShiftState);

  if not RegisterHotkey(Handle, OurHotkeyID, hkModifiers, hkKey) then
    ShowMessageFmt( 'Unable to register hotkey %s.'#13#13+
        'LastError: %d', [GetLastError]);
end;


procedure T_Mainform.SendTabKey(bShifted: boolean);
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
  KeyInputCount := 0;

  if bShifted then
    KeybdInput(VK_SHIFT, 0);
  KeybdInput(VK_TAB, 0);
  KeybdInput(VK_TAB, KEYEVENTF_KEYUP);
  if bShifted then
    KeybdInput(VK_SHIFT, KEYEVENTF_KEYUP);
  SendInput(KeyInputCount, KeyInputs[0], SizeOf(KeyInputs[0]));
  Sleep(50);
end;


procedure T_Mainform.WMHotkey(var msg: TWMHotkey);
begin
  case msg.Hotkey of
    OurHotkeyID:
      if (Screen.ActiveForm = Self) or    //None of our modal dlgs is showing
          (GetLastActivePopup(Application.Handle) = Application.Handle)   //No system dlgs are modal (FileOpen, etc.)
          then begin

        Sleep(200);  //<== THE SOLUTION. Note: values of 150 or less failed!

        SendTabKey(True);       //True=Shift+Tab
        SendTabKey(True);
        Sleep(1000);            //...to observe UI effect
        SendTabKey(False);      //False=Tab
      end;
  end;
end;

end.
Ilya
  • 27,538
  • 18
  • 104
  • 148
Mark Wilsdorf
  • 751
  • 1
  • 5
  • 16

1 Answers1

1

My guess is, the target application is reading the state of the Ctrl key, and sensing it down, actually is responding to a Shift+Ctrl+Tab as opposed to a Shift+Tab.

Send a 'ctrl' key up before the Shift+Tab when your hotkey includes the 'ctrl' key.

begin
  KeyInputCount := 0;

  if CtrlInHotkey then              // to be implemented
    KeybdInput(VK_CONTROL, KEYEVENTF_KEYUP);

  if bShifted then
    KeybdInput(VK_SHIFT, 0);
  ...
Ilya
  • 27,538
  • 18
  • 104
  • 148
Sertac Akyuz
  • 52,752
  • 4
  • 91
  • 157
  • Excellent idea...thanks! It didn't end up being the exact solution, but in testing your idea I stumbled upon the solution as a timing problem, so I'm accepting your answer, and have modified the code to show that adding Sleep(200) ahead of the keystroke sequence solved the problem....now I just need to explore why and different circumstances. (It surprises me that 200 ms. was required, and that smaller values didn't work reliably.) – Mark Wilsdorf Jul 03 '14 at 01:04
  • @Mark - You're welcome. I'm a bit puzzled by what you observe. The sleep, being a solution, suggests that you may be waiting for the 'ctrl' key up. But in that case what I proposed should work reliably. – Sertac Akyuz Jul 03 '14 at 01:07
  • I'm puzzled too. I just now realized a similar app I wrote long ago has no such problem with Ctrl in the hotkey sequence (doesn't need a long Sleep() before keystrokes are sent). It uses a PeekMessage() loop to check whether the target app has processed each SendInput keystroke I send, which the above test app does not because I don't have the target's window handle. I'll have to experiment...and I'll comment back here when I learn something. Thanks again. – Mark Wilsdorf Jul 03 '14 at 11:32
  • I've tried several approaches and for now I've given up trying to figure out why the Sleep(200) works and/or is needed. Maybe someday I'll stumble upon a reason. Until then, adding this to my code doesn't hinder much. – Mark Wilsdorf Jul 04 '14 at 22:13