6

I am working on an application that has a Menu on top of it. I want to use a different method for shortcut keys (being this snippet): this is for shortcut key: CTRL + N, 1

bool prefixSeen = false;

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (prefixSeen)
    {
        switch (keyData)
        {
            case (Keys.Control | Keys.D1):
                MessageBox.Show("New file");
                prefixSeen = false;
                break;
        }
    }
    switch (keyData)
    {
        case (Keys.Control | Keys.n):
            prefixSeen = true;
        break;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

Code taken from here.

Here is my menu:

enter image description here

And I want in menu items to be displayed (aligned on the right) the shortcut key (that should just be interpreted as a string I think). How can I achieve this effect?

Thanks in advance, and a Happy New Year to every one.

Edit: the built-in method for Visual Studio is:

enter image description here

4
  • 1
    I want to use a different method for shortcut keys -- this is built in (see Mark Hall's answer). Why use a different method? Commented Dec 31, 2012 at 18:00
  • @JonB, look at my edit (at my snippet) to see why I want to use custom shortcut keys listener. Commented Dec 31, 2012 at 18:16
  • You keep violating the attribution rule, even after it was explicitly pointed out to you. stackoverflow.com/questions/13459248/visual-studio-shortcutkeys Commented Dec 31, 2012 at 18:26
  • @HansPassant, that is my own question. Commented Dec 31, 2012 at 18:30

1 Answer 1

8

Use the MenuItem.ShortCut and MenuItem.ShowShortCut Properties.


If you want to create your own custom shortcuts these properties will not work for you, since they depend on a predetermined enumeration of ShortCut Keys. In that case I would suggest that you add it to the Text of your Menu, there is no automatic way of doing it.


Since it was pointed out that you are using ToolStripMenuItems you should be able to independantly set the ShortCutKeyDisplayString to what every you wish. You will still need to handle the actual Shortcut yourself.

Sign up to request clarification or add additional context in comments.

3 Comments

But how can I customize the Menu control to achieve that?
@Victor Are you using MenuItems or toolStripMenuItems?
I am using toolStripMenuItems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.