2010-07-29 9 views

Antwort

3
/// <summary> 
    /// Security routines related to the Windows Key on a standard personal computer Keyboard 
    /// </summary> 
    public static class WindowsKey { 
     /// <summary> 
     /// Disables the Windows Key 
     /// </summary> 
     /// <remarks>May require the current user to logoff or restart the system</remarks> 
     public static void Disable() { 
      RegistryKey key = null; 
      try { 
       key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Keyboard Layout", true); 
       byte[] binary = new byte[] { 
        0x00, 
        0x00, 
        0x00, 
        0x00, 
        0x00, 
        0x00, 
        0x00, 
        0x00, 
        0x03, 
        0x00, 
        0x00, 
        0x00, 
        0x00, 
        0x00, 
        0x5B, 
        0xE0, 
        0x00, 
        0x00, 
        0x5C, 
        0xE0, 
        0x00, 
        0x00, 
        0x00, 
        0x00 
       }; 
       key.SetValue("Scancode Map", binary, RegistryValueKind.Binary); 
      } 
      catch (System.Exception ex) { 
       Debug.Assert(false, ex.ToString()); 
      } 
      finally { 
       key.Close(); 
      } 
     } 

     /// <summary> 
     /// Enables the Windows Key 
     /// </summary> 
     /// <remarks>May require the current user to logoff or restart the system</remarks> 
     public static void Enable() { 
      RegistryKey key = null; 
      try { 
       key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Keyboard Layout", true); 
       key.DeleteValue("Scancode Map", true); 
      } 
      catch (System.Exception ex) { 
       Debug.Assert(false, ex.ToString()); 
      } 
      finally { 
       key.Close(); 
      } 
     } 
    } 
+0

sehr nette Antwort –

4

Sie benötigen einen Tastaturhaken. Startet irgendwo wie folgt aus:

hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstance, 0); 

und weiterhin wie folgt aus:

LRESULT KeyboardProc(...) 
    { 
    if (Key == VK_SOMEKEY) 
    return 1;    // Trap key 


    return CallNextHookEx(...); // Let the OS handle it 

    } 

Und für weitere Einzelheiten: http://www.codeproject.com/KB/winsdk/AntonioWinLock.aspx

1

Unter der Annahme, dass Sie die Windows-Taste permenantly deaktivieren möchten und nicht nur, wenn Sie Ihren Code ist im Fokus, dann können Sie dies tun, indem Sie die Registrierung wie folgt bearbeiten:

Deaktivieren In einen neuen REG_BINARY Wert namens "Scancode Map" auf "HKEY_LOCAL_ MACHINE \ System \ CurrentControlSet \ Control \ Keyboard Layout" mit einem Datenwert von "00000000000000000300000000005BE000005CE000000000"

zu aktivieren: : Löschen Sie den Wert "Scancode Map" vollständig aus der Registrierung.

+0

+1, danke dafür. Ich wäre nicht glücklich mit dem Typen, der meine Tastatur zerstört hat. – Tobiasopdenbrouw

3

Die Verwendung der Windows-Hooks ist viel sauberer als das Ändern der Registrierung. Außerdem haben Leute manchmal eigene Scan- Code-Karten eingerichtet, und das Überschreiben ist nicht sehr freundlich.

Um die Windows-Taste Hook-Funktionen verwenden, müssen Sie ein paar winapi Funktionen DllImport:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern IntPtr GetModuleHandle(string lpModuleName); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern IntPtr SetWindowsHookEx(int idHook, HookHandlerDelegate lpfn, IntPtr hMod, uint dwThreadId); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool UnhookWindowsHookEx(IntPtr hhk); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam); 

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] 
public static extern short GetKeyState(int keyCode); 

Eine ziemlich vollständige Erklärung und Durchlauf auf CodeProject finden. Hier ist eine direct link zu einer eigenständigen Klassendatei von diesem Beispiel, das alles tut (Um es sauber zu kompilieren, wenn Sie WPF verwenden, müssen Sie manuell auf System.Windows.Forms dll verweisen oder nur die 'System.Windows.Forms ändern Der Verweis von Keys auf System.Windows.Input.Key sollte funktionieren.

Denken Sie daran, UnhookWindowsHookEx() aufzurufen (die Klasse macht das in Dispose()), um Ihre Captures auszuhängen, oder Leute werden Sie hassen.

Verwandte Themen