Exemplo n.º 1
0
        private static void SetKeyboardHook(ref KeyboardHook hook, EventHandler <KeyPressedEventArgs> func, int keyData)
        {
            // Get Key and Modifiers from KeyData
            Keys data = (Keys)keyData;
            Keys key  = data & Keys.KeyCode;
            Keys mods = data & Keys.Modifiers;

            // Set the hook
            hook = new KeyboardHook();
            // register the event that is fired after the key press.
            hook.KeyPressed += func;
            try
            {
                hook.RegisterHotKey(HotKeys.ToModKeys(mods), key);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Esta funcion toma los eventos KeyDown sobre los controles y actualiza el texto mostrando la informacion de la HotKey
        /// </summary>
        /// <param name="textBox">Control donde se actualiza el texto con la nueva HotKey</param>
        /// <param name="e">KeyEventArgs e</param>
        /// <param name="hook">Variable tipo KeyboardHook donde se crea el hook</param>
        /// <param name="func">Funcion que es llamada cuando las teclas se apretan </param>
        /// <returns>-1 Si no debe modificarse el valor del setting, 0 si se elimina la hotkey, sino int32 con la KeyData</returns>
        private int CheckHotKey(TextBox textBox, KeyEventArgs e, ref KeyboardHook hook, EventHandler <KeyPressedEventArgs> func)
        {
            e.Handled = true;

            // Fetch the actual shortcut key.
            Keys key  = e.KeyCode;
            Keys mods = e.Modifiers;
            Keys data = e.KeyData;

            // Ignore modifier keys.
            if (key == Keys.LShiftKey || key == Keys.RShiftKey || key == Keys.ShiftKey ||
                key == Keys.LControlKey || key == Keys.RControlKey || key == Keys.ControlKey ||
                key == Keys.Alt || key == Keys.LWin || key == Keys.RWin)
            {
                return(-1);
            }

            // HotKey remover keys
            if (key == Keys.Back || key == Keys.Delete || key == Keys.Escape)
            {
                hook.Dispose();
                textBox.Text = "None";
                return(0);
            }

            // Set the hook
            hook = new KeyboardHook();
            // register the event that is fired after the key press.
            hook.KeyPressed += func;
            try
            {
                hook.RegisterHotKey(HotKeys.ToModKeys(mods), key);
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(-1);
            }
            // Update the text box.
            textBox.Text = GetTextForKeyAndMods(key, mods);
            return((int)data);
        }