2009-07-21 37 views
9

Ví dụ: với winamp (trên Windows ít nhất), bạn có thể chơi toàn màn hình trò chơi với winamp ở chế độ nền và sử dụng các nút media * để điều khiển âm thanh. Winamp không cần tập trung, cho phép trò chơi tiếp tục toàn màn hình.Chụp lần nhấn phím mà không lấy nét

Tôi muốn viết điều này bằng Java nhưng điều đó có thể sẽ không hoạt động (ghi lại các lần nhấn phím mà không lấy nét đã gặp khó khăn trong Java afaik), vì vậy mọi giải pháp C# đều tốt.

Vì vậy, câu hỏi cơ bản là: cách nắm bắt các lần nhấn phím mà không tập trung?

*) Tôi tin rằng các nút 'back/forward/stop/mail/search/favorites/web/home' được gọi là các nút media, nhưng một tên tốt hơn sẽ được chào đón :).

+0

tôi gọi cho họ phương tiện truyền thông các nút hoặc phím phương tiện truyền thông cũng vậy, không biết bất kỳ tên nào tốt hơn. – Luc

Trả lời

7

Móc cửa sổ ở mức độ thấp là một cách để thực hiện. Đây là một trong số article và đây là thông tin thêm một chút từ MSDN.

Đây là một cái nhìn một phần của những gì mã có thể trông giống như:

private IntPtr LowLevelKeyboardHook(int nCode, WindowsMessages wParam, [In] KBDLLHOOKSTRUCT lParam) 
    { 
     bool callNext = true; 

     bool isKeyDown = (wParam == WindowsMessages.KEYDOWN || wParam == WindowsMessages.SYSKEYDOWN); 
     bool isKeyUp = (wParam == WindowsMessages.KEYUP || wParam == WindowsMessages.SYSKEYUP); 

     if ((nCode >= 0) && (isKeyDown || isKeyUp)) 
     { 
      // the virtual key codes and the winforms Keys have the same enumeration 
      // so we can freely cast back and forth between them 
      Keys key = (Keys)lParam.vkCode; 

      // Do your other processing here... 
     } 

     // if any handler returned false, trap the message 
     return (callNext) ? User32.CallNextHookEx(_mainHook, nCode, wParam, lParam) : _nullNext; 
    } 


    /// <summary> 
    /// Registers the user's LowLevelKeyboardProc with the system in order to 
    /// intercept any keyboard events before processed in the regular fashion. 
    /// This can be used to log all keyboard events or ignore them. 
    /// </summary> 
    /// <param name="hook">Callback function to call whenever a keyboard event occurs.</param> 
    /// <returns>The IntPtr assigned by the Windows's sytem that defines the callback.</returns> 
    private IntPtr RegisterLowLevelHook(LowLevelKeyboardProc hook) 
    { 
     IntPtr handle = IntPtr.Zero; 

     using (Process currentProcess = Process.GetCurrentProcess()) 
     using (ProcessModule currentModule = currentProcess.MainModule) 
     { 
      IntPtr module = Kernel32.GetModuleHandle(currentModule.ModuleName); 
      handle = User32.SetWindowsHookEx(HookType.KEYBOARD_LL, hook, module, 0); 
     } 

     return handle; 
    } 

    /// <summary> 
    /// Unregisters a previously registered callback from the low-level chain. 
    /// </summary> 
    /// <param name="hook">IntPtr previously assigned to the low-level chain. 
    /// Users should have stored the value given by 
    /// <see cref="Drs.Interop.Win32.LowLevelKeyboard.RegisterLowLevelHook"/>, 
    /// and use that value as the parameter into this function.</param> 
    /// <returns>True if the hook was removed, false otherwise.</returns> 
    private bool UnregisterLowLevelHook(IntPtr hook) 
    { 
     return User32.UnhookWindowsHookEx(hook); 
    } 

Chỉ cần thực hiện tất cả các P/Gọi tờ khai cần thiết, và nó sẽ làm việc. Tôi sử dụng phương pháp này trong ứng dụng của mình và nó hoạt động tốt.

+0

Các loại này không được nhận dạng trên ứng dụng của tôi và nó không biên dịch (WindowsMessages, User32 vv ...) bạn đã làm gì để làm cho nó hoạt động? bạn có mã này trực tuyến ở bất cứ nơi nào bởi bất kỳ cơ hội nào không? –

+1

Không, đó là những tờ khai mà bạn cần khai báo. Tôi không có mã trực tuyến, nhưng bạn có thể tạo ra các cuống đơn giản bằng cách sử dụng P/Invoke signatures từ http://www.pinvoke.net. –

+0

biến _nullNext là gì? –

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