2010-07-29 36 views

Trả lời

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

câu trả lời rất hay –

4

Bạn cần móc nối bàn phím. Bắt đầu ở đâu đó như thế này:

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

và tiếp tục như thế này:

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


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

    } 

Và cho biết thêm chi tiết: http://www.codeproject.com/KB/winsdk/AntonioWinLock.aspx

1

Giả sử bạn muốn vô hiệu hóa phím Windows permenantly và không chỉ khi mã của bạn đang tập trung sau đó bạn có thể làm như vậy bằng cách chỉnh sửa đăng ký như sau:

Để tắt : Thêm một giá trị REG_BINARY mới gọi là "Scancode Map" thành "HKEY_LOCAL_ MÁY \ System \ CurrentControlSet \ Control \ Keyboard Layout" với một giá trị dữ liệu "00000000000000000300000000005BE000005CE000000000"

Để kích hoạt : Xóa "Bản đồ Scancode" giá trị hoàn toàn từ sổ đăng ký.

+0

+1, cảm ơn vì điều đó. Tôi không hài lòng với anh chàng đã phá hỏng bàn phím của tôi. – Tobiasopdenbrouw

3

Sử dụng móc cửa sổ sạch hơn rất nhiều so với sửa đổi sổ đăng ký. Ngoài ra, đôi khi mọi người đã thiết lập bản đồ cá nhân hóa scancode của riêng mình, và ghi đè lên chúng không phải là một điều rất tốt để làm.

Để sử dụng các cửa sổ chức năng móc chìa khóa bạn cần phải DllImport một chức năng vài WINAPI:

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

Một lời giải thích khá đầy đủ và hương có thể được tìm thấy trên CodeProject. Dưới đây là một direct link cho một tập tin lớp học khép kín từ ví dụ đó làm tất cả mọi thứ (Để có được nó để biên dịch sạch nếu bạn đang sử dụng WPF sẽ yêu cầu bạn tham khảo thủ công System.Windows.Forms dll hoặc chỉ thay đổi 'System.Windows.Forms Tham chiếu .Keys 'để System.Windows.Input.Key nên làm việc).

Hãy nhớ gọi UnhookWindowsHookEx() (lớp này thực hiện việc này trong Dispose()) để hủy các ảnh chụp của bạn hoặc mọi người sẽ ghét bạn.

Các vấn đề liên quan