Question Change keybind hotkeys

ceobros

Member
Joined
Jul 8, 2022
Messages
10
Programming Experience
Beginner
Hi, I am using the HotkeyListener Nuget package and I am trying to update a hotkey.

I have a label that displays what hotkey is being used which is F7, I added a click function to change the text.

C#:
        private void OnrecordPOSClick(object sender, EventArgs e) =>
            recordPOS.Text = "Change";

So now the label displays "Change" and now I gave a keypress function to the form.

C#:
        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));
            }
        }



here is my form1 load


C#:
        private void Form1_Load(object sender, EventArgs e)
        {
            var hkl = new HotkeyListener();

            // Define a new hotkey using the Hotkey class.
            // Parameters are: [modifiers], [keys].
            Hotkey hotkey1 = new Hotkey(Keys.F7);
            Hotkey hotkey2 = new Hotkey(Keys.F8);
            Hotkey hotkey3 = new Hotkey(Keys.F9);

            hkl.Add(hotkey1);
            hkl.Add(hotkey2);
            hkl.Add(hotkey3);
            hkl.HotkeyPressed += Hkl_HotkeyPressed;
        }

I also added HotKey1 as public so I can use it in the "Form1_KeyDown" event

C#:
public Hotkey hotkey1 = new Hotkey(Keys.F7);


The problem I am having is that the text in the label doesn't change to the key I pressed and the HotKey does not change either. Any help would be great.
 
You seem to have left out quite a bit of relevant information but I think the crux of the issue is that you create one HotkeyListener on the Load event of the form but you don't a reference to it. You then create a completely new HotkeyListener on the KeyDown event of the form. If your aim is to change something then I would expect that you need to change what you already have, not leave what you have as it is and create something new as well.

Also note that the form will not raise its KeyDown event if a child control has focus and KeyPreview is false.
 

Latest posts

Back
Top Bottom