2010-08-25 30 views
5

Làm cách nào để tạo ứng dụng thực hiện tác vụ bằng phím tắt (Ứng dụng phải ẩn). Ví dụ: Hiển thị MessageBox khi người dùng nhấn Ctrl + Alt + W.Cách tạo lối tắt bàn phím trong cửa sổ gọi chức năng trong ứng dụng của tôi?

+0

Bạn cần thêm thông tin về những gì bạn đang cố gắng hoàn thành. Công nghệ (winforms, wpf, vv). Ngoài ra câu hỏi của bạn dường như ngụ ý rằng nó phải là nhiệm vụ nền cố gắng để móc bàn phím, nhưng nó không phải là rõ ràng. –

+1

Có, tôi muốn nó hoạt động trên nền. Và Winforms –

Trả lời

9

Một giải pháp sẽ được sử dụng interop và sử dụng API Win32 RegisterHotKey. Dưới đây là một ví dụ nhanh chóng và bẩn tôi chỉ cần đặt lại với nhau vì vậy nó không được thử nghiệm tốt và tôi không chắc chắn rằng không có tác dụng phụ không được giải thích, nhưng nó sẽ làm việc.

Đầu tiên đây là một đơn giản HotKeyManager này sẽ chăm sóc của các interop cơ bản, cung cấp một cửa sổ ẩn để xử lý các thông điệp của Windows bản địa (WM_HOTKEY) được dịch sang một sự kiện .NET HotKeyPressed

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

    public class HotKeyManager 
    { 
    public static event EventHandler<HotKeyEventArgs> HotKeyPressed; 

    public static int RegisterHotKey(Keys key, KeyModifiers modifiers) 
    { 
     int id = System.Threading.Interlocked.Increment(ref _id); 
     RegisterHotKey(_wnd.Handle, id, (uint)modifiers, (uint)key); 
     return id; 
    } 

    public static bool UnregisterHotKey(int id) 
    { 
     return UnregisterHotKey(_wnd.Handle, id); 
    } 

    protected static void OnHotKeyPressed(HotKeyEventArgs e) 
    { 
     if (HotKeyManager.HotKeyPressed != null) 
     { 
     HotKeyManager.HotKeyPressed(null, e); 
     } 
    } 

    private static MessageWindow _wnd = new MessageWindow(); 

    private class MessageWindow : Form 
    { 
     protected override void WndProc(ref Message m) 
     { 
     if (m.Msg == WM_HOTKEY) 
     { 
      HotKeyEventArgs e = new HotKeyEventArgs(m.LParam); 
      HotKeyManager.OnHotKeyPressed(e); 
     } 

     base.WndProc(ref m); 
     } 

     private const int WM_HOTKEY = 0x312; 
    } 

    [DllImport("user32")] 
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); 

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

    private static int _id = 0; 
    } 


    public class HotKeyEventArgs : EventArgs 
    { 
    public readonly Keys Key; 
    public readonly KeyModifiers Modifiers; 

    public HotKeyEventArgs(Keys key, KeyModifiers modifiers) 
    { 
     this.Key = key; 
     this.Modifiers = modifiers; 
    } 

    public HotKeyEventArgs(IntPtr hotKeyParam) 
    { 
     uint param = (uint)hotKeyParam.ToInt64(); 
     Key = (Keys)((param & 0xffff0000) >> 16); 
     Modifiers = (KeyModifiers)(param & 0x0000ffff); 
    } 
    } 

    [Flags] 
    public enum KeyModifiers 
    { 
    Alt = 1, 
    Control = 2, 
    Shift = 4, 
    Windows = 8, 
    NoRepeat = 0x4000 
    } 

Các chương trình sau một cửa sổ đơn giản tạo ứng dụng sẽ giữ biểu mẫu chính ẩn và trả lời các sự kiện quan trọng. Tôi không xử lý việc đóng ứng dụng và việc hủy đăng ký khóa nóng, bạn có thể xử lý điều đó.

using System; 
using System.Windows.Forms; 

namespace HotKeyManager 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
     HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt); 
     HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);  
    } 

    void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) 
    { 
     MessageBox.Show("Hello"); 
    } 

    protected override void SetVisibleCore(bool value) 
    {  
     // Quick and dirty to keep the main window invisible  
     base.SetVisibleCore(false); 
    } 
    } 
} 
-4

Thêm phần này vào sự kiện KeyPress của mẫu của bạn:

if(e.KeyCode == (char)Keys.W && e.Modifiers == Keys.Control && e.Modifiers = Keys.Alt) 
{ 
    MessageBox.Show("I think this is a homework and that you should study instead of asking for an already cooked up answer on programming websites","Cheater"); 
} 
+0

Đó là câu trả lời sai. Bạn đã làm bài tập chưa? –

+0

Có vẻ như tôi đã bỏ lỡ phần "ứng dụng phải ẩn". – Wildhorn

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