Differentiating between different hotkeys in c#
-
Hi, I'm making a hotkey app and don't know how to know which hotkey has been pressed. Here's the relevant part of my source: My hotkey class
class Hotkey : IMessageFilter { \[DllImport("user32.dll", SetLastError = true)\] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); \[DllImport("user32.dll", SetLastError = true)\] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); private const int WMHotkey = 0x0312; #region Properties private IntPtr \_Handle; public IntPtr Handle { get { return \_Handle; } set { \_Handle = value; } } private int \_Key; public int Key { get { return \_Key; } set { UnregisterHotkey(); try { RegisterHotKey(value, \_Mod); } catch (Exception e) { MessageBox.Show("Could not register Hotkey - there is probably a conflict. " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } \_Key = value; } } private byte \_Mod; public byte Mod { get { return \_Mod; } set { UnregisterHotkey(); try { RegisterHotKey(Key, value); } catch (Exception e) { MessageBox.Show("Could not register Hotkey - there is probably a conflict. " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } \_Mod = value; } } private bool \_isPressed; public bool isPressed { get { return \_isPressed; } set { \_isPressed = value; } } #endregion private event EventHandler HotkeyPressed; // Alt = 1, Ctrl = 2, Shift = 4, Win = 8 public Hotkey(Keys key, byte modifier, EventHandler hotKeyPressed) { if (key != Keys.None) { HotkeyPressed = hotKeyPressed; try { RegisterHotKey(CharCodeFromKeys(key), modifier); } catch (Exception e) {
-
Hi, I'm making a hotkey app and don't know how to know which hotkey has been pressed. Here's the relevant part of my source: My hotkey class
class Hotkey : IMessageFilter { \[DllImport("user32.dll", SetLastError = true)\] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); \[DllImport("user32.dll", SetLastError = true)\] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); private const int WMHotkey = 0x0312; #region Properties private IntPtr \_Handle; public IntPtr Handle { get { return \_Handle; } set { \_Handle = value; } } private int \_Key; public int Key { get { return \_Key; } set { UnregisterHotkey(); try { RegisterHotKey(value, \_Mod); } catch (Exception e) { MessageBox.Show("Could not register Hotkey - there is probably a conflict. " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } \_Key = value; } } private byte \_Mod; public byte Mod { get { return \_Mod; } set { UnregisterHotkey(); try { RegisterHotKey(Key, value); } catch (Exception e) { MessageBox.Show("Could not register Hotkey - there is probably a conflict. " + "/br " + e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } \_Mod = value; } } private bool \_isPressed; public bool isPressed { get { return \_isPressed; } set { \_isPressed = value; } } #endregion private event EventHandler HotkeyPressed; // Alt = 1, Ctrl = 2, Shift = 4, Win = 8 public Hotkey(Keys key, byte modifier, EventHandler hotKeyPressed) { if (key != Keys.None) { HotkeyPressed = hotKeyPressed; try { RegisterHotKey(CharCodeFromKeys(key), modifier); } catch (Exception e) {
Hi, the id parameter in RegisterHotkey must be unique (your _Mod is not good); it is used internally to discriminate the different hotkeys; I once used the following code:
int chr=(int)name\[0\]; if (chr>=(int)'a' && chr<=(int)'z') chr+=(int)'A'-(int)'a'; // identifier must be unique, so we use a hash code int ident=GetType().GetHashCode()+(modifiers<<16)+chr;
but that is just one attempt to get unique numbers for some command based on its name (some textual description) and the modifier keys (similar to Control.ModifierKeys) all inside a form. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }