Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to bind a keyboard button (preferably "ESC") to stop my software from running, putting it in a idle mode. But the thing is, it only works with actual buttons, is there anyway to bypass this so it works when pressing labels aswell?



C#
private void script1_Click(object sender, EventArgs e)
{

            if (e.Control && e.KeyCode.ToString() == "ESC")
            {
                MessageBox.Show("This does not work");
            }
            
    launchLabel.Text = ("Script Started");

   WindowMover();

   AutoItX3 autoit = new AutoItX3();

    autoit.MouseClick("left", 468, 286, 1, 8);

    return;

}


Ive read somewhere that it is possible its just that the Visual Studio GUi doesnt provide it, but you can do it with code somehow, is this true?

What I have tried:

I've tried using a button and a label and it seems like visual-studio wont allow it.
Posted
Updated 25-Apr-16 6:43am

1 solution

Not with labels, no - they don't accept keyboard input so there isn;t a "quick key" setting for them although you can "attach" a quick key to the next control via an '&' in the label text: "&Hello" would make the follow control active on ALT+H for example. That won't work for ESC though.
It can be done - but exactly how depends on the environment you are working in. Since you specify C#, we can exclude websites (because it would have to be a javascript solution for that, and it would likely be browser dependant).
For a WinForms (and probably WPF, though I haven't tested it) you would override the Form ProcessCMdKey:
C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if (msg.WParam == (IntPtr) 0x1b)
        Console.WriteLine("Esc");
But I'd use a const value instead of the magic number.

"That looks scary, see I've never seen that code before which makes me not understand any of it, Im sure it works but could you go through it quicky? "


It's not that scary really - all you are doing is overriding a Form procedure so that you can "get at" the message internals before the form does (because it will strip it out and direct it to textboxes and so forth).
The problem is that you're trying to do something that doesn't fit into the "normal" way of things, so you have to go "outside the box" to a certain extent in order to get it to work!
Copy the code below and paste it into your app, and then run it in the debugger. Look at the Output Pane and type something - nothing will happen. But press ESC and you should get a message. Instead of a message in your "real" app, you can do what you need to.
C#
private const int WM_KEYPRESS= 0x100;
private IntPtr ESCAPE = (IntPtr)0x1b;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if (msg.Msg == WM_KEYPRESS && msg.WParam == ESCAPE)
        {
        Console.WriteLine("Escape pressed!");
        return true;
        }
    return base.ProcessCmdKey(ref msg, keyData);
    }
All it does is look at the messages windows is passing around, and intercept one specific message ("A key was pressed") and a specific key: ESC
It returns true to let the system know it dealt with it and it doesn't need to pass it any further.
 
Share this answer
 
v2
Comments
BladeLogan 25-Apr-16 12:46pm    
Wow.. That looks scary, see I've never seen that code before which makes me not understand any of it, Im sure it works but could you go through it quicky?

Also, I am developing this in a WinForm application, The general idea was that I could put some code into the label which allows me or the user to use like ALT + (any key) or just pressing something like (F1) to stop the program, not close it down just stop what its doing which i could add once I know how to do the shortcut key thing.
OriginalGriff 25-Apr-16 14:11pm    
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900