【发布时间】:2013-03-15 09:18:41
【问题描述】:
我有一个应用程序,它总是检查是否按下了像 F12 这样的键。它不需要集中在我的应用程序的主窗口中。我试过这段代码:
public int a = 1;
// DLL libraries used to manage hotkeys
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
const int MYACTION_HOTKEY_ID = 1;
public Form1()
{
InitializeComponent();
// Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
// Compute the addition of each combination of the keys you want to be pressed
// ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6...
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int) Keys.F12);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID)
{
a++;
MessageBox.Show(a.ToString());
}
base.WndProc(ref m);
}
我在RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int) Keys.F12);这一行设置了0,这样只有按下F12才会捕获。
但它没有用。我该如何解决这个问题?
这里有些行我看不懂:
const int MYACTION_HOTKEY_ID = 1;
m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID
base.WndProc(ref m);
谁能帮我理解这些台词?
【问题讨论】:
标签: c# keyboard-hook global-hotkey