2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
29 #include "../../idlib/precompiled.h"
33 // This file implements the low-level keyboard hook that traps the task keys.
36 //#define _WIN32_WINNT 0x0500 // for KBDLLHOOKSTRUCT
37 #include <afxwin.h> // MFC core and standard components
38 #include "win_local.h"
40 #define DLLEXPORT __declspec(dllexport)
42 // Magic registry key/value for "Remove Task Manager" policy.
43 LPCTSTR KEY_DisableTaskMgr = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
44 LPCTSTR VAL_DisableTaskMgr = "DisableTaskMgr";
46 // The section is SHARED among all instances of this DLL.
47 // A low-level keyboard hook is always a system-wide hook.
48 #pragma data_seg (".mydata")
49 HHOOK g_hHookKbdLL = NULL; // hook handle
50 BOOL g_bBeep = FALSE; // beep on illegal key
52 #pragma comment(linker, "/SECTION:.mydata,RWS") // tell linker: make it shared
58 Low-level keyboard hook:
59 Trap task-switching keys by returning without passing along.
62 LRESULT CALLBACK MyTaskKeyHookLL( int nCode, WPARAM wp, LPARAM lp ) {
63 KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
65 if ( nCode == HC_ACTION ) {
66 BOOL bCtrlKeyDown = GetAsyncKeyState( VK_CONTROL)>>((sizeof(SHORT) * 8) - 1 );
68 if ( ( pkh->vkCode == VK_ESCAPE && bCtrlKeyDown ) // Ctrl+Esc
69 || ( pkh->vkCode == VK_TAB && pkh->flags & LLKHF_ALTDOWN ) // Alt+TAB
70 || ( pkh->vkCode == VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN ) // Alt+Esc
71 || ( pkh->vkCode == VK_LWIN || pkh->vkCode == VK_RWIN ) // Start Menu
74 if ( g_bBeep && ( wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN ) ) {
75 MessageBeep( 0 ); // beep on downstroke if requested
77 return 1; // return without processing the key strokes
80 return CallNextHookEx( g_hHookKbdLL, nCode, wp, lp );
87 Are task keys disabled--ie, is hook installed?
88 Note: This assumes there's no other hook that does the same thing!
91 BOOL AreTaskKeysDisabled() {
92 return g_hHookKbdLL != NULL;
100 BOOL IsTaskMgrDisabled() {
103 if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
104 return FALSE; // no key ==> not disabled
109 return RegQueryValueEx( hk, VAL_DisableTaskMgr, NULL, NULL, (BYTE*)&val, &len ) == ERROR_SUCCESS && val == 1;
117 void DisableTaskKeys( BOOL bDisable, BOOL bBeep, BOOL bTaskMgr ) {
119 // task keys (Ctrl+Esc, Alt-Tab, etc.)
121 if ( !g_hHookKbdLL ) {
122 g_hHookKbdLL = SetWindowsHookEx( WH_KEYBOARD_LL, MyTaskKeyHookLL, win32.hInstance, 0 );
124 } else if ( g_hHookKbdLL != NULL ) {
125 UnhookWindowsHookEx( g_hHookKbdLL );
130 // task manager (Ctrl+Alt+Del)
133 if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
134 RegCreateKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk );
137 // disable TM: set policy = 1
139 RegSetValueEx( hk, VAL_DisableTaskMgr, NULL, REG_DWORD, (BYTE*)&val, sizeof(val) );
141 // enable TM: remove policy
142 RegDeleteValue( hk,VAL_DisableTaskMgr );