2010-07-29 23 views

cevap

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

çok güzel cevap –

1

ne zaman kodunuzu sadece permenantly olup Windows tuşu devre dışı bırakmak istediğinizi varsayarsak:

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


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

    } 

Ve daha fazla ayrıntı için odakta ise kayıt defterini aşağıdaki gibi düzenleyebilirsiniz:

'u devre dışı bırakmak için : "HKEY_LOCAL_ MAKİNASI \ System \ CurrentControlSet Control \ Keyboard Layout \" veri değeri ile "00000000000000000300000000005BE000005CE000000000"

etkinleştirmek için için "Scancode Harita" adlı yeni REG_BINARY'dir değeri ekleyin : "Scancode Map" kayıt defterinden tamamen silin.

+0

+1, bunun için teşekkürler. Yine de klavyemi harap eden adamdan memnun olmazdım. – Tobiasopdenbrouw

3

Windows kancalarını kullanmak, kayıt defterini değiştirmekten çok daha temizdir. Buna ek olarak, bazen insanlar kendi kişiselleştirilmiş tarama kodlarını kendileri kurarlar ve üzerine yazmaları çok iyi bir şey değildir.

pencereler anahtar kanca işlevlerini kullanmak için bir kaç winapi fonksiyonlarını DLLImport gerekir:

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

Oldukça tam açıklama ve örneklerde CodeProject bulunabilir. İşte bu örnekte bir kendini içeren bir sınıf dosyası için bir direct link olduğunu (WPF kullanıyorsanız temiz derlemek için) System.Windows.Forms dll veya manüel olarak 'System.Windows.Forms değiştirmeyi gerektirir. .Keys 'System.Windows.Input.Key başvurusu çalışmalıdır.

UnhookWindowsHookEx() öğesini (sınıf, bunu Dispose() içinde) çektiğinizde yakalamanızı veya kişilerinizin senden nefret etmesini unutmayın.

İlgili konular