Can task-switching keyboard shortcuts be disabled in W7 using Delphi?
Asked Answered
C

4

10

MY application has had a mode for years where the customer can 'disable access to the OS'. Obviously this feature goes against the grain (at least as far as Windows is concerned) but there are installations where my App is the only program that should ever be visibile to a machine operator amd in this case such a feature is useful.

The technigue I used was built from several 'layers':

  1. Hide the taskbar and button.
  2. Disable task-switching.
  3. Disable my main form system icons.

To disable the taskbar I used:

// Get a handle to the taskbar and its button..
Taskbar := FindWindow('Shell_TrayWnd', Nil);
StartButton := FindWindow('Button', Nil);

// Hide the taskbar and button
if Taskbar <> 0 then
  ShowWindow( Taskbar, SW_HIDE );
if StartButton <> 0 then
  ShowWindow( StartButton, SW_HIDE );

// Set the work area to the whole screen
R := Rect( 0,0,Screen.Width,Screen.Height );
SystemParametersInfo(
  SPI_SETWORKAREA,
  0,
  @R,
  0 );

This worked well and still seems fine on W7. Researching how to disable task-switching some years ago turned up the only technique of 'pretending' that your App is a screen saver (other than terrible things like renaming your app to 'explorer.exe' and booting into it etc):

procedure EnableTaskSwitching( AState : boolean );
// Enables / disables task switching
begin
  SystemParametersInfo(
    SPI_SCREENSAVERRUNNING,
    Cardinal( not AState),
    nil,
    0 );
end;

Not surprisingly this seems to have no effect in W7 (I think it works in XP etc). Does anyone know of another, better, way of enabling / disabling Alt-Tab (and other special windows keys) from working?

Claymore answered 1/5, 2011 at 18:46 Comment(1)
and what about ctrl-alt-del ?Motivity
A
12

If found a solution:

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;

This disables (as you can see!)

  • 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, ...
  • Almost any combination including the Windows key (but not all, e.g. Win+L)
Aecium answered 1/5, 2011 at 19:9 Comment(6)
@Andreas did you check all Win+key combinations? From my experience some combinations are not intercepted (Win-L, for example).Dolley
@Eugene: Oh, my mistake. Win+L is of course handled differently for security reasons. But my proposition still holds with "all" replaced by "almost all". Thanks for pointing that out!Aecium
@Andreas: This code can't catch the Ctrl-Alt-Del, which can be used to start Task Manager that can start an other app that kills your nice hook.Dashiell
@Jeroen: I know. But at least it answers the question in the title of this question...Aecium
Aren't there "group policy" settings that take care of a lot of that stuff, too? Brian, check Server Fault for ways that sysadmins lock down a computer with OS settings instead of relying on their software vendors to do it with each individual software package.Delphina
Now I have received four downvotes in a row. Apparently I have made someone upset. I am sorry, but since I don't know who you are, I don't know what I've done.Aecium
N
6

As David has pointed out, this is called "Kiosk Mode". A couple of good articles (part 1 and part 2) can be found on About.com.

Nostrum answered 1/5, 2011 at 21:16 Comment(2)
Using the tricks on the first link won't work: Windows 7 does not allow you to catch Ctrl-Alt-Del in that way any more. The second link requires you to replace Gina.dll which replaces the complete login interface (including any 3rd party that for instance enables fingerprint logon) which can be a big problem.Dashiell
@Jeroen "kiosk mode" usually means complete control over the system, so replacing a GINA dll is not a problem. Yet the OP doesn't need kios mode, as it turned out.Dolley
D
1

There is Windows Embedded Standard 7 that you can package in a way that has a true kiosk mode.

Dashiell answered 2/5, 2011 at 7:3 Comment(1)
Thanks, I knew about WE. My needs are not a full 'kiosk' mode but to just discourage curious or stupid fingers.Claymore
D
0

dWinLock also provides a solution. IIRC, they install a service that can stop Ctrl+Alt+Del.

Demission answered 2/5, 2011 at 9:0 Comment(3)
I think dwinLock is not working properly with Win7 though. Although they state now that Win7 is supported, so I guess there's a new version. I've used it, and it's very very slick.Biconvex
@Warren: A useful solution. I see they mention '32-bit' It's not clear whether this a restriction on the OS or the App so I've emailed them.Claymore
@Warren: Yes, it supports a Delphi 32-bit app running under W7/64Claymore

© 2022 - 2024 — McMap. All rights reserved.