Disable Windows Shortcuts in Delphi Application

This code disables:

  • Ctrl+Esc (show start menu)
  • Alt+Tab (task switch)
  • Alt+Esc (task switch)
  • Win (show start menu)
  • Win+Tab (3D task switch)
  • Win+D, Win+M, Win+Space, Win+Arrows, Win+P, Win+U, Win+E, Win+F, Win+Digit
function LowLevelKeyboardProc(nCode: integer; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
type
PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
TKBDLLHOOKSTRUCT = record
vkCode: cardinal;
scanCode: cardinal;
flags: cardinal;
time: cardinal;
dwExtraInfo: Cardinal;
end;
PKeyboardLowLevelHookStruct = ^TKeyboardLowLevelHookStruct;
TKeyboardLowLevelHookStruct = TKBDLLHOOKSTRUCT;
const
LLKHF_ALTDOWN = $20;
var
hs: PKeyboardLowLevelHookStruct;
ctrlDown: boolean;
begin
if nCode = HC_ACTION then
begin
hs := PKeyboardLowLevelHookStruct(lParam); ctrlDown := GetAsyncKeyState(VK_CONTROL) and $8000 <> 0; if (hs^.vkCode = VK_ESCAPE) and ctrlDown then Exit(1); if (hs^.vkCode = VK_TAB) and ((hs^.flags and LLKHF_ALTDOWN) <> 0) then Exit(1); if (hs^.vkCode = VK_ESCAPE) and ((hs^.flags and LLKHF_ALTDOWN) <> 0) then Exit(1); if (hs^.vkCode = VK_LWIN) or (hs^.vkCode = VK_RWIN) then Exit(1);
end;
result := CallNextHookEx(0, nCode, wParam, lParam);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardProc, 0, 0);
end;
Advertisements

Windows Service Application Tips

From Vista, Service applications run on different session, not on user’s session.

  • If Shellexecute something, it won’t be visible to user, but it will work background.
  • If Beep, it won’t be heard.

You need the main form. It is not visible though. Change its DisplayName property. It is the Service’s DisplayName.