views:

39

answers:

2

It's easy to set up an alt hotkey for a winform button by adding an & in front of the hotkey character in the text property of the button.

Now, I need to create a control with around 50 buttons (don't ask), and the desire is to have a hot key for each of them, utilizing ctrl hotkeys as well as alt hotkeys. Also, it is desired to have the ctrl hotkeys mimic the alt hotkey behavior, in that when you depress the ctrl button the underscores will appear on the buttons to indicate the hotkey.

I could trap key events and try to mimic the behavior, but is there a more straightforward way to do this? Probably not an issue, but the buttons also need to be generated programatically from definitions stored in a db.

EDIT: Explaining this issue a little further, the problem is not only trapping the command key, but actually being able to trap the control key, and then being able to alter the display in the buttons to show the user what the control hot key is for that button.

Below is a little blurb for trapping the command key and the control key at the control level (though it looks like a couple more tricks are necessary to get OnKeyDown to fire):

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Control)
        {
            // show the hot key on the buttons with cntrl hot keys
        }
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Control | Keys.C:
                // execute the ctrl c button action
                btnControl_Click(this, null);
                return true;
            default:
                break;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

At the control level, is it possible to alter the button display to mimic the hotkey behavior to show the underscore? It doesn't look like you can put formatting into the Text property of the button.

The control level approach is also more problematic since the buttons are dynamically generated at runtime. What would be ideal is to be able to assign an alt or ctrl hotkey to a button. I suppose I could extend the button control to add control hotkey behavior. Seems like there should be a more straight forward approach.

+1  A: 

There is not a method as easy as using '&', but this article does a pretty good job of explaining how to do it.

msergeant
Thanks msergeant. The larger issue is actually being able to mimic the overall hotkey behavior for ctrl that you get with alt, see edit above.
Dave Clemmer
+1  A: 

Check out this article.

Adel Hazzah
Thanks Adel. The larger issue is actually being able to mimic the overall hotkey behavior for ctrl that you get with alt, see edit above.
Dave Clemmer