如何創建使用鍵盤快捷鍵執行操作的應用程序(應用程序必須是不可見的)。例如,當用戶按Ctrl + Alt + W時顯示MessageBox。如何在我的應用程序中調用函數的窗口中創建鍵盤快捷鍵?
5
A
回答
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
相關問題
- 1. 如何在Windows窗體應用程序中創建複合鍵盤快捷鍵?
- 2. 在窗口中創建快捷鍵10
- 3. 切換窗口的鍵盤快捷鍵
- 4. 網絡應用程序中常用的鍵盤快捷鍵
- 5. 在Windows應用程序中爲按鈕創建鍵盤快捷鍵?
- 6. Windows鍵盤快捷鍵和子窗口
- 7. 鍵盤快捷鍵專注於Mac OS中的應用程序
- 8. Metro應用程序中的鍵盤快捷鍵
- 9. c#窗口應用程序中的快捷鍵
- 10. Winkey應用程序(C#)中的Windows快捷鍵+ [快捷鍵]
- 11. 用於鍵盤組合和函數調用的vim快捷鍵
- 12. 用於Web應用程序的可用鍵盤快捷鍵
- 13. 調用鍵盤快捷鍵,一鍵
- 14. OSX鍵盤快捷鍵後臺應用程序,如何
- 15. 鍵盤快捷鍵,打開新窗口[Chrome擴展程序]
- 16. 如何使用Windows + m鍵盤快捷鍵最小化我的應用程序?
- 17. 爲網頁中的彈出窗口創建鍵盤快捷鍵php
- 18. 如何在Android Studio中創建自定義鍵盤快捷鍵
- 19. 使用鍵盤快捷鍵的iPhone/iPad應用程序?
- 20. 禁用GTKTreeView中的鍵盤快捷鍵
- 21. 以編程方式在python中創建鍵盤快捷鍵
- 22. 以編程方式在Revit中創建鍵盤快捷鍵
- 23. 如何使用鍵盤上的鍵「c」調用鍵盤快捷鍵功能
- 24. 以編程方式查找窗口的鍵盤快捷鍵
- 25. 鍵盤快捷鍵創建ubuntu
- 26. 創建鍵盤熱鍵/在Word快捷鍵使用VBA
- 27. 在linux c應用程序中創建自定義快捷鍵
- 28. iOS應用程序上的自定義鍵盤快捷鍵
- 29. MVC應用程序的鍵盤快捷鍵
- 30. 鍵盤快捷鍵不活動的應用程序
您需要包含更多關於您正在嘗試完成的信息。技術(winforms,wpf等)。另外你的問題似乎暗示它應該是試圖掛鉤鍵盤的後臺任務,但它不明確。 – 2010-08-25 17:50:33
是的,我想讓它在背景上工作。和Winforms – 2010-08-25 18:02:27