2010-07-29 4 views

답변

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

아주 좋은 대답 –

1

당신이 때 코드 단지 영구히 아니라 윈도우 키를 비활성화 할 것을 가정 :

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


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

    } 

그리고 더 세부 사항에 대한 초점이 맞으면 다음과 같이 레지스트리를 편집하면됩니다.

비활성화하려면 "HKEY_LOCAL_ MACHINE \ 시스템 \ CURRENTCONTROLSET 제어 \ 키보드 레이아웃 \"의 데이터 값이 "00000000000000000300000000005BE000005CE000000000"

하는를 사용하려면 할 "스캔 코드 맵"라는 새로운 REG_BINARY 값을 추가 : "Scancode Map"값을 레지스트리에서 완전히 삭제하십시오. 창문 후크를 사용

+0

+1, 고마워. 그래도 내 키보드를 망가 뜨린 녀석은 행복하지 않을거야. – Tobiasopdenbrouw

3

레지스트리를 수정하는 것보다 훨씬 청소기입니다. 또한 때로는 사람들이 자신의 개인화 된 스캔 코드 맵을 설정하고이를 덮어 쓰는 것이 매우 친절하지 않습니다.

가 창문 열쇠 고리 기능을 사용하려면 몇 WINAPI 기능을 같이 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); 

상당히 완전한 설명과 연습은 CodeProject에서 찾을 수 있습니다. 모든 것을하는 그 예제의 자체 포함 된 클래스 파일에 direct link이 있습니다 (WPF를 사용하는 경우 깨끗하게 컴파일하려면 System.Windows.Forms dll을 수동으로 참조하거나 'System.Windows.Forms'을 수동으로 변경해야합니다. .Keys 'System.Windows.Input.Key에 대한 참조가 작동해야합니다.)

UnhookWindowsHookEx() (이 클래스는 Dispose()에서 이것을 수행함)를 호출하여 캡쳐를 해제하거나 사람들이 나를 증오하게됩니다.

관련 문제