我正在使用c#处理窗口应用程序 。我想按键时显示我的应用程序窗口,但我不知道如何创建热键..告诉我?
答案 0 :(得分:3)
不要使用钩子,更好地使用RegisterHotKey和UnregisterHotKey win32函数:
public class YourForm : Form
{
private const int WM_HOTKEY = 0x0312;
[Flags]
private enum MOD : uint
{
MOD_ALT = 0x0001,
MOD_CONTROL = 0x0002,
MOD_SHIFT = 0x0004,
MOD_WIN = 0x0008
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, MOD fsModifiers, uint vk);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
protected override WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY)
{
// Your code here
}
}
}
答案 1 :(得分:1)
你可以创建一个启动的应用程序,然后安装一个所谓的键盘钩子。这是一个很好的例子:
http://www.codeproject.com/KB/cs/globalhook.aspx
之后,应用程序可以最小化到任务栏或系统托盘。然后键盘钩子的事件处理程序重新激活应用程序。