win_taskkeyhook.cpp 4.7 KB

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