【发布时间】:2020-02-19 21:19:59
【问题描述】:
如何使用带有 wpf 的 C# 禁用 ctrl+Alt+del 和 windows 按钮??
我尝试使用一些事件形式的事件,例如按键但失败了。
【问题讨论】:
如何使用带有 wpf 的 C# 禁用 ctrl+Alt+del 和 windows 按钮??
我尝试使用一些事件形式的事件,例如按键但失败了。
【问题讨论】:
无法特别禁用 Ctrl-Alt-Del 的快捷方式, 这是因为 Ctrl-Alt-Del 组合是一个根深蒂固的系统调用。
但是可以单独过滤它们,因此您可以使用这些键阻止其他快捷键。 为此,您需要挂钩操作系统事件:
这与系统事件挂钩。
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
如果您将 id 设置为 13,它将连接到键盘输入。
在回调中你需要几件事:
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
public readonly Keys key;
private readonly int scanCode;
private readonly int flags;
private readonly int time;
private readonly IntPtr extra;
}
这个结构是读取 c# 中的实际键所必需的。
这可以通过给委托一个函数来使用:
private static IntPtr CaptureKey(int nCode, IntPtr wp, IntPtr lp)
{
if (nCode < 0) return (IntPtr) 1; //CallNextHookEx(_ptrHook, nCode, wp, lp);
KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
if(objKeyInfo.key == /*some key*/){
// do something
}
}
使用此功能时,您可以从objKeyInfo.key获取密钥
有关Ctrl-Alt-Del 组合的更多背景信息:
Is there any method to disable logoff,lock and taskmanager in ctrl+alt+del in C#
【讨论】:
Tamas Piros 就该主题写了一篇不错的文章 http://tamas.io/c-disable-ctrl-alt-del-alt-tab-alt-f4-start-menu-and-so-on/ 也应该在 WPF 中工作
【讨论】: