Giter VIP home page Giter VIP logo

hotkeylistener's Introduction

Yow, Willy here!

👋 Many thanks for stopping by! Name is Willy Kimura, and I simply love building products.


Currently open to contracts, opportunities and/or consultation.

Languages & Tools used:

android csharp css3 docker dotnet express figma firebase graphql heroku html5 java javascript mongodb mssql mysql nodejs php react redux sass webpack

hotkeylistener's People

Contributors

merlincooper avatar mfroeh avatar willy-kimura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

hotkeylistener's Issues

WPF support for HotkeySelector

Hello, this project is awesome.

Do you have any plans to add WPF support for the HotkeySelector?

I have tried to make my own which does work.

It uses System.Windows.Controls.TextBox

Add references to these (WPF) assemblies:

WindowsBase
PresentationCore
PresentationFrameWork

HotkeySelectorWPF.cs

#region Copyright

/*
 * Developer    : Willy Kimura (WK).
 * Library      : HotkeySelector.
 * License      : MIT.
 * 
 */

#endregion

using System;
using System.Collections;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Controls;

namespace WK.Libraries.HotkeyListenerNS
{
    /// <summary>
    /// Provides support for enabling standard Windows controls 
    /// and User controls to select hotkeys at runtime. 
    /// Combined with the <see cref="HotkeyListener"/> class, 
    /// you can easily enable the selection and registering of 
    /// hotkeys for a seamless end-user experience.
    /// </summary>
    //[DebuggerStepThrough]
    [Description("Provides support for enabling standard Windows controls " +
                 "and User controls to select hotkeys at runtime.")]
    public partial class HotkeySelectorWPF : Component
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="HotkeySelector"/> class.
        /// </summary>
        public HotkeySelectorWPF() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="HotkeySelector"/> class.
        /// </summary>
        public HotkeySelectorWPF(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        #endregion

        #region Fields

        // These variables store the selected hotkey and modifier key(s).
        private Keys _hotkey = Keys.None;
        private Keys _modifiers = Keys.None;

        // ArrayLists used to enforce the use of proper modifiers.
        // Shift+A isn't a valid hotkey, for instance.
        private ArrayList _needNonShiftModifier = null;
        private ArrayList _needNonAltGrModifier = null;

        // Stores the list of enabled hotkey selection controls.
        private List<System.Windows.Controls.Control> _controls = new List<System.Windows.Controls.Control>();

        #endregion

        #region Properties

        #region Public

        /// <summary>
        /// Gets or sets the text to be displayed in a 
        /// control when no hotkey has been set. 
        /// (Preferred default text is "None")
        /// </summary>
        public string EmptyHotkeyText { get; set; } = "None";

        /// <summary>
        /// Gets or sets the text to be displayed in a control 
        /// when an invalid or unsupported hotkey is pressed.
        /// (Preferred default text is "(Unsupported)")
        /// </summary>
        public string InvalidHotkeyText { get; set; } = "Unsupported";

        #endregion

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Enables a control for hotkey selection and previewing.
        /// This will make use of the control's Text property to 
        /// preview the current hotkey selected.
        /// </summary>
        /// <param name="control">The control to enable.</param>
        public bool Enable(System.Windows.Controls.TextBox control)
        {
            try
            {
                control.Text = EmptyHotkeyText;

                control.KeyDown += new System.Windows.Input.KeyEventHandler(OnKeyDown);
                control.KeyUp += new System.Windows.Input.KeyEventHandler(OnKeyUp);

                ResetModifiers();

                try
                {
                    _controls.Add(control);
                }
                catch (Exception) { }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Enables a control for hotkey selection and previewing.
        /// This will make use of the control's Text property to 
        /// preview the current hotkey selected.
        /// </summary>
        /// <param name="control">The control to enable.</param>
        /// <param name="hotkey">Assign the default hotkey to be previewed in the control.</param>
        public bool Enable(System.Windows.Controls.TextBox control, Hotkey hotkey)
        {
            try
            {
                Enable(control);

                _hotkey = hotkey.KeyCode;
                _modifiers = hotkey.Modifiers;

                Refresh(control);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Disables a control for hotkey selection and previewing.
        /// </summary>
        /// <param name="control">The control to disable.</param>
        /// <param name="clearKeys">Clear the control's previewed keys?</param>
        public bool Disable(System.Windows.Controls.TextBox control, bool clearKeys = true)
        {
            try
            {
                control.KeyDown -= OnKeyDown;
                control.KeyUp -= OnKeyUp;

                if (clearKeys)
                    control.Text = string.Empty;

                try
                {
                    if (_controls.Contains(control))
                        _controls.Remove(control);
                }
                catch (Exception) { }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether a specific 
        /// control is enabled for hotkey selection.
        /// </summary>
        /// <param name="control">The control to determine.</param>
        public bool IsEnabled(System.Windows.Controls.TextBox control)
        {
            if (_controls.Contains(control))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Sets a hotkey selection to be previewed in a control. 
        /// Thsi does not automatically enable the control for 
        /// hotkey selection. For this, please use the <see cref="Enable(Control)"/> method.
        /// </summary>
        /// <param name="control">The control to set.</param>
        /// <param name="hotkey">Provide a standard key or key combination string.</param>
        public bool Set(System.Windows.Controls.TextBox control, Hotkey hotkey)
        {
            try
            {
                Refresh(control);

                control.Text = Convert(hotkey);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Sets a hotkey selection to be previewed in a control. 
        /// Thsi does not automatically enable the control for 
        /// hotkey selection. For this, please use the <see cref="Enable(Control)"/> method.
        /// </summary>
        /// <param name="control">The control to set.</param>
        /// <param name="key">Provide a standard key selection.</param>
        /// <param name="modifiers">Provide a modifier key selection.</param>
        public bool Set(System.Windows.Controls.TextBox control, Keys key, Keys modifiers)
        {
            try
            {
                _hotkey = key;
                _modifiers = modifiers;

                Refresh(control);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Clears the currently previewed hotkey 
        /// selection displayed in a control.
        /// </summary>
        public void Clear(System.Windows.Controls.TextBox control)
        {
            this._hotkey = Keys.None;
            this._modifiers = Keys.None;

            Refresh(control);
        }

        /// <summary>
        /// (Variant of the <see cref="Clear(System.Windows.Controls.TextBox)"/> method) 
        /// Clears the currently previewed hotkey 
        /// selection displayed in a control.
        /// </summary>
        public void Reset(System.Windows.Controls.TextBox control)
        {
            this._hotkey = Keys.None;
            this._modifiers = Keys.None;

            Refresh(control);
        }

        /// <summary>
        /// [Helper] Converts keys or key combinations to their string types.
        /// </summary>
        /// <param name="hotkey">The hotkey to convert.</param>
        public string Convert(Hotkey hotkey)
        {
            try
            {
                _hotkey = hotkey.KeyCode;
                _modifiers = hotkey.Modifiers;

                string parsedHotkey = string.Empty;

                // No modifier or shift only, and a hotkey that needs another modifier.
                if ((_modifiers == Keys.Shift || _modifiers == Keys.None))
                {
                    if (_needNonShiftModifier != null && _needNonShiftModifier.Contains((int)this._hotkey))
                    {
                        if (this._modifiers == Keys.None)
                        {
                            // Set Ctrl+Alt as the modifier unless Ctrl+Alt+<key> won't work.
                            if (_needNonAltGrModifier.Contains((int)this._hotkey) == false)
                            {
                                this._modifiers = Keys.Alt | Keys.Control;
                            }
                            else
                            {
                                // ...In that case, use Shift+Alt instead.
                                this._modifiers = Keys.Alt | Keys.Shift;
                            }
                        }
                        else
                        {
                            // User pressed Shift and an invalid key (e.g. a letter or a number), 
                            // that needs another set of modifier keys.
                            this._hotkey = Keys.None;
                        }
                    }
                }

                // Without this code, pressing only Ctrl 
                // will show up as "Control + ControlKey", etc.
                if (this._hotkey == Keys.Menu || /* Alt */
                    this._hotkey == Keys.ShiftKey ||
                    this._hotkey == Keys.ControlKey)
                {
                    this._hotkey = Keys.None;
                }

                if (this._modifiers == Keys.None)
                {
                    // LWin/RWin don't work as hotkeys...
                    // (neither do they work as modifier keys in .NET 2.0).
                    if (_hotkey == Keys.None || _hotkey == Keys.LWin || _hotkey == Keys.RWin)
                    {
                        parsedHotkey = string.Empty;
                    }
                    else
                    {
                        parsedHotkey = this._hotkey.ToString();
                    }
                }
                else
                {
                    parsedHotkey = this._modifiers.ToString() + " + " + this._hotkey.ToString();
                }

                return parsedHotkey;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        #endregion

        #region Private

        /// <summary>
        /// Resets the hotkey modifiers to their defaults.
        /// </summary>
        private void ResetModifiers()
        {
            // Fill the ArrayLists that contain  
            // all invalid hotkey combinations.
            _needNonShiftModifier = new ArrayList();
            _needNonAltGrModifier = new ArrayList();

            PopulateModifierLists();
        }

        /// <summary>
        /// Populates the ArrayLists specifying disallowed Hotkeys 
        /// such as Shift+A, Ctrl+Alt+4 (produces 'dollar' sign).
        /// </summary>
        private void PopulateModifierLists()
        {
            // Shift + 0 - 9, A - Z.
            for (Keys k = Keys.D0; k <= Keys.Z; k++)
                _needNonShiftModifier.Add((int)k);

            // Shift + Numpad keys.
            for (Keys k = Keys.NumPad0; k <= Keys.NumPad9; k++)
                _needNonShiftModifier.Add((int)k);

            // Shift + Misc (,;<./ etc).
            for (Keys k = Keys.Oem1; k <= Keys.OemBackslash; k++)
                _needNonShiftModifier.Add((int)k);

            // Shift + Space, PgUp, PgDn, End, Home.
            for (Keys k = Keys.Space; k <= Keys.Home; k++)
                _needNonShiftModifier.Add((int)k);

            // Misc keys that we can't loop through.
            _needNonShiftModifier.Add((int)Keys.Insert);
            _needNonShiftModifier.Add((int)Keys.Help);
            _needNonShiftModifier.Add((int)Keys.Multiply);
            _needNonShiftModifier.Add((int)Keys.Add);
            _needNonShiftModifier.Add((int)Keys.Subtract);
            _needNonShiftModifier.Add((int)Keys.Divide);
            _needNonShiftModifier.Add((int)Keys.Decimal);
            _needNonShiftModifier.Add((int)Keys.Return);
            _needNonShiftModifier.Add((int)Keys.Escape);
            _needNonShiftModifier.Add((int)Keys.NumLock);
            _needNonShiftModifier.Add((int)Keys.Scroll);
            _needNonShiftModifier.Add((int)Keys.Pause);

            // Ctrl+Alt + 0 - 9.
            for (Keys k = Keys.D0; k <= Keys.D9; k++)
                _needNonAltGrModifier.Add((int)k);
        }

        /// <summary>
        /// Refreshes the previewed hotkey combination displayed in a control.
        /// </summary>
        /// <param name="control">
        /// The control providing hotkey selection.
        /// </param>
        private void Refresh(System.Windows.Controls.TextBox control)
        {
            Refresh(control, false);
        }

        /// <summary>
        /// Refreshes the previewed hotkey combination displayed in a control.
        /// </summary>
        /// <param name="control">
        /// The control providing hotkey selection.
        /// </param>
        /// <param name="internalCall">
        /// Specifies whether this function is 
        /// called internally or by the user.
        /// </param>
        private void Refresh(System.Windows.Controls.TextBox control, bool internalCall)
        {
            try
            {
                string parsedHotkey = string.Empty;

                // No hotkey set.
                if (this._hotkey == Keys.None && this._modifiers == Keys.None)
                {
                    control.Text = EmptyHotkeyText;
                    return;
                }

                // LWin/RWin don't work as hotkeys...
                // (neither do they work as modifier keys in .NET 2.0).
                if (this._hotkey == Keys.LWin || this._hotkey == Keys.RWin)
                {
                    control.Text = InvalidHotkeyText;

                    return;
                }

                if (this._modifiers == Keys.None)
                {
                    if (this._hotkey == Keys.None)
                    {
                        control.Text = EmptyHotkeyText;

                        return;
                    }
                    else
                    {
                        // We get here if we've got a hotkey that is valid without a modifier,
                        // like F1-F12, Media-keys etc.
                        control.Text = this._hotkey.ToString();

                        return;
                    }
                }
                else
                {
                    // Only validate input if it comes from the user.
                    if (internalCall == false)
                    {
                        // No modifier or shift only, and a hotkey that needs another modifier.
                        if ((this._modifiers == Keys.Shift || this._modifiers == Keys.None) &&
                            this._needNonShiftModifier.Contains((int)this._hotkey))
                        {
                            if (this._modifiers == Keys.None)
                            {
                                // Set Ctrl+Alt as the modifier unless Ctrl+Alt+<key> won't work.
                                if (_needNonAltGrModifier.Contains((int)this._hotkey) == false)
                                {
                                    this._modifiers = Keys.Alt | Keys.Control;
                                }
                                else
                                {
                                    // ...In that case, use Shift+Alt instead.
                                    this._modifiers = Keys.Alt | Keys.Shift;
                                }
                            }
                            else
                            {
                                // User pressed Shift and an invalid key (e.g. a letter or a number), 
                                // that needs another set of modifier keys.
                                this._hotkey = Keys.None;

                                control.Text = this._modifiers.ToString() + $" + {InvalidHotkeyText}";

                                return;
                            }
                        }
                    }
                }

                // Without this code, pressing only Ctrl 
                // will show up as "Control + ControlKey", etc.
                if (this._hotkey == Keys.Menu || /* Alt */
                    this._hotkey == Keys.ShiftKey ||
                    this._hotkey == Keys.Alt ||
                    this._hotkey == Keys.ControlKey ||
                    this._hotkey == Keys.LControlKey ||
                    this._hotkey == Keys.RControlKey ||
                    this._hotkey == Keys.LShiftKey ||
                    this._hotkey == Keys.RShiftKey)
                {
                    this._hotkey = Keys.None;
                }

                // A final compilation of the processed keys in string format.
                if (this._modifiers == Keys.None)
                    parsedHotkey = this._hotkey.ToString();
                else
                    parsedHotkey = this._modifiers.ToString() + " + " + this._hotkey.ToString();

                control.Text = parsedHotkey;

                return;
            }
            catch (Exception) { }
        }

        #endregion

        #endregion

        #region Events

        #region Private

        /// <summary>
        /// Fires when a key is pressed down. Here, we'll want to update the Text  
        /// property to notify the user what key combination is currently pressed.
        /// </summary>
        private void OnKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Delete || e.Key == (System.Windows.Input.Key.LeftCtrl | System.Windows.Input.Key.RightCtrl | System.Windows.Input.Key.Delete))
                Reset((System.Windows.Controls.TextBox)sender);

            if (e.Key == (System.Windows.Input.Key.LeftShift | System.Windows.Input.Key.RightShift | System.Windows.Input.Key.Insert))
            {
                this._modifiers = Keys.Shift;
                e.Handled = true;
            }

            // Clear the current hotkey.
            if (e.Key == System.Windows.Input.Key.Back || e.Key == System.Windows.Input.Key.Delete)
            {
                Reset((System.Windows.Controls.TextBox)sender);

                return;
            }
            else
            {
                this._modifiers = ToWinforms(e.KeyboardDevice.Modifiers);
                this._hotkey = (Keys)System.Windows.Input.KeyInterop.VirtualKeyFromKey(e.Key);

                Refresh((System.Windows.Controls.TextBox)sender);
            }
        }

        /// <summary>
        /// Source: https://stackoverflow.com/questions/1153009/how-can-i-convert-system-windows-input-key-to-system-windows-forms-keys
        /// </summary>
        /// <param name="modifier"></param>
        /// <returns></returns>
        private System.Windows.Forms.Keys ToWinforms(System.Windows.Input.ModifierKeys modifier)
        {
            var retVal = System.Windows.Forms.Keys.None;
            if (modifier.HasFlag(System.Windows.Input.ModifierKeys.Alt))
            {
                retVal |= System.Windows.Forms.Keys.Alt;
            }
            if (modifier.HasFlag(System.Windows.Input.ModifierKeys.Control))
            {
                retVal |= System.Windows.Forms.Keys.Control;
            }
            if (modifier.HasFlag(System.Windows.Input.ModifierKeys.None))
            {
                // Pointless I know
                retVal |= System.Windows.Forms.Keys.None;
            }
            if (modifier.HasFlag(System.Windows.Input.ModifierKeys.Shift))
            {
                retVal |= System.Windows.Forms.Keys.Shift;
            }
            if (modifier.HasFlag(System.Windows.Input.ModifierKeys.Windows))
            {
                // Not supported lel
            }
            return retVal;
        }

        /// <summary>
        /// Fires when all keys are released. If the current hotkey isn't valid, reset it.
        /// Otherwise, do nothing and keep the Text and hotkey as it was.
        /// </summary>
        private void OnKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

            if (this._hotkey == Keys.None && e.KeyboardDevice.Modifiers == System.Windows.Input.ModifierKeys.None)
            {
                Reset((System.Windows.Controls.TextBox)sender);

                return;
            }
        }

        #endregion

        #endregion
    }
}

save hotkey in settings

Hello,
i use your library in a project and it works very well. Is it possible to store the hotkey in Settings.settings or something else? Can i find an example for this?

thanks a lot and best regards
Benno

Screen resizes

I have a simple C# Windows Form App (.Net Framework 4.6)

HotkeyListener works perfectly. The one issue I have is that when I hit a define hot key, the screen resizes.

Any ideas?

Most appreciated

ScrollLock based combo's

Hi @Willy-Kimura,

Would it be possible to catch ScrollLock based keycombo's with your codebase? Currently ScrollLock is detected, but as soon as a second key is pressed, the ScrollLock part disappears.

I've had some users (HASS.Agent) ask for it, and thought I'd ask you first before trying myself.

Thanks again for sharing your amazing library :)

How do I change hotkeys

I have been trying for literally hours now and cannot get it to work!

hotkey1 is already set to F7 on startup. I want the user to change the keybind to something else of their choice. How do I go about do so?

` private void Form1_KeyDown(object sender, KeyEventArgs e)

    {

        var hkl = new HotkeyListener();

        if (recordPOS.Text == "Change")

        {

            recordPOS.Text = e.KeyCode.ToString();

            Keys[] keys = (Keys[])typeof(Keys).GetEnumValues();
            Keys pickedKey = keys.FirstOrDefault(k => k.ToString() == recordPOS.Text);

            hkl.Update(ref hotkey1, new Hotkey(pickedKey));
        }
    }`

Question: Sending an hotkey stored class Hotkey to windows application

Found today the HotkeyListener on NuGet.

Would like to use it in an application were the user can define a hotkey which will be used to remotely control another application. But I seem to miss how I can send the short-cut.

P.S.: The application I'm working on is using the Win32 function keybd_event - which is already superseded by another function. Thus I will have to update the code anyway. But if possible I would right away start using a more convenient solution.

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-keybd_event

Exception raised in UIAutomationClientsideProviders.dll (see GetTextFromAutomationElement)

Hi again,

Thanks for the release of the 1.3 which I have followed closely.

I'm trying to register a simple {p} hotkey (no modifier) but when I press the key it raises an exception (actually two but they are similar) which I have listed below.

System.ArgumentException
image

`A first chance exception of type 'System.ArgumentException' occurred in UIAutomationClientsideProviders.dll

Additional information: Value does not fall within the expected range.`

System.Windows.Automation.ElementNotAvailableException
image

`An exception of type 'System.Windows.Automation.ElementNotAvailableException' occurred in UIAutomationClient.dll and wasn't handled before a managed/native boundary

Additional information: The target element corresponds to UI that is no longer available (for example, the parent window has closed).`

.net 5.0 support

Trying to use it in a .net 5.0 project. While resolving "Hotkey", the below error happens:

CS0012: The type 'Keys' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms', Version = 4.0.0.0

Any workaround for using in .net 5.0 ?

Prevent RegisterHotKey from stopping the default key action?

Hi again,

Is there a functionality to prevent RegisterHotKey from stopping the default action ? The problem is, this is stopping the F2 key from working for other applications, such as Excel.

Couldn't you add a e.Cancel field in the HotkeyEventArgs for exemple ? If set to false, a SendKeys.Send("{F2}"); would be sent to the focused window ?

Hotkey Manager

Hi!

I am looking for a way to do a hotkey editor like the one in Visual Studio for example, a list with hotkeys and you could assign 1 hotkey per row. Can I easily serialize all hotkeys with your library? In a first stage, it would be just select which action-> change hotkey and a small modal window will pop to capture your new hotkey and reassign it

Strange behavior of Google Chrome

Hi!

I just made C# program using HotkeyListener and registred 4 hotkeys:

  • Ctrl + Shift + ~
  • Ctrl + Shift + 1
  • Ctrl + Shift + 2
  • Ctrl + Shift + 3

Now when program is running it's accepting hot keys if focused or not. That's what I wanted.
But when Google Chrome is opened and if I press any of those 4 hotkeys, Chrome opens inspect Window.
If I close my program and try same hot keys nothing happens in Chrome.

I tried also other hotkeys, but behavior in chrome stay the same.

What could be wrong?

image

public partial class Form1 : Form
{
static List<S7.Net.Types.DataItem> list = new List<S7.Net.Types.DataItem>();
static Plc plc = new Plc(CpuType.S71200, "192.168.0.90", 0, 1);
static HotkeyListener hkl = new HotkeyListener();
static Hotkey hotkey1 = new Hotkey(Keys.Control | Keys.Shift, Keys.Oemtilde);
static Hotkey hotkey2 = new Hotkey(Keys.Control | Keys.Shift, Keys.D1);
static Hotkey hotkey3 = new Hotkey(Keys.Control | Keys.Shift, Keys.D2);
static Hotkey hotkey4 = new Hotkey(Keys.Control | Keys.Shift, Keys.D3);

     private void Form1_Load(object sender, EventArgs e)
    {
        hkl.Add(new[] { hotkey1, hotkey2, hotkey3, hotkey4 });
        hkl.HotkeyPressed += Hkl_HotkeyPressed;
    }

    private void Hkl_HotkeyPressed(object sender, HotkeyEventArgs e)
    {
        if (e.Hotkey == hotkey1)
            Console.WriteLine("HK1");

        if (e.Hotkey == hotkey2)
            Console.WriteLine("HK2");

        if (e.Hotkey == hotkey3)
            Console.WriteLine("HK3");

        if (e.Hotkey == hotkey4)
            Console.WriteLine("HK4");
        
    }
   private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        hkl.RemoveAll();
    }

}

Win32Exception thrown in even the most basic program

While using this in my program I noticed I was getting a lot of exceptions. I created a new temporary program that is the most basic possible and I'm still seeing them in the output window of Visual Studio 2019.

Here is my code:

using System;
using System.Windows.Forms;
using WK.Libraries.HotkeyListenerNS;

namespace TestApp
{
    public partial class Form1 : Form
    {
        private HotkeyListener keyhook;

        public Form1()
        {
            InitializeComponent();
            keyhook = new HotkeyListener();
            keyhook.Add(new Hotkey("Control+Alt+L"));
            keyhook.HotkeyPressed += Hkl_HotkeyPressed;
        }

        private void Hkl_HotkeyPressed(object sender, HotkeyEventArgs e)
        {
            Console.WriteLine("hotkey received");
        }
    }
}

No exception is thrown until I press the hotkey combo, and then I get this (each time I press the hotkey):

''TestApp.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Symbols loaded.
'TestApp.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\scottismyname\source\repos\TestApp\TestApp\bin\Debug\TestApp.exe'. Symbols loaded.
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Symbols loaded.
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Symbols loaded.
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Symbols loaded.
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\Users\scottismyname\source\repos\TestApp\TestApp\bin\Debug\HotkeyListener.dll'. 
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Symbols loaded.
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. 
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Symbols loaded.
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'. Symbols loaded.
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\UIAutomationClient\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationClient.dll'. 
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. 
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll'. 
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\UIAutomationProvider\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationProvider.dll'. 
'TestApp.exe' (CLR v4.0.30319: TestApp.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\UIAutomationClientsideProviders\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationClientsideProviders.dll'. 
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll
hotkey received

Compile Error in VS 2019

After cloning you git repository and compile i get an error "System.ArgumentException
HResult=0x80070057
Nachricht = Die Assembly "C:\Users\XXX\source\repos\HotkeyListener\HotkeyListener\obj\Debug\HotkeyListener.dll" enthält keine UserControl-Typen.
Quelle = UserControlTestContainer
Stapelüberwachung:
at Microsoft.VisualStudio.Tools.UserControlTestContainer.UserControlTestContainer_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
"

AccessViolationException on Windows 11

I've been using your nuget for a while on W10, works really great, thanks for sharing :)

Recently I made the switch to W11, and now when using the same code, I get the following exception:

[MAIN] AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at MS.Internal.Automation.UiaCoreApi.RawTextRange_GetText(SafeTextRangeHandle hobj, Int32 maxLength, String& result)
   at System.Windows.Automation.Text.TextPatternRange.GetText(Int32 maxLength)
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.<>c.<GetTextFromAutomationElement>b__5_0(TextPatternRange r)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.String.Join(String separator, IEnumerable`1 values)
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.GetTextFromAutomationElement()
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.<.ctor>b__0_0()
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.TryGetSelectedTextFromActiveControl()
   at WK.Libraries.HotkeyListenerNS.Helpers.SourceAttributes.GetSelection()
   at WK.Libraries.HotkeyListenerNS.Helpers.HotkeyHandle.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Program.Main()

It looks like the TextSelectionReader is causing a crash, perhaps it's breaking some security measures.

Do you have the time to look into this, or could you give me some pointers on how to remove/disable the TextSelectionReader from your source?

Thanks!

Win Key Exception

When I use the key 'RWin' the app crashes, for example (CTRL+RWIN+J) :
Hotkey hotkey1 = new Hotkey(Keys.Control | Keys.LWin, Keys.J);

Here the exception generated:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Value not found 'LWinJ'

It says LWinJ, LWIN be joined with the next key.

How I can fix it?

KeyNotFoundException when hotkey is pressed twice and suspending

If a defined hotkey is pressed twice and is currently suspended, I get a KeyNotFoundException.

Hotkey hotkey1 = new Hotkey(Keys.Escape);
if (e.Hotkey == hotkey1)
            {
                Debug.WriteLine("Hotkey pressed");
                hkl.Suspend();
            }

Execute this code and press the Escape key twice.

System.Collections.Generic.KeyNotFoundException
  HResult=0x80131577
  Nachricht = Der angegebene Schlüssel war nicht im Wörterbuch angegeben.
  Quelle = mscorlib
  Stapelüberwachung:
   bei System.Collections.Generic.Dictionary2.get_Item(TKey key)
   bei WK.Libraries.HotkeyListenerNS.Helpers.HotkeyHandle.WndProc(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   bei System.Windows.Forms.Application.Run(Form mainForm)
   bei HotkeyBugTest.Program.Main() in C:\Users\Eddy\source\repos\HotkeyBugTest\HotkeyBugTest\Program.cs: Zeile19

  Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
    [Externer Code]
    HotkeyBugTest.Program.Main() in Program.cs

System.Windows.Forms.Keys.VolumeUp gives VolumeUp + None which doesn't work

it gives me 'VolumeUpNone' not found (pardon my french) :

System.ArgumentException
HResult=0x80070057
Message=La valeur demandée 'VolumeUpNone' est introuvable.
Source=mscorlib
Arborescence des appels de procédure :
à System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)
à System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
à System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
à WK.Libraries.HotkeyListenerNS.HotkeyListener.Convert(String hotkey)
à WK.Libraries.HotkeyListenerNS.Helpers.HotkeyHandle.WndProc(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
à MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
à System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
à System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
à System.Windows.Application.RunDispatcher(Object ignore)
à System.Windows.Application.RunInternal(Window window)
à System.Windows.Application.Run(Window window)
à System.Windows.Application.Run()
à Volmatic.App.Main()

Here is the bit that I use :
volume_up = new Hotkey(System.Windows.Forms.Keys.VolumeUp);

The error happens only after I press the VolumeUp key.


I just found a workaround!
volume_up = new Hotkey(System.Windows.Forms.Keys.VolumeUp);
volume_up.KeyCode = System.Windows.Forms.Keys.VolumeUp;
Console.WriteLine(volume_up.KeyCode);
Modifying the keycode after creating the variable allows to remove the "None" that seems to be the problem.

edit :
Actually it does not work but gives VolumeUpVolumeUp

Doing this tho :
volume_up = new Hotkey(System.Windows.Forms.Keys.Control, System.Windows.Forms.Keys.F);
volume_up.KeyCode = System.Windows.Forms.Keys.VolumeUp;
Console.WriteLine(volume_up.KeyCode);

gives me "VolumeUp" but then the handling function does not capture anything.

I therefore do not have a valid workaround.

key PageDown

"PageDown" key registration works like "Next" key

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.