Hotkey Changing Button [Source Code]

Posts 1–5 of 5 · Page 1 of 1
Hotkey Changing Button [Source Code]
I coded a Button that user can set hotkeys with it as he wishes.
How to use:

-Drag and Drop the HotkeyButton to form.
-Set Default Hotkey.



VirusScan:
Code:
It's false positive.
https://www.metadefender.com/#!/results/file/343bb43b683c4db4a6e203039b06aa39/extracted/files
https://www.virustotal.com/tr/file/94480dd5876c954df6e8d176c646260d612d273def78923c415e6aca3143bef0/analysis/1468541389/
HotkeyDesigner_mpgh.net.rar25 KB · 19 downloads 2/55 malicious
So, just a button?
Could you explain what happens in the code behind?
Quote Originally Posted by _NightWare View Post
So, just a button?
Could you explain what happens in the code behind?
Each button has a property called "Hotkey" as Forms.Keys . You can use this property like this
Code:
GetaSyncKeyState(HotkeyButton1.Hotkey)
If you Hover the button, it's text changes to something like "Click to Change" (i am not in my computer now)
If you Click the button, it's text changes to "Hit a Key"
If you hit a key that key becomes new HotkeyButton1.Hotkey.
Look into RegisterHotKey function, it's a lot more useful.
GetAsyncKeyState is a bad idea as it may ignore two combined keys, RegisterHotkey too as you don't need to hook system keys.
I'd prefer this method, maybe a bit risky, by overriding the ProcessCmdKey method. It should works just with forms.

Code:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.A)
            {

                myButton.PerformClick();

                return true;

            }

            return base.ProcessCmdKey(ref msg, keyData); // return the base method to let process shortcuts or hotkeys you are not handling yourself
        }
if you need to handle more hotkeys, I suggest to do a switch case to make it more readable. You could make a wrapper for this if you are too lazy to handle the hotkeys yourself. Something like this should give you an idea:

Code:
using System.Windows.Forms;

class hotkeyButton : Button
{

    private Keys _hotkey;

    public Keys Hotkey
    {
        get { return _hotkey; }

        set 
        {
            if(_hotkey != value)
            {
               _hotkey = value;
            }
        }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == _hotkey)
        {

            this.PerformClick();

            return true;

        }

        return base.ProcessCmdKey(ref msg, keyData);
     }
}
build, go to the toolbox, put an hotkeyButton in your form, explore its properties, find the Hotkey property and select your favourite hotkey.
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Talk with us