Low-Level Taste unterdrücken



  • Hallo,
    ich versuche die "Sleep"-Taste zu unterdrücken, da ich sie öfters unabsichtlich drücke. Mein Code erkennt sie auch, allerdings wird mein Computer trotzdem schlafen gelegt. Lässt sich die Funktion eventuell nicht unterdrücken?

    Mein Code:

    static bool status = true;
    
            [DllImport("user32.dll")]
            static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc callback, IntPtr hInstance, uint threadId);
    
            [DllImport("user32.dll")]
            static extern bool UnhookWindowsHookEx(IntPtr hInstance);
    
            [DllImport("user32.dll")]
            static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, int wParam, IntPtr lParam);
    
            [DllImport("kernel32.dll")]
            static extern IntPtr LoadLibrary(string lpFileName);
    
            private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    
            const int WH_KEYBOARD_LL = 13;
            const int WM_KEYDOWN = 0x100;
            const int WM_KEYUP = 0x0101;
    
            private LowLevelKeyboardProc _proc = hookProc;
    
            private static IntPtr hhook = IntPtr.Zero;
    
            public void SetHook()
            {
                IntPtr hInstance = LoadLibrary("User32");
                hhook = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, hInstance, 0);
            }
    
            public static void UnHook()
            {
                UnhookWindowsHookEx(hhook);
            }
    
            public static IntPtr hookProc(int code, IntPtr wParam, IntPtr lParam)
            {
                if (code >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_KEYUP))
                {
                    int vkCode = Marshal.ReadInt32(lParam);
    
                    if (vkCode.ToString() == "95" && status == true)
                    {
                        //Wenn Sleep gedrückt wurde
                    }
    
                    return (IntPtr) 1;
                }
                else
                {
                    return CallNextHookEx(hhook, code, (int)wParam, lParam);
                }
            }
    


  • Willst du nicht besser dafür die Windows-Funktionalität in den Systemeinstellungen (Disabling Sleep Keyboard button in Windows) benutzen?



  • Ja das macht Sinn, trotzdem würde ich gerne wissen wie man das mit C# macht.


Anmelden zum Antworten