C# shortcut key underscore not displayed in built application

隐身守侯 提交于 2019-11-29 08:02:29
BartoszKP

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.

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:

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.

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.

David Heffernan

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!