win_taskkeyhook.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. //
  23. // This file implements the low-level keyboard hook that traps the task keys.
  24. //
  25. //#define _WIN32_WINNT 0x0500 // for KBDLLHOOKSTRUCT
  26. //#include <afxwin.h> // MFC core and standard components
  27. #include "win_local.h"
  28. #define DLLEXPORT __declspec(dllexport)
  29. // Magic registry key/value for "Remove Task Manager" policy.
  30. LPCTSTR KEY_DisableTaskMgr = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
  31. LPCTSTR VAL_DisableTaskMgr = "DisableTaskMgr";
  32. // The section is SHARED among all instances of this DLL.
  33. // A low-level keyboard hook is always a system-wide hook.
  34. #pragma data_seg (".mydata")
  35. HHOOK g_hHookKbdLL = NULL; // hook handle
  36. BOOL g_bBeep = FALSE; // beep on illegal key
  37. #pragma data_seg ()
  38. #pragma comment(linker, "/SECTION:.mydata,RWS") // tell linker: make it shared
  39. /*
  40. ================
  41. MyTaskKeyHookLL
  42. Low-level keyboard hook:
  43. Trap task-switching keys by returning without passing along.
  44. ================
  45. */
  46. LRESULT CALLBACK MyTaskKeyHookLL( int nCode, WPARAM wp, LPARAM lp ) {
  47. KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
  48. if ( nCode == HC_ACTION ) {
  49. BOOL bCtrlKeyDown = GetAsyncKeyState( VK_CONTROL)>>((sizeof(SHORT) * 8) - 1 );
  50. if ( ( pkh->vkCode == VK_ESCAPE && bCtrlKeyDown ) // Ctrl+Esc
  51. || ( pkh->vkCode == VK_TAB && pkh->flags & LLKHF_ALTDOWN ) // Alt+TAB
  52. || ( pkh->vkCode == VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN ) // Alt+Esc
  53. || ( pkh->vkCode == VK_LWIN || pkh->vkCode == VK_RWIN ) // Start Menu
  54. ) {
  55. if ( g_bBeep && ( wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN ) ) {
  56. MessageBeep( 0 ); // beep on downstroke if requested
  57. }
  58. return 1; // return without processing the key strokes
  59. }
  60. }
  61. return CallNextHookEx( g_hHookKbdLL, nCode, wp, lp );
  62. }
  63. /*
  64. ================
  65. AreTaskKeysDisabled
  66. Are task keys disabled--ie, is hook installed?
  67. Note: This assumes there's no other hook that does the same thing!
  68. ================
  69. */
  70. BOOL AreTaskKeysDisabled() {
  71. return g_hHookKbdLL != NULL;
  72. }
  73. /*
  74. ================
  75. IsTaskMgrDisabled
  76. ================
  77. */
  78. BOOL IsTaskMgrDisabled() {
  79. HKEY hk;
  80. if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
  81. return FALSE; // no key ==> not disabled
  82. }
  83. DWORD val = 0;
  84. DWORD len = 4;
  85. return RegQueryValueEx( hk, VAL_DisableTaskMgr, NULL, NULL, (BYTE*)&val, &len ) == ERROR_SUCCESS && val == 1;
  86. }
  87. /*
  88. ================
  89. DisableTaskKeys
  90. ================
  91. */
  92. void DisableTaskKeys( BOOL bDisable, BOOL bBeep, BOOL bTaskMgr ) {
  93. // task keys (Ctrl+Esc, Alt-Tab, etc.)
  94. if ( bDisable ) {
  95. if ( !g_hHookKbdLL ) {
  96. g_hHookKbdLL = SetWindowsHookEx( WH_KEYBOARD_LL, MyTaskKeyHookLL, win32.hInstance, 0 );
  97. }
  98. } else if ( g_hHookKbdLL != NULL ) {
  99. UnhookWindowsHookEx( g_hHookKbdLL );
  100. g_hHookKbdLL = NULL;
  101. }
  102. g_bBeep = bBeep;
  103. // task manager (Ctrl+Alt+Del)
  104. if ( bTaskMgr ) {
  105. HKEY hk;
  106. if ( RegOpenKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk ) != ERROR_SUCCESS ) {
  107. RegCreateKey( HKEY_CURRENT_USER, KEY_DisableTaskMgr, &hk );
  108. }
  109. if ( bDisable ) {
  110. // disable TM: set policy = 1
  111. DWORD val = 1;
  112. RegSetValueEx( hk, VAL_DisableTaskMgr, NULL, REG_DWORD, (BYTE*)&val, sizeof(val) );
  113. } else {
  114. // enable TM: remove policy
  115. RegDeleteValue( hk,VAL_DisableTaskMgr );
  116. }
  117. }
  118. }