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.
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);
...