Author: Tomas Rutkauskas I would like to be able to minimize my application if the user presses [ALT] + [TAB]. Will I need to hook the keyboard for this? There is lot of code around to disable [ALT] [TAB] but nothing to detect it. Answer: This works on WinNT SP3+, Win2K and WinXP: 1 { ... } 2 var 3 FHook: HHook = 0; 4 5 const 6 WH_KEYBOARD_LL = 13; 7 LLKHF_ALTDOWN = KF_ALTDOWN shr 8; 8 9 type 10 tagKBDLLHOOKSTRUCT = packed record 11 vkCode: DWord; 12 scanCode: DWord; 13 flags: DWord; 14 time: DWord; 15 dwExtraInfo: PDWord; 16 end; 17 TKBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT; 18 PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT; 19 { ... } 20 21 function LowLevelKeyboardProc(HookCode: Longint; MessageParam: WParam; 22 StructParam: LParam): DWord; stdcall; 23 var 24 SwitchingTask: Boolean; 25 P: PKBDLLHOOKSTRUCT; 26 begin 27 SwitchingTask := False; 28 if (HookCode = HC_ACTION) then 29 case (MessageParam) of 30 WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP: 31 begin 32 P := PKBDLLHOOKSTRUCT(StructParam); 33 SwitchingTask := ((P.VKCode = VK_TAB) and (P.Flags and LLKHF_ALTDOWN <> 34 0)) 35 or 36 ((P.VKCode = VK_ESCAPE) and ((P.Flags and LLKHF_ALTDOWN) <> 0)) or 37 ((P.VKCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) 38 and $8000) <> 0)); 39 end; 40 end; 41 if SwitchingTask then 42 begin 43 {If you want to disable task switch just uncomment next two lines} 44 // Result := 1; 45 // Exit; 46 {If not, put your code here...} 47 Application.Minimize; 48 end; 49 Result := CallNextHookEx(0, HookCode, MessageParam, StructParam); 50 end; 51 52 procedure SetHook; 53 begin 54 FHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardProc, Hinstance, 0); 55 end; 56 57 procedure UnHook; 58 begin 59 if FHook > 0 then 60 UnHookWindowsHookEx(FHook); 61 end; 62 63 procedure TMainForm.FormCreate(Sender: TObject); 64 begin 65 SetHook; 66 end; 67 68 procedure TMainForm.FormDestroy(Sender: TObject); 69 begin 70 UnHook; 71 end;