Question C# - Button click - Select hotkey

Voqxdb

New member
Joined
May 9, 2022
Messages
2
Programming Experience
1-3
What i want. If you click the button (See picture) you can the press keys on your keyboard for example CTRL + D in The white text change to the hotkey you have set. This will be registerd in an enum list Like i have now:
private enum Listry
{
LeftMouse = 1,
RightMouse,
X1Mouse = 5,
X2Button,
Shift = 160,
Ctrl = 162,
Alt = 164,
Capslock = 20,
Numpad0 = 96,
Numlock = 144
}

Does any know how to do this? Thanks in advance
 

Attachments

  • unknown.png
    unknown.png
    6.8 KB · Views: 12
Last edited:
What button? Your UI in the screenshot is not following the Windows design guidelines and so nothing there looks like a Windows button. Are you talking about the blue rounded rectangle that has "Alt" in it?
 
What button? Your UI in the screenshot is not following the Windows design guidelines and so nothing there looks like a Windows button. Are you talking about the blue rounded rectangle that has "Alt" in it?
Sorry my bad. The blue Rounded rectangle is indeed the button just like an normal windows button but with an theme added. Sorry for not making that clear.
 
On a high level, you'll likely want to wait for that button to be pressed. When it is pressed, then you set flag to tell your form that you are waiting for a keypress. Once your form gets that keypress, then you unset the flag. How you get that keypress depends on exactly what your scenario looks like.

I highly recommend reading Jessica Fosler's old blog post:

Probably more than you want to know about keyboarding in Windows Forms….


Only you will truly know which particular scenario is appropriate for you.

Below is a dead simple WinForms program that shows what key is currently being pressed in the caption of the window. It uses ProcessDialogKey() because it fits my particular needs. If I had more stuff on the form, I may have to change what method I use to detect the keypresses.

C#:
using System;
using System.Windows.Forms;

namespace WinForms
{
    class MainForm : Form
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            Text = $"{DateTime.Now.TimeOfDay} {keyData}";
            return base.ProcessDialogKey(keyData);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}
 
Back
Top Bottom