使用 Win32 应用程序在 Windows 7 中禁用 CONTROL + ALT + DELETE 和 Windows(win) Key
我正在编写一个像 kiosk
一样运行的应用程序,应该允许用户离开应用程序.在 Windows 7
中,当他按下 Win 键或 CTRL + ALT+ DELETE ,它从程序中出来.我需要在 Windows 7<中禁用 Ctrl+ ALT + DELETE 组合键和 Win 键/code> 以编程方式.
I am writing an application which runs like a kiosk
and should allow user to go out of the application. In Windows 7
, when he presses the Win key or CTRL + ALT+ DELETE , it comes out of program. I need to disable the Ctrl+ ALT + DELETE combination key and Win key in Windows 7
programmatically.
推荐答案
按 Alt+Tab 也将退出应用程序.Ctrl+Esc 是打开开始"菜单的备用快捷方式.Alt+Esc 在运行的应用程序之间切换.有许多不同的键序列可以做到这一点;Windows 是多任务操作系统的自然结果.
Pressing Alt+Tab will switch out of the application, also. Ctrl+Esc is an alternate shortcut for opening the Start menu. Alt+Esc flips between running applications. There a number of different key sequences that can do this; a natural consequence of Windows being a multitasking operating system.
要解决此问题,您必须安装一个可以捕获这些任务键的低级键盘挂钩.该钩子将监视用于切换任务的任何常用键序列.每当它检测到这些序列之一时,它就会通过不将其传递到钩子链来忽略输入.Paul DiLascia 在 2002 年 9 月写了一篇文章来解决这个问题MSDN 杂志的版本.你关心的部分从页面的一半开始,但为了方便和获得语法高亮的乐趣,我在这里重印:
To work around this, you're going to have to install a low-level keyboard hook that can trap these task keys. The hook will watch for any of the common key sequences used to switch tasks. Whenever it detects one of those sequences, it will ignore the input by not passing it down the hook chain. Paul DiLascia wrote an article that addresses this very question in the September 2002 edition of MSDN Magazine. The part you care about starts about halfway down the page, but I've reprinted it here for convenience and to get the pleasures of syntax highlighting:
TaskKeyHook.h:
TaskKeyHook.h:
////////////////////////////////////////////////////////////////
// MSDN Magazine ― September 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio 6.0 and Visual Studio .NET on Windows XP.
//
#define DLLIMPORT __declspec(dllimport)
DLLIMPORT BOOL DisableTaskKeys(BOOL bEnable, BOOL bBeep);
DLLIMPORT BOOL AreTaskKeysDisabled();
TaskKeyHook.cpp
TaskKeyHook.cpp
////////////////////////////////////////////////////////////////
// MSDN Magazine ― September 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio 6.0 and Visual Studio .NET on Windows XP.
//
// This file implements the low-level keyboard hook that traps the task
// keys.
//
#define _WIN32_WINNT 0x0500 // for KBDLLHOOKSTRUCT
#include <afxwin.h> // MFC core and standard components
#define DLLEXPORT __declspec(dllexport)
//////////////////
// App (DLL) object
//
class CTaskKeyHookDll : public CWinApp {
public:
CTaskKeyHookDll() { }
~CTaskKeyHookDll() { }
} MyDll;
////////////////
// The section is SHARED among all instances of this DLL.
// A low-level keyboard hook is always a system-wide hook.
//
#pragma data_seg (".mydata")
HHOOK g_hHookKbdLL = NULL; // hook handle
BOOL g_bBeep = FALSE; // beep on illegal key
#pragma data_seg ()
#pragma comment(linker, "/SECTION:.mydata,RWS") // tell linker: make it
// shared
/////////////////
// Low-level keyboard hook:
// Trap task-switching keys by returning without passing along.
//
LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
{
KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
if (nCode==HC_ACTION) {
BOOL bCtrlKeyDown =
GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
if ((pkh->vkCode==VK_ESCAPE && bCtrlKeyDown) || // Ctrl+Esc
// Alt+TAB
(pkh->vkCode==VK_TAB && pkh->flags & LLKHF_ALTDOWN) ||
// Alt+Esc
(pkh->vkCode==VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN)||
(pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN)) { // Start Menu
if (g_bBeep && (wp==WM_SYSKEYDOWN||wp==WM_KEYDOWN))
MessageBeep(0); // only beep on downstroke if requested
return 1; // gobble it: go directly to jail, do not pass go
}
}
return CallNextHookEx(g_hHookKbdLL, nCode, wp, lp);
}
//////////////////
// Are task keys disabled―ie, is hook installed?
// Note: This assumes there's no other hook that does the same thing!
//
DLLEXPORT BOOL AreTaskKeysDisabled()
{
return g_hHookKbdLL != NULL;
}
//////////////////
// Disable task keys: install low-level kbd hook.
// Return whether currently disabled or not.
//
DLLEXPORT BOOL DisableTaskKeys(BOOL bDisable, BOOL bBeep)
{
if (bDisable) {
if (!g_hHookKbdLL) {
g_hHookKbdLL = SetWindowsHookEx(WH_KEYBOARD_LL,
MyTaskKeyHookLL, MyDll.m_hInstance, 0);
}
} else if (g_hHookKbdLL != NULL) {
UnhookWindowsHookEx(g_hHookKbdLL);
g_hHookKbdLL = NULL;
}
g_bBeep = bBeep;
return AreTaskKeysDisabled();
}
他还提供了禁用任务栏的示例代码(从而防止 Windows 键显示开始"菜单)以及使用这些库的完整示例应用程序.
He also provides sample code to disable the taskbar (thus preventing the Windows key from showing the Start menu) and a complete sample application that uses these libraries.
就防止Ctrl+Alt+Del(安全注意序列,或SAS)而言,上述方法不会工作.原因是操作系统将 SAS 产生的硬件中断与其他键分开捕获,专门用于防止程序挂钩序列和欺骗登录提示.您将无法使用键盘挂钩禁用此功能.我上面链接的文章确实在顶部详细介绍了这个要求,但这些策略只是经过测试,很可能只适用于 Windows XP.文章建议的另一种方法是禁用任务管理器,但请注意,这不会阻止用户关闭系统等.正确 方法是编写键盘驱动程序.
As far as preventing Ctrl+Alt+Del (the secure attention sequence, or SAS), the above approach is not going to work. The reason is that the OS traps the hardware interrupt generated by the SAS separately from other keys, specifically to prevent programs from hooking the sequence and spoofing a login prompt. You aren't going to be able to disable this feature with a keyboard hook. The article I linked to above does cover this requirement in great detail at the top portion, but those strategies are only tested and more than likely will only work on Windows XP. Another approach suggested by the article is to disable the Task Manager, but note that that won't stop the user from shutting down the system, etc. The right way to do this is to write a keyboard driver.
相关文章