简体   繁体   中英

wpf Stop ctrl+Alt+del and windows button using C#

how to disable ctrl + Alt + del and windows button using C# with wpf ??

I tried to use some events form events like key down but fail.

It isn't possible to disable the shortcut for Ctrl - Alt - Del specifically, this is because the Ctrl - Alt - Del combo is a deeply baked system call.

However it is possible to filter them separately, so you can prevent the other shortcuts with these keys. To do this you need to hook into the OS events:


this hooks onto the system events.

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);

if you set the id to 13 it will hook onto the keyboard inputs.


in the callback you need several things:

[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;
}

this struct is required to read the actual keys in c#.

this can be used by giving the delegate a function like:

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
    }
}

when using this you can get the key from objKeyInfo.key

for more background info about the Ctrl - Alt - Del combo: 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 中工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM