7

I have a small problem with .Net 4.0 ToolStripMenuItem caption.
I want it to underscore the Shortcut (access) key letter in the item text.
I used the ampersand sign in the item text field: '&New map', and it looks fine in the editor:
enter image description here

But, when I build the application, the underscores disappear:
enter image description here

Does anyone know why does it happen and how to make the underscored display in the built form?

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
Nidrax
  • 372
  • 3
  • 12

5 Answers5

8

That is the default behaviour of Windows. The accelerator keys are hidden unless you invoke the menu with the keyboard. Press ALT to see the accelerators. Note that you can see this behaviour in other programs, for instance try Notepad.

If you wish to change the behaviour on your own machine, you can configure the system to show accelerator keys at all times from the Ease of Access Center. The setting is found under Make the keyboard easier to use and on my Windows 7 machine looks like this:

enter image description here

Note that it is strongly recommended that you let the user make the choice as to whether or not to hide accelerator keys. In other words, your application is already behaving correctly and, in my view, you should make no changes to its current behaviour.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • As far as I know, this wasn't always "the default behaviour of Windows", though. In fact, never knew this was a system setting. Glad I can enable it now. – Nyerguds Jun 20 '16 at 09:22
  • Also note, a lot of these menus are triggered by _right mouse button_, and not by alt, and pressing alt after right-clicking simply _closes_ that menu, meaning you can never see the shortcuts on these. This always annoyed me in internet browsers. – Nyerguds Jun 20 '16 at 09:27
  • @Nyerguds I think that early versions of Windows always displayed accelerator key underlines, but in Windows 2000 they became optional – David Heffernan Jun 20 '16 at 09:30
  • As far as I remember, XP _did_ show them _on right click menus_ though. Also, on Win10 they don't even seem to show when you press the context menu button on the keyboard, while the use of that key generally does imply that you want to navigate the menus keyboard-only. – Nyerguds Jun 20 '16 at 09:35
7

As mentioned in other answers, this the default behaviour. Accelerators are being shown only after the ALT key is pressed.

However it seems possible to force Windows to display accelerator keys constantly:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni);

private const int SPI_SETKEYBOARDCUES = 4107; //100B
private const int SPIF_SENDWININICHANGE = 2;

[STAThread]
static void Main()
{
    // always show accelerator underlines
    SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, 1, 0);

    Application.Run(new MainForm());
}

Found here.

As I've just verified (after ken2k's suggestion in the comments), this unfortunately affects the whole system. So it needs some tweaking: 1) remember current value of SPI_SETKEYBOARDCUES on startup 2) reset the setting to this value on exit, 3) create a domain exception handler, to be sure that the setting always gets reset back.

Unfortunately this behaves this way even if the last parameter is zero, even though documentation says:

This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message

Simple version is of course just:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni);

private const int SPI_SETKEYBOARDCUES = 4107; //100B
private const int SPIF_SENDWININICHANGE = 2;

[STAThread]
static void Main()
{
    // always show accelerator underlines
    SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, 1, 0);

    Application.Run(new MainForm());

    SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, 0, 0);
}

In this answer you can find a code example on how to achieve this locally only for your application.

Community
  • 1
  • 1
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • 2
    Yes you can do this but it is a bad idea. The user should be allowed to make this choice. The application should not make it for them. Users would be quite rightly upset if they were denied this choice. – David Heffernan Jan 03 '14 at 13:02
  • 2
    @DavidHeffernan That's not the user's choice, but the choice of application's designer. I think it's OK that you want to make your application look the way YOU want. If you want to involve user in determining the looks of the application, then it's a whole another level - implementing themes etc. – BartoszKP Jan 03 '14 at 13:06
  • Whole other level? Not at all. The customisation already exists in the ease of access center. Obviously the application designer can choose to disregard the system settings, but in my view that is a mistake. – David Heffernan Jan 03 '14 at 13:15
  • @BartoszKP "to make your application look the way YOU want" ==> true, but it's not about the application only, this applies the change to the whole system. If it took a window handle as parameter it would be fine, but changing system parameters for a particular application is **not** expected from the application. – ken2k Jan 03 '14 at 13:19
  • @ken2k AFAIK, and as it says in the blog I've linked to, this only changes the behaviour of your application. – BartoszKP Jan 03 '14 at 13:21
  • @DavidHeffernan In general I agree with you. But in some special cases I can imagine that you really want to do it :) – BartoszKP Jan 03 '14 at 13:22
  • @ken2k Sorry, you were right. I've just run it, and indeed it affects other applications. I've updated my answer. – BartoszKP Jan 03 '14 at 13:31
  • Hmm. Remove SPIF_ SENDWININICHANGE. – David Heffernan Jan 03 '14 at 13:37
  • @DavidHeffernan Yeah, I've read the docs also. Unfortunately, the `0` doesn't work (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724947%28v=vs.85%29.aspx) – BartoszKP Jan 03 '14 at 13:42
  • There's a clean way to do this for the calling app only, but I can't give the details right now. – David Heffernan Jan 03 '14 at 13:52
  • @DavidHeffernan Yeah, that would be nice. I didn't have time also to research this branch ;) – BartoszKP Jan 03 '14 at 14:04
  • @BartoszKP I added an answer with the details. – David Heffernan Jan 03 '14 at 14:30
  • I cannot understand why you accepted this answer given that all the code in it is no good. – David Heffernan Jan 03 '14 at 18:43
  • @DavidHeffernan I don't understand it either but perhaps you should comment under the question for OP to be notified. – BartoszKP Jan 04 '14 at 15:07
  • @BartoszKP asker is notified whenever anyone comments, I think – David Heffernan Jan 04 '14 at 15:09
2

Other answers have explained the behaviour that you are observing. I won't repeat any of that.

For the sake of completeness, and to expand on BartoszKP's answer, there is a way to control the hiding of accelerator keys in a way that is local to your application. Specifically that is the WM_UPDATEUISTATE message. Pass UIS_CLEAR and UISF_HIDEACCEL. My answer to this question shows how to do it: Show Hotkeys at All Times.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • As expected, this requires the handle of the target window as parameter. Thanks for the details. – ken2k Jan 03 '14 at 14:48
1

Since Windows Vista (I think), shortcuts hints are not displayed by default. You need to press Alt to display them. There is a global system setting to change that behavior, in the accessibility settings.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
0

Windows by default no longer shows these shortcuts. You can re-enable them by going into the "Ease of access center" in Control Panel and in the "Make the keyboard easier to use" section tick the option "Underline keyboard shortcuts and access keys" although obviously this only affects the computer you're on.

Paul Hunt
  • 3,395
  • 2
  • 24
  • 39