フォーカスの有無にかかわらず動作する C# WinForm アプリケーションで、いくつかのグローバル ホットキーを構成しようとしています。このために、次のようないくつかのフック ライブラリで少し遊んでみました。
しかし、これらのライブラリは本当に信頼できるものではないため、別の方法を探すことにしました。そして、私は1つを見つけましたが、1つの問題があります.以下に投稿された新しい方法では、キープレスをキャッチできますが、キーダウン(WM_KEYDOWN/0x100)とキーアップイベントもキャッチしたい...しかし、これは機能していません. 何か案は?
コード:
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
const int MOD_CONTROL = 0x0002;
const int MOD_SHIFT = 0x0004;
const int WM_HOTKEY = 0x0312;
const int WM_KEYDOWN = 0x0100;
private void Form1_Load(object sender, EventArgs e)
{
// Hook Keys
RegisterHotKey(this.Handle, 1, MOD_CONTROL, (int)0);
RegisterHotKey(this.Handle, 2, MOD_CONTROL, (int)Keys.X);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 1);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY && (int)m.WParam == 1)
{
MessageBox.Show("here");
}
if (m.Msg == WM_KEYDOWN && (int)m.WParam == 2)
this.Opacity = 100;
base.WndProc(ref m);
}