2013-08-17 36 views

Trả lời

17

Vâng, nếu bạn có vấn đề với hệ thống móc, đây là làm sẵn giải pháp (dựa trên http://www.dreamincode.net/forums/topic/180436-global-hotkeys/):

Xác định lớp tĩnh trong dự án của bạn:

public static class Constants 
{ 
    //windows message id for hotkey 
    public const int WM_HOTKEY_MSG_ID = 0x0312; 
} 

Xác định lớp học trong dự án của bạn:

public class KeyHandler 
{ 
    [DllImport("user32.dll")] 
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); 

    [DllImport("user32.dll")] 
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id); 

    private int key; 
    private IntPtr hWnd; 
    private int id; 

    public KeyHandler(Keys key, Form form) 
    { 
     this.key = (int)key; 
     this.hWnd = form.Handle; 
     id = this.GetHashCode(); 
    } 

    public override int GetHashCode() 
    { 
     return key^hWnd.ToInt32(); 
    } 

    public bool Register() 
    { 
     return RegisterHotKey(hWnd, id, 0, key); 
    } 

    public bool Unregiser() 
    { 
     return UnregisterHotKey(hWnd, id); 
    } 
} 

thêm usings:

using System.Windows.Forms; 
using System.Runtime.InteropServices; 

bây giờ, trong Form của bạn, thêm lĩnh vực:

private KeyHandler ghk; 

và trong constructor Mẫu:

ghk = new KeyHandler(Keys.PrintScreen, this); 
ghk.Register(); 

Thêm những 2 phương pháp để hình của bạn:

private void HandleHotkey() 
{ 
     // Do stuff... 
} 

protected override void WndProc(ref Message m) 
{ 
    if (m.Msg == Constants.WM_HOTKEY_MSG_ID) 
     HandleHotkey(); 
    base.WndProc(ref m); 
} 

HandleHotkey là nút của bạn báo chí xử lý. Bạn có thể thay đổi nút bằng cách chuyển thông số khác nhau tại đây: ghk = new KeyHandler(Keys.PrintScreen, this);

Bây giờ, chương trình của bạn phản ứng cho đầu vào buton ngay cả khi không tập trung.

+0

Cách chỉnh sửa mã này, nếu tôi muốn 2 phím nóng khác nhau có hai chức năng khác nhau? Tôi có thể kiểm tra bằng cách nào đó trong "WndProc" hoặc "HandleHotkey" mà phím đã được nhấn? – user1696947

+0

Điều này có thể được điều chỉnh để xử lý các tổ hợp phím không? Chẳng hạn như CTRL-N – MrVimes

0

API GetAsyncKeyState() có thể là giải pháp thay thế hoàn toàn có thể chấp nhận để thiết lập Windows Hook.

Điều này phụ thuộc vào cách bạn muốn nhận dữ liệu nhập. Nếu bạn thích các thông báo hướng sự kiện, thì móc là cách để đi; tuy nhiên, nếu bạn thích bỏ phiếu bàn phím để thay đổi trạng thái, bạn có thể sử dụng API ở trên.

Dưới đây là một minh chứng đơn giản về cách sử dụng GetAsyncKeyState:
Xuất phát từ Pinvoke.NET

[DllImport("User32.dll")] 
private static extern short GetAsyncKeyState(int vKey); 

private static readonly int VK_SNAPSHOT = 0x2C; //This is the print-screen key. 

//Assume the timer is setup with Interval = 16 (corresponds to ~60FPS). 
private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer(); 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    short keyState = GetAsyncKeyState(VK_SNAPSHOT); 

    //Check if the MSB is set. If so, then the key is pressed. 
    bool prntScrnIsPressed = ((keyState >> 15) & 0x0001) == 0x0001; 

    //Check if the LSB is set. If so, then the key was pressed since 
    //the last call to GetAsyncKeyState 
    bool unprocessedPress = ((keyState >> 0) & 0x0001) == 0x0001; 

    if (prntScrnIspressed) 
    { 
     //TODO Execute client code... 
    } 

    if (unprocessedPress) 
    { 
     //TODO Execute client code... 
    } 
} 
Các vấn đề liên quan