Я реализовал методы регистрации user32.dll и отмены регистрации горячих клавиш, но после регистрации горячих клавиш я никогда не получаю сообщение WndProc
0x0312
при нажатии горячих клавиш. Может кто-нибудь просмотреть мой код и помочь мне понять, почему я никогда не получаю сообщение 0x0312
.
Комбинация горячих клавиш, которые я пробовал до сих пор:
- Ctrl + Shift + F12
- F12
- F9
Моя реализация является просто самой распространенной реализацией:
[DllImport("c:\\windows\\system32\\user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("c:\\windows\\system32\\user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
protected override void WndProc(ref Message m) {
if(m.Msg == 0x0312) {
int id = m.WParam.ToInt32();
switch(id) {
case 0:
MessageBox.Show("Ctrl + Shift + F12 HotKey Pressed ! Do something here ... ");
break;
}
}
}
Я создал одноэлементный класс для обрабатывать регистрацию и отмену регистрации горячих клавиш:
public class HotKeyHandler {
//Hotkey register and unregister.
[DllImport("c:\\windows\\system32\\user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("c:\\windows\\system32\\user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public const int MOD_ALT = 0x0001;
public const int MOD_CONTROL = 0x0002;
public const int MOD_SHIFT = 0x0004;
public const int MOD_WIN = 0x0008;
byte ID = 0;
/// <summary>
/// Keep the constructor private due to singleton implementation
/// </summary>
private HotKeyHandler() { }
public static HotKeyHandler Instance = new HotKeyHandler();
public bool RegisterHotKey(IntPtr handle, int modifier, Key key) {
bool returnVal = RegisterHotKey(handle, ID, modifier, (int) key);
ID++;
return returnVal;
}
public void UnregisterAllHotKeys(IntPtr handle) {
for(short s = 0; s <= ID; s++) {
UnregisterHotKey(handle, s);
}
}
}
Наконец, я регистрирую горячую клавишу следующим образом:
HotKeyHandler.Instance.RegisterHotKey(this.Handle, HotKeyHandler.MOD_CONTROL | HotKeyHandler.MOD_SHIFT, Key.F12);