0

So I want to make a Global hotkey and have Mouse 4 or 5 auto click my mouse.

The way I'm global binding is a bit interesting.

So on the form, I have a label that will set active control when clicked to listen to show what the keybind is when pressed.

private Keys clickerHotkey;
private void leftClickHotkeyLbl_Click(object sender, EventArgs e)
    {
        this.ActiveControl = leftClickHotkeyLbl; // set active to able to respond to the key down event.
        leftClickHotkeyLbl.Text = "[...]";
    }

private void leftClickHotkeyLbl_KeyDown(object sender, KeyEventArgs e)
    {
        leftClickHotkeyLbl.TabStop = false;
        if (!((e.KeyValue >= 16 && e.KeyValue <= 18) || (e.KeyValue >= 21 && e.KeyValue <= 25) || (e.KeyValue >= 28 && e.KeyValue <= 31) || e.KeyValue == 229 || (e.KeyValue >= 91 && e.KeyValue <= 92))) // this gets rid of non sense keys...
        {
            KeyBindManager.KeysConverter.UnregisterHotKey(this.Handle, (int)clickerHotkey); // unregister previous key.
            clickerHotkey = e.KeyData;
             
            if (clickerHotkey == Keys.XButton1) // doesn't work :(
            {
                Console.WriteLine("Mouse 5 Detected");
            }

            if (clickerHotkey == Keys.Escape) // if the key is escape, return
            {
                UnsetHotkey(clickerHotkey);
                leftClickHotkeyLbl.Text = "[-]";
                this.ActiveControl = null;
                return;
            }

            clickerModifiers = ExtractModifier(clickerModifiers, e);
            SetHotkey(clickerModifiers, clickerHotkey);

            leftClickHotkeyLbl.Text = $"[{KeyBindManager.KeysConverter.Convert(clickerHotkey)}]";
            this.ActiveControl = null; // set to null so its no longer being edited.
        }
    }

Any help is much appreciated! Cheers!

  • https://stackoverflow.com/a/47526143/17034 – Hans Passant Oct 14 '21 at 11:14
  • This isn't what I am looking for, I need to be able to click on the label, it will begin to listen to the very next key or mouse press. Then I press Mouse 4 or 5, I will then save it to the hotkey. – buggy_code312 Oct 14 '21 at 16:16

1 Answers1

0

You are trying to detect a mouse event/mouse button via a keyboard key...

You cant use Keys.Button for a mouse event as it's specific to keyboard keys, chence the name Keys.

if (clickerHotkey == Keys.XButton1) // doesn't work :(
{
    Console.WriteLine("Mouse 5 Detected");
}

If you want to detect a mouse event/mouse button click you can change it to the following:

if (clickerHotkey == MouseButtons.XButton1) // or you can use XButton2
{
    Console.WriteLine("Mouse 5 Detected");
}

This intern specifies that you want to listen for a mouse clicks. Due to your event handler being a Keyboard specific one, you need to add a seperate event handler for mouse clicks eg.

private void mouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.XButton1)
    {
        Console.WriteLine("Mouse 5 Detected");
    }
}
SimpleCoder
  • 474
  • 4
  • 18