Keyboard Accelerator with Menu (Alt) Modifier plays an alert sound (WinUI Desktop)
Describe the bug If you add a keyboard accelerator to a control (typically button, appbarbutton etc.) that uses the Menu modifier (the Alt button), when the accelerator is invoked, the required action is processed but we get a spurious windows alert sound played at the same time. This is somewhat disconcerting for users as it makes them think they did something wrong and to be honest it's really annoying ;-) if you make a lot of use of Alt+ key combinations for desktop/keyboard interaction.
Steps to reproduce the bug
Steps to reproduce the behavior:
- Create a new "Blank App, Packagde (WinUI in Desktop)" app
- Modify the Main Window xaml to include a keyboard accelerator:
<Button x:Name="myButton"
Click="myButton_Click">
<Button.KeyboardAccelerators>
<KeyboardAccelerator Key="C"
Modifiers="Menu" />
</Button.KeyboardAccelerators>
Click Me
</Button>
- Run the app
- Activate the keyboard accelerator (Alt+C)
- The button will change text but there will be a windows sound played at the same time
Expected behavior Because there is a valid accelerator I expect there to be no sound unless we add code to play it.
I would be happy with the sound being played if there wasn't an appropriate accelerator.
Screenshots Can add video if necessary, in case it's only my box?
Version Info
NuGet package version:
<ItemGroup>
<PackageReference Include="Microsoft.WinUI" Version="3.0.0-preview4.210210.4" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
This was around in Preview 3 so it's not a regression.
Windows app type:
| UWP | Win32 |
|---|---|
| Yes |
| Windows 10 version | Saw the problem? |
|---|---|
| Insider Build (xxxxx) | |
| October 2020 Update (19042) | Yes |
| May 2020 Update (19041) | |
| November 2019 Update (18363) | |
| May 2019 Update (18362) | |
| October 2018 Update (17763) | |
| April 2018 Update (17134) | |
| Fall Creators Update (16299) | |
| Creators Update (15063) |
| Device form factor | Saw the problem? |
|---|---|
| Desktop | Yes |
| Xbox | |
| Surface Hub | |
| IoT |
Additional context
@Austin-Lamb FYI
I could not reproduce this issue in Winui2
I could not reproduce this issue in Winui2
@StephenLPeters I believe this is likely to be a Desktop only issue, as I don't recall having the problem in UWP apps even though I used the same style?
This is stil an issue in Windows App SDK 1.1.4 (WinUI 3).
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.
This issue is still occurring.
@hiteshkrmsft Could you please link the duplicated issue? I cannot find it. This issue is still reproducible and there should be an open issue for it.
The internal copy of the issue was resolved as duplicate of another internal issue. Reactivating for continued tracking against the other, still-active internal issue.
We are working on supporting AccessKey's in our WinUI 3 Desktop application. Documented here The whole concept of AccessKey's in XAML is based on the combination of the Alt key and one or more letter keys (mnemonics). However, we have the exact same problem with an alert sound always being played when invoking an AccessKey. This is incredibly annoying and practically renders the AccessKey features in XAML useless. Any user will of course think he is doing something wrong. We have tried a number of hacks to get rid of the sound, without success.
Our theory is this: The "Windows window" behind the XAML window is interpreting Alt + key as a menu shortcut. Since there are no menus in our app - this will always end up in an "invalid shortcut" alert sound. There is one exception: Alt + Space will invoke the system menu in the upperleft corner of the window. Is it possible to somehow disable the menu shortcut mechanism? In a typical WinUI 3 app it is not useful.
Just an other observation: If you try pressing Alt + key in the WinUI 3 Gallery demo application, the same alert sound can be heard.
You can remove the sound by intercepting WM_MENUCHAR (with SetWindowSubclass)
You can remove the sound by intercepting WM_MENUCHAR (with SetWindowSubclass)
Can you provide more details? I've managed to use the SetWindowSubclass and can return whatever I want. However, no return value I use seems to actually suppress the ALT key chime.
You can remove the sound by intercepting WM_MENUCHAR (with SetWindowSubclass)
Can you provide more details? I've managed to use the SetWindowSubclass and can return whatever I want. However, no return value I use seems to actually suppress the ALT key chime.
The test I did on my OS (Windows 10 22H2, Windows App SDK 1.7.250401001) :
private IntPtr hWndMain = IntPtr.Zero;
private SUBCLASSPROC SubClassDelegate;
hWndMain = WinRT.Interop.WindowNative.GetWindowHandle(this);
SubClassDelegate = new SUBCLASSPROC(WindowSubClass);
bool bRet = SetWindowSubclass(hWndMain, SubClassDelegate, 0, 0);
private int WindowSubClass(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, uint dwRefData)
{
switch (uMsg)
{
case WM_MENUCHAR:
{
int nResult = (MNC_CLOSE << 16); // HIWORD = MNC_CLOSE
return (nResult);
}
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
with :
public delegate int SUBCLASSPROC(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, uint dwRefData);
[DllImport("Comctl32.dll", SetLastError = true)]
public static extern bool SetWindowSubclass(IntPtr hWnd, SUBCLASSPROC pfnSubclass, uint uIdSubclass, uint dwRefData);
[DllImport("Comctl32.dll", SetLastError = true)]
public static extern int DefSubclassProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
public const int WM_MENUCHAR = 0x0120;
public const int MNC_IGNORE = 0;
public const int MNC_CLOSE = 1;
public const int MNC_EXECUTE = 2;
public const int MNC_SELECT = 3;
Heck yeah! I had glossed over the "return the value in the highword" and was just returning values directly. My app beeps much, much less often.
And for anyone else doing this: I put this into my WinUI3 "App.xaml.cs" file. It's on Github at https://github.com/pedasmith/BluetoothDeviceController/blob/main/SmallProjects/TestNmeaGpsParserWinUI/App.xaml.cs