DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

win_taskkeyhook.cpp (4772B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition 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 BFG Edition 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 #pragma hdrstop
     30 #include "../../idlib/precompiled.h"
     31 
     32 //
     33 // This file implements the low-level keyboard hook that traps the task keys.
     34 //
     35 #include "win_local.h"
     36 
     37 #define DLLEXPORT __declspec(dllexport)
     38 
     39 // Magic registry key/value for "Remove Task Manager" policy.
     40 LPCTSTR KEY_DisableTaskMgr = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
     41 LPCTSTR VAL_DisableTaskMgr = "DisableTaskMgr";
     42 
     43 // The section is SHARED among all instances of this DLL.
     44 // A low-level keyboard hook is always a system-wide hook.
     45 #pragma data_seg (".mydata")
     46 HHOOK g_hHookKbdLL = NULL;	// hook handle
     47 BOOL  g_bBeep = FALSE;		// beep on illegal key
     48 #pragma data_seg ()
     49 #pragma comment(linker, "/SECTION:.mydata,RWS") // tell linker: make it shared
     50 
     51 /*
     52 ================
     53 MyTaskKeyHookLL
     54 
     55   Low-level keyboard hook:
     56   Trap task-switching keys by returning without passing along.
     57 ================
     58 */
     59 LRESULT CALLBACK MyTaskKeyHookLL( int nCode, WPARAM wp, LPARAM lp ) {
     60 	KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
     61 
     62 	if ( nCode == HC_ACTION ) {
     63 		BOOL bCtrlKeyDown = GetAsyncKeyState( VK_CONTROL)>>((sizeof(SHORT) * 8) - 1 );
     64 
     65 		if (	( pkh->vkCode == VK_ESCAPE && bCtrlKeyDown )				// Ctrl+Esc
     66 			 || ( pkh->vkCode == VK_TAB && pkh->flags & LLKHF_ALTDOWN )		// Alt+TAB
     67 			 || ( pkh->vkCode == VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN )	// Alt+Esc
     68 			 || ( pkh->vkCode == VK_LWIN || pkh->vkCode == VK_RWIN )		// Start Menu
     69 			 ) {
     70 
     71 			if ( g_bBeep && ( wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN ) ) {
     72 				MessageBeep( 0 ); // beep on downstroke if requested
     73 			}
     74 			return 1; // return without processing the key strokes
     75 		}
     76 	}
     77 	return CallNextHookEx( g_hHookKbdLL, nCode, wp, lp );
     78 }
     79 
     80 /*
     81 ================
     82 AreTaskKeysDisabled
     83 
     84   Are task keys disabled--ie, is hook installed?
     85   Note: This assumes there's no other hook that does the same thing!
     86 ================
     87 */
     88 BOOL AreTaskKeysDisabled() {
     89 	return g_hHookKbdLL != NULL;
     90 }
     91 
     92 /*
     93 ================
     94 IsTaskMgrDisabled
     95 ================
     96 */
     97 BOOL IsTaskMgrDisabled() {
     98 	HKEY hk;
     99 
    100 	if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
    101 		return FALSE; // no key ==> not disabled
    102 	}
    103 
    104 	DWORD val = 0;
    105 	DWORD len = 4;
    106 	return RegQueryValueEx( hk, VAL_DisableTaskMgr, NULL, NULL, (BYTE*)&val, &len ) == ERROR_SUCCESS && val == 1;
    107 }
    108 
    109 /*
    110 ================
    111 DisableTaskKeys
    112 ================
    113 */
    114 void DisableTaskKeys( BOOL bDisable, BOOL bBeep, BOOL bTaskMgr ) {
    115 
    116 	// task keys (Ctrl+Esc, Alt-Tab, etc.)
    117 	if ( bDisable ) {
    118 		if ( !g_hHookKbdLL ) {
    119 			g_hHookKbdLL = SetWindowsHookEx( WH_KEYBOARD_LL, MyTaskKeyHookLL, win32.hInstance, 0 );
    120 		}
    121 	} else if ( g_hHookKbdLL != NULL ) {
    122 		UnhookWindowsHookEx( g_hHookKbdLL );
    123 		g_hHookKbdLL = NULL;
    124 	}
    125 	g_bBeep = bBeep;
    126 
    127 	// task manager (Ctrl+Alt+Del)
    128 	if ( bTaskMgr ) {
    129 		HKEY hk;
    130 		if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
    131 			RegCreateKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk );
    132 		}
    133 		if ( bDisable ) {
    134 			// disable TM: set policy = 1
    135 			DWORD val = 1;
    136 			RegSetValueEx( hk, VAL_DisableTaskMgr, NULL, REG_DWORD, (BYTE*)&val, sizeof(val) );
    137 		} else {
    138 			// enable TM: remove policy 
    139 			RegDeleteValue( hk,VAL_DisableTaskMgr );
    140 		}
    141 	}
    142 }