2010-08-25 30 views
5

如何創建使用鍵盤快捷鍵執行操作的應用程序(應用程序必須是不可見的)。例如,當用戶按Ctrl + Alt + W時顯示MessageBox。如何在我的應用程序中調用函數的窗口中創建鍵盤快捷鍵?

+0

您需要包含更多關於您正在嘗試完成的信息。技術(winforms,wpf等)。另外你的問題似乎暗示它應該是試圖掛鉤鍵盤的後臺任務,但它不明確。 – 2010-08-25 17:50:33

+1

是的,我想讓它在背景上工作。和Winforms – 2010-08-25 18:02:27

回答

9

一個解決辦法是使用互操作和使用Win32 API RegisterHotKey。這是一個快速而骯髒的例子,我只是把它放在一起,所以它沒有得到很好的測試,我不確定沒有未被發現的副作用,但它應該起作用。

首先這裏是一個簡單HotKeyManager這需要照顧的基本互操作的,提供了一個隱藏的窗口來處理本地的Windows消息(WM_HOTKEY),它被翻譯成.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 
    } 

下面顯示一個簡單的Windows窗體應用程序,它將隱藏主窗體並響應熱鍵事件。我沒有處理關閉應用程序和取消註冊熱鍵,您可以處理該問題。

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

添加到您的窗體的KeyPress事件:

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

這是錯誤的答案。你做了功課嗎? – 2010-08-25 18:37:22

+0

看起來我錯過了「應用程序必須是隱形的」部分。 – Wildhorn 2010-08-25 18:38:12

相關問題