private void btnRegHotkey_Click(object sender, EventArgs e) { ModifierKeys_e controlKeys = ModifierKeys_e.None; Keys key; if (ckbCtrl.Checked) { controlKeys |= ModifierKeys_e.Control; } if (ckbShift.Checked) { controlKeys |= ModifierKeys_e.Shift; } if (ckbAlt.Checked) { controlKeys |= ModifierKeys_e.Alt; } Object selectedKey = cmbKeys.Text; char keyLetter = Convert.ToChar(selectedKey); //char 'M' //key = VkKeyScan(keyLetter); //0x0000014d //key = (Keys)selectedKey; // can not work key = (Keys)keyLetter; // can work -> convert char 'M' to Keys.M if (registerHotkey(controlKeys, key, hook_KeyPressed)) { saveHotkey(); showTip("快捷键注册成功."); } }
/// <summary> /// Registers a hot key in the system. /// </summary> /// <param name="modifier">The modifiers that are associated with the hot key.</param> /// <param name="key">The key itself that is associated with the hot key.</param> public void RegisterHotKey(ModifierKeys_e modifier, Keys key) { // increment the counter. _currentId = _currentId + 1; // register the hot key. if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key)) throw new InvalidOperationException("Couldn't register this hot key. Please choose another hotkey!"); }
/// <summary> /// Registers a hot key in the system. /// </summary> /// <param name="modifier">The modifiers that are associated with the hot key.</param> /// <param name="key">The key itself that is associated with the hot key.</param> public void RegisterHotKey(ModifierKeys_e modifier, Keys key) { // increment the counter. _currentId = _currentId + 1; // register the hot key. if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key)) { throw new InvalidOperationException("Couldn't register this hot key. Please choose another hotkey!"); } }
/// <summary> /// Overridden to get the notifications. /// </summary> /// <param name="m"></param> protected override void WndProc(ref Message m) { base.WndProc(ref m); // check if we got a hot key pressed. if (m.Msg == WM_HOTKEY) { // get the keys. Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); ModifierKeys_e modifier = (ModifierKeys_e)((int)m.LParam & 0xFFFF); // invoke the event to notify the parent. if (KeyPressed != null) { KeyPressed(this, new KeyPressedEventArgs(modifier, key)); } } }
public bool registerHotkey(ModifierKeys_e controlKeys, Keys key, funcDelegate func) { bool registerOk = false; KeyboardHook hook = new KeyboardHook(); // register the event that is fired after the key press. hook.KeyPressed += new EventHandler <KeyPressedEventArgs>(func); try { hook.RegisterHotKey(controlKeys, key); registerOk = true; } catch (Exception ex) { registerOk = false; MessageBox.Show(ex.Message); } return(registerOk); }
internal KeyPressedEventArgs(ModifierKeys_e modifier, Keys key) { _modifier = modifier; _key = key; }
public bool registerHotkey(ModifierKeys_e controlKeys, Keys key, funcDelegate func) { bool registerOk = false; KeyboardHook hook = new KeyboardHook(); // register the event that is fired after the key press. hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(func); try { hook.RegisterHotKey(controlKeys, key); registerOk = true; } catch (Exception ex) { registerOk = false; MessageBox.Show(ex.Message); } return registerOk; }