HOWTO: Disable Task Switching on Win32 Platforms

ID: Q226359


The information in this article applies to:


SUMMARY

This article describes how to disable task switching and other system functions accessed through key combinations such as CTRL+ESC and CTRL+ATL+DEL on Win32 Platforms.

Windows 95 and Windows 98

Applications can enable and disable ALT+TAB and CTRL+ESC; for example, by calling SystemParametersInfo (SPI_SETSCREENSAVERRUNNING). To disable ALT_TAB and CTRL+ESC set the uiParam parameter to TRUE or FALSE to enable the keys.


UINT nPreviousState;

// Disables task switching
SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, TRUE, &nPreviousState, 0);

// Enables task switching
SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, FALSE, &nPreviousState, 0); 

Applications that use SystemParametersInfo (SPI_SETSCREENSAVERRUNNING) to disable task switching must remember to enable task switching before exiting. Otherwise, task switching remains disabled after the process terminates.

Windows NT 4.0 Service Pack 3 and later Windows 2000

Applications can disable ALT+TAB or CTRL+ESC by installing a low-level keyboard hook. A low-level keyboard hook 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:

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 ATL+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;

            // Disable the WINDOWS key
            if (pkbhs->vkCode == VK_LWIN || pkbhs->vkCode == VK_RWIN)
                return 1;

            break;
        }

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

NOTE: Although a low-level keyboard hook gets notified when CTRL+ALT+DEL is pressed, it cannot prevent the CRTL+ALT+DEL from being handled by the system.

Another option available is to install a keyboard filter driver, that can prevent keystrokes from being sent to the system, including CTRL+ALT+DEL. Consult the Windows NT DDK documentation for more information on keyboard filter drivers.


Windows NT 4.0 Service Pack 2 and earlier, Windows NT 3.51 and earlier

Applications can disable CTRL+ESC system-wide by replacing the Windows NT Task Manager, however this is not recommended.

Applications can disable ALT+TAB and ALT+ESC when the application is running by registering hotkeys for the ALT+TAB and ALT+ESC combinations by calling RegisterHotKey.


REFERENCES

Q89373 Replacing the Windows NT Task Manager

Additional query words: CTRL+ESC CTL+ESC ALT+TAB ALT+ESC CTRL+ALT+DEL CTL+ALT+DEL SetWindowsHookEx WH_KEYBOARD_LL SystemParametersInfo SPI_SCREENSAVERRUNNING SPI_SETSCREENSAVERRUNNING


Keywords          : kbHook kbInput kbNTOS400 kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS95 kbWinOS98 
Version           : winnt:3.51,4.0
Platform          : winnt 
Issue type        : kbhowto 

Last Reviewed: May 3, 1999