doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
win_taskkeyhook.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
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.
13 
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.
18 
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/>.
21 
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.
23 
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.
25 
26 ===========================================================================
27 */
28 
29 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 //
33 // This file implements the low-level keyboard hook that traps the task keys.
34 //
35 
36 //#define _WIN32_WINNT 0x0500 // for KBDLLHOOKSTRUCT
37 #include <afxwin.h> // MFC core and standard components
38 #include "win_local.h"
39 
40 #define DLLEXPORT __declspec(dllexport)
41 
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";
45 
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
51 #pragma data_seg ()
52 #pragma comment(linker, "/SECTION:.mydata,RWS") // tell linker: make it shared
53 
54 /*
55 ================
56 MyTaskKeyHookLL
57 
58  Low-level keyboard hook:
59  Trap task-switching keys by returning without passing along.
60 ================
61 */
62 LRESULT CALLBACK MyTaskKeyHookLL( int nCode, WPARAM wp, LPARAM lp ) {
63  KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
64 
65  if ( nCode == HC_ACTION ) {
66  BOOL bCtrlKeyDown = GetAsyncKeyState( VK_CONTROL)>>((sizeof(SHORT) * 8) - 1 );
67 
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
72  ) {
73 
74  if ( g_bBeep && ( wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN ) ) {
75  MessageBeep( 0 ); // beep on downstroke if requested
76  }
77  return 1; // return without processing the key strokes
78  }
79  }
80  return CallNextHookEx( g_hHookKbdLL, nCode, wp, lp );
81 }
82 
83 /*
84 ================
85 AreTaskKeysDisabled
86 
87  Are task keys disabled--ie, is hook installed?
88  Note: This assumes there's no other hook that does the same thing!
89 ================
90 */
92  return g_hHookKbdLL != NULL;
93 }
94 
95 /*
96 ================
97 IsTaskMgrDisabled
98 ================
99 */
101  HKEY hk;
102 
103  if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
104  return FALSE; // no key ==> not disabled
105  }
106 
107  DWORD val = 0;
108  DWORD len = 4;
109  return RegQueryValueEx( hk, VAL_DisableTaskMgr, NULL, NULL, (BYTE*)&val, &len ) == ERROR_SUCCESS && val == 1;
110 }
111 
112 /*
113 ================
114 DisableTaskKeys
115 ================
116 */
117 void DisableTaskKeys( BOOL bDisable, BOOL bBeep, BOOL bTaskMgr ) {
118 
119  // task keys (Ctrl+Esc, Alt-Tab, etc.)
120  if ( bDisable ) {
121  if ( !g_hHookKbdLL ) {
122  g_hHookKbdLL = SetWindowsHookEx( WH_KEYBOARD_LL, MyTaskKeyHookLL, win32.hInstance, 0 );
123  }
124  } else if ( g_hHookKbdLL != NULL ) {
125  UnhookWindowsHookEx( g_hHookKbdLL );
126  g_hHookKbdLL = NULL;
127  }
128  g_bBeep = bBeep;
129 
130  // task manager (Ctrl+Alt+Del)
131  if ( bTaskMgr ) {
132  HKEY hk;
133  if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
134  RegCreateKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk );
135  }
136  if ( bDisable ) {
137  // disable TM: set policy = 1
138  DWORD val = 1;
139  RegSetValueEx( hk, VAL_DisableTaskMgr, NULL, REG_DWORD, (BYTE*)&val, sizeof(val) );
140  } else {
141  // enable TM: remove policy
142  RegDeleteValue( hk,VAL_DisableTaskMgr );
143  }
144  }
145 }
LPCTSTR KEY_DisableTaskMgr
BOOL g_bBeep
DWORD
Definition: win_qgl.cpp:61
LPCTSTR VAL_DisableTaskMgr
GLenum GLsizei len
Definition: glext.h:3472
#define BOOL
Definition: mprintf.c:71
BOOL AreTaskKeysDisabled()
LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
#define NULL
Definition: Lib.h:88
#define FALSE
Definition: mprintf.c:70
HINSTANCE hInstance
Definition: win_local.h:102
BOOL IsTaskMgrDisabled()
Win32Vars_t win32
Definition: win_main.cpp:65
HHOOK g_hHookKbdLL
void DisableTaskKeys(BOOL bDisable, BOOL bBeep, BOOL bTaskMgr)