microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

How to open MenuBar via alt key? #6978

Open akash07k opened 2 years ago

akash07k commented 2 years ago

So I'm developing a WinUI3 app and Implementing a MenuBar inside it. I want to open the menu when user presses the "ALT" key. However, I'm unable to do so. There's no explicit way to accomplish this and by default it doesn't open via alt either. Many other apps already do this. For example, modern Notepad. And I think that it is developed in WinUI3. My code is like this:

        <MenuBar>
            <MenuBarItem Title="File">
                <MenuBarItem.KeyboardAccelerators>
                    <KeyboardAccelerator Key="Menu" />
                </MenuBarItem.KeyboardAccelerators>
                <MenuFlyoutItem Name="hello">New...</MenuFlyoutItem>
                <MenuFlyoutItem>Save...</MenuFlyoutItem>
                <MenuFlyoutItem>Exit...</MenuFlyoutItem>
            </MenuBarItem>

                <MenuBarItem Title="Edit"></MenuBarItem>
        </MenuBar>

Please help

BreeceW commented 2 years ago

Use the AccessKey property, like so:

<MenuBar>
    <MenuBarItem Title="File" AccessKey="F">
        <MenuFlyoutItem AccessKey="N" Text="New" />
        <MenuFlyoutItem AccessKey="S" Text="Save" />
        <MenuFlyoutItem AccessKey="X" Text="Exit" />
    </MenuBarItem>

    <MenuBarItem Title="Edit" AccessKey="E" />
</MenuBar
akash07k commented 2 years ago

@BreeceW Thanks, but it will allow the user to open the menu with the keys such as "Alt+F" however, I want that as soon as as user presses alt, the menu bar should open. Like notepad does this.

akash07k commented 2 years ago

Anyone please?

akash07k commented 2 years ago

Update?

akash07k commented 1 year ago

Can someone please provide any insights to fix this issue?

akash07k commented 1 year ago

@castorix @ranjeshj Can you please help?

JJBrychell commented 1 year ago

I am not sure I am following here. There is no provision for opening a menu on the pressing of "alt'. Alt is the key that transitions the application so that it expects access keys to follow (vs standard text characters). You then need to press a character key to get an action to occur.

In the case of the current releases of Notepad, when you press and release the alt button, keytips appear for the available menu items.

image

Pressing one of the defined access keys (F, E, V or S) will then open the corresponding menu (or the settings page in the case of S). This is the behavior you would get if you implemented BreeceW's suggestion above and is the designed behavior.

You can't use alt by itself as an accelerator because it isn't a character; it is a modifier. And if you could, you would end up disabling all the other access keys defined on the screen since they would require alt to be pressed BEFORE the access key itself is entered.

If you add the access keys suggested by BreeceW, then you will get the common menu bar behavior.

github-actions[bot] commented 1 year ago

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.

akash07k commented 1 year ago

Not stale

mdtauk commented 1 year ago

This may be a focusing issue, Pressing Alt is intermittent in Paint.

xiaopoyang-msft commented 10 months ago

Alt key can’t be used as an accelerator. The behavior you got is expected behavior.

Many other apps already do this. For example, modern Notepad.

Notepad doesn't do this. What modern Notepad does is the same as the AccessKey does.

akash07k commented 10 months ago

No, I respectfully disagree 😊. Notepad doesn't do same as the AccessKey thingy. as soon as we press Alt in Notepad, it opens/invokes the Menu. I double checked it just now.

Alt key can’t be used as an accelerator. The behavior you got is expected behavior.

Many other apps already do this. For example, modern Notepad.

Notepad doesn't do this. What modern Notepad does is the same as the AccessKey does.

xiaopoyang-msft commented 9 months ago

I also double checked it on my Windows 11 just now. Open the notepad app and press the Alt key. The notepad will show access key for menu items just like @ JJBrychell’s image. This is what I exactly get. Alt key will not open the MenuFlyout. If the MenuFlyout is opened already, it shows the access key for each item. This is the suggested solution from @BreeceW. Are you using the system built-in Notepad app?

In the case of the current releases of Notepad, when you press and release the alt button, keytips appear for the available menu items.

image

Pressing one of the defined access keys (F, E, V or S) will then open the corresponding menu (or the settings page in the case of S). This is the behavior you would get if you implemented BreeceW's suggestion above and is the designed behavior.

You can't use alt by itself as an accelerator because it isn't a character; it is a modifier. And if you could, you would end up disabling all the other access keys defined on the screen since they would require alt to be pressed BEFORE the access key itself is entered.

If you add the access keys suggested by BreeceW, then you will get the common menu bar behavior.