DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Please discuss general Delphi programming topics here.

DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Johan Theunissen » December 5th, 2009, 8:05 pm

How can one disable/enable the task switching keys in Windows (XP, Vista, 7) ??

Ctrl-Alt-Del can be disabled by changing a registry entry in XP, how are the other key combo's changed CTRL-ESC, ALT-F4, ALT-ESC ALT-Tab etc.

Code: Select all
procedure DisableCtrAltDel(Val: Boolean);
var
  MyReg: TRegistry;
begin
  Try
    MyReg := TRegistry.Create;
    with MyReg do
    begin
      RootKey := HKEY_CURRENT_USER;
      OpenKey('\Software\Microsoft\Windows\CurrentVersion\Policies\System', True);
      if Val then
         WriteString('DisableTaskMgr', '1')
      else
         DeleteValue('DisableTaskMgr');
      CloseKey;
    end;
  Finally
     MyReg.Free;
  End;
end;


Another way is to use a keyboard hook and set certain key entries to 0 - here is an example I got form the Microsoft Developer Network but I dont really know how to change it to Delphi code.

Windows NT 4.0 Service Pack 3 and Later and Windows 2000
Applications can disable ALT+TAB or CTRL+ESC by installing a low-level keyboard hook. A low-level keyboard hook (WH_KEYBOARD_LL) is installed by calling SetWindowsHookEx. For more information on Window hooks see the "Hooks" overview in the Platform SDK documentation.

The following is a sample low-level keyboard hook procedure that disables CTRL+ESC, ALT+TAB, and ALT+ESC:

Code: Select all
LRESULT CALLBACK LowLevelKeyboardProc (INT nCode, WPARAM wParam, LPARAM lParam)
{
    // By returning a non-zero value from the hook procedure, the
    // message does not get passed to the target window
    KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
    BOOL bControlKeyDown = 0;

    switch (nCode)
    {
        case HC_ACTION:
        {
            // Check to see if the CTRL key is pressed
            bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);
           
            // Disable CTRL+ESC
            if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
                return 1;

            // Disable ALT+TAB
            if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;

            // Disable ALT+ESC
            if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;

            break;
        }

        default:
            break;
    }
    return CallNextHookEx (hHook, nCode, wParam, lParam);
}



Can anyone please help?
Johan Theunissen
Active Member
Active Member
 
Posts: 9
Joined: November 6th, 2008, 7:26 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Kambiz » December 8th, 2009, 12:52 pm

Put the following code inside a DLL and export DisableWindowsUI and EnableWindowsUI functions. Then, call these functions from your application to install/uninstall the hook.

Code: Select all
var hKeybaordHook: HHOOK = 0;

function LowLevelKeyboardProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
type
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
  TKBDLLHOOKSTRUCT = packed record
    vkCode: DWORD;
    scanCode: DWORD;
    flags: DWORD;
    time: DWORD;
    dwExtraInfo: DWORD;
  end;
const
  LLKHF_ALTDOWN = $20;
var
  pkbhs: PKBDLLHOOKSTRUCT;
begin
  pkbhs := PKBDLLHOOKSTRUCT(lParam);
  if nCode = HC_ACTION then
  begin
    // Disable CTRL+ESC
    if (pkbhs^.vkCode = VK_ESCAPE) and WordBool(GetAsyncKeyState(VK_CONTROL) and $8000) then
    begin
      Result := 1;
      Exit;
    end;
    // Disable ALT+TAB
    if (pkbhs^.vkCode = VK_TAB) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
    begin
      Result := 1;
      Exit;
    end;
    // Disable ALT+ESC
    if (pkbhs^.vkCode = VK_ESCAPE) and LongBool(pkbhs^.flags and LLKHF_ALTDOWN) then
    begin
      Result := 1;
      Exit;
    end;
  end;
  Result := CallNextHookEx(hKeybaordHook, nCode, wParam, lParam);
end;

// export this function
function DisableWindowsUI: Boolean;
const
  WH_KEYBOARD_LL = 13;
begin
  if hKeybaordHook = 0 then
    hKeybaordHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardProc, HInstance, 0);
  Result := (hKeybaordHook <> 0)
end;

// export this function
function EnableWindowsUI: Boolean;
begin
  Result := False;
  if (hKeybaordHook <> 0) and UnhookWindowsHookEx(hKeybaordHook) then
  begin
    hKeybaordHook := 0;
    Result := True;
  end;
end;

I didn't test the code, but it should work. Please let me know if it didn't work as expected.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Johan Theunissen » December 21st, 2009, 5:21 pm

Hi,

I get a few errors when compiling.

hKeyboardHook := SetWindowsHookEx(WH_KEYBOARD, LowLevelKeyboardProc, HInstance, 0);
Compiler gives a Error on the second parameter(LowLevelKeyboardProc) of above line
[Error] HBWindows.pas(206): Incompatible types: 'Integer' and 'HRESULT'

HResult is declared as a LongInt in System unit
hKeyboardHook is type i.e. HHOOK = type LongWord in Windows Unit


In which delphi unit is LLKHF_ALTDOWN declared (I use Delphi 7)
[Error] HBWindows.pas(187): Undeclared identifier: 'LLKHF_ALTDOWN'

Another question: Why is it neccesary to use a DLL. I suppose a delphi package could also work or compiling the code in a normal unit linked into application??

Thanks for your help so far!
Johan Theunissen
Active Member
Active Member
 
Posts: 9
Joined: November 6th, 2008, 7:26 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Kambiz » December 22nd, 2009, 3:58 pm

Johan Theunissen wrote:hKeyboardHook := SetWindowsHookEx(WH_KEYBOARD, LowLevelKeyboardProc, HInstance, 0);
Compiler gives a Error on the second parameter(LowLevelKeyboardProc) of above line
[Error] HBWindows.pas(206): Incompatible types: 'Integer' and 'HRESULT'

I've missd the @ before LowLevelKeyboardProc, sorry.

Johan Theunissen wrote:HResult is declared as a LongInt in System unit
hKeyboardHook is type i.e. HHOOK = type LongWord in Windows Unit

Windows API receives a 4 bytes integer with both types, that matters.

Johan Theunissen wrote:In which delphi unit is LLKHF_ALTDOWN declared (I use Delphi 7)
[Error] HBWindows.pas(187): Undeclared identifier: 'LLKHF_ALTDOWN'

Apparently, nowhere. :)

Johan Theunissen wrote:Another question: Why is it neccesary to use a DLL. I suppose a delphi package could also work or compiling the code in a normal unit linked into application??

I've never used a global hook in bpl. Using a runtime package may works as dll, or may not.

I updated the original code.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Johan Theunissen » December 23rd, 2009, 8:57 pm

Changing the return parameter of the LowLevelKeyboardProc to LRESULT solves the first problem - see origonal C++ code above.

Code: Select all

function LowLevelKeyboardProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;



I found a example in Visual Basic on the MSDN for the undefined constants translated to Delphi they are as follows:

Code: Select all

// Low-Level Keyboard Constants
   Const HC_ACTION : Integer = 0;
   Const LLKHF_EXTENDED : Integer = $1;
   Const LLKHF_INJECTED : Integer = $10;
   Const LLKHF_ALTDOWN : Integer = $20;
   Const LLKHF_UP : Integer = $80;
   Const WH_KEYBOARD_LL : Integer = 13;

 


Changing the calling parameter of SetWindowsHookEx to address of (@LowLevelKeyboardProc) does not seem to make a difference (although I have changed it as you suggested)

So after making the above changes the DLL compiles Ok but the disabling of task switching does not work.

Something funny happens. The LowLevelKeyboardProc gets called on the first press of the CTRL key (before next key for example Esc is pressed) and not after that again (found from using debugging code). If CTRL key is pressed and ALT key a few times after that application loses focus but CTRL-ESC is suppressed after that until by mouse click application receives focus again then Start Menu pops up all of a sudden and CTRL-ESC is not suppressed after that again. Looks like Que processing gets confused somehow.
Johan Theunissen
Active Member
Active Member
 
Posts: 9
Joined: November 6th, 2008, 7:26 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Kambiz » December 24th, 2009, 3:01 pm

I only translated your C code to Delphi. I have no idea why the hook procedure doesn't work as expected.

I think I have done something similar some years ago. I will look at my old codes to find the solution.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Stefan » December 24th, 2009, 4:52 pm

what about google ?
User avatar
Stefan
Moderator
Moderator
 
Posts: 128
Joined: September 27th, 2004, 9:40 am
Location: Tilburg, The Netherlands

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Kambiz » December 25th, 2009, 3:20 am

Thanks Stefan! :)
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Kambiz » December 25th, 2009, 11:42 pm

I figure out the problem.

The hook procedure is corrcet, the problem is in DisableWindowsUI function. instead of WH_KEYBOARD_LL, I was used WH_KEYBOARD as the first parameter of SetWindowHookEx function, sorry.

I checked the code and it works (see attachment).
Attachments
NoWinUI.zip
Disabling Ctrl+Esc, Alt+Esc and Alt+Tab
(6.56 KiB) Downloaded 961 times
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Andrea Belli » March 5th, 2010, 12:51 am

..New to this forum, hello everybody.
I was searching a way to disable those keys, found your thread on google, downloaded the NoWinUI project and give it a try...
I made just a couple of minor changes to use it inside a delphi project and not a DLL.
Removed the export clauses, added the 3 functions to the interface section, added the 'stdcall' clause to the installed hook.

I have attached the modified unit; just include it in a project and call EnableWindowsUI / DisableWindowsUI to do the magic.

Thanks all.
Attachments
DisableWinKeys.rar
Disabling system keys
(756 Bytes) Downloaded 1441 times
Andrea Belli
Member
Member
 
Posts: 1
Joined: March 5th, 2010, 12:40 am

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp,W7

Postby Kambiz » March 5th, 2010, 2:13 am

Hi Andrea,

Welcome to the forum, and thanks for the update.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp

Postby Johan Theunissen » April 16th, 2010, 3:18 pm

Kambiz, Thank's a lot for your help. Have been busy on a heavy project and only got the change now to say thank you. Good to have clever internet experts when you have a question or problem.

Best wishes
Johan Theunissen
Johan Theunissen
Active Member
Active Member
 
Posts: 9
Joined: November 6th, 2008, 7:26 pm

Re: DISABLING TASK SWITCH CTRL-ESC ALT-TAB ALT-ESC etc in Xp

Postby Jay Sybert » June 2nd, 2011, 5:23 am

Thank You Delphi Area Forum and especially Andrea for the DisableWinKeys.pas!
Jay Sybert
Member
Member
 
Posts: 1
Joined: June 2nd, 2011, 5:14 am


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron