当前位置:   article > 正文

macos 菜单栏 快捷键_SwiftUI键盘快捷键和macOS菜单栏。

swiftui macos 绑定 快捷键

macos 菜单栏 快捷键

With SwiftUI 2, it’s incredibly easy to add keyboard shortcuts to actions and to add buttons to your app’s menu bar.

使用SwiftUI 2,在操作中添加键盘快捷键以及在应用程序的菜单栏中添加按钮非常容易。

键盘快捷键 (Keyboard Shortcut)

You can add keyboard shortcuts to actions simply by adding .keyboardShortcut(_:) to the button:

您只需在按钮上添加.keyboardShortcut(_:) ,即可为操作添加键盘快捷键:

  1. Button(action: {}) {
  2. Text("Button")
  3. }.keyboardShortcut("B")

You can set nearly every character as KeyEquivalent . But note, that you can’t toggle the button by simply pressing “B”. When using letters like “V”, you need to press “⌘⇧V”, when using numbers like “6”, you need to press “⌘6”. You can also specify your own modifier keys, by passing them to the modifier parameter.

您几乎可以将每个字符设置为KeyEquivalent 。 但是请注意,您不能仅通过按“ B”来切换按钮。 使用“ V”等字母时,需要按“⌘⇧V”;使用“ 6”等数字时,则需要按“⌘6”。 您还可以通过将自己的修改键传递给modifier参数来指定它们。

.keyboardShortcut("B", modifiers: .control)

You can define a single one, or even multiple.

您可以定义一个,甚至多个。

.keyboardShortcut("B", modifiers: [.command, .shift, .control])

A list of all event modifiers is on Apple’s developer website.

所有事件修饰符的列表在Apple的开发人员网站上

You cannot only set letters and numbers as shortcuts, but also other buttons like space or page up, accessing the KeyEquivalent properties.

您不仅可以将字母和数字设置为快捷方式,还可以将其他按钮(如空格或向上翻页)设置为快捷键,以访问KeyEquivalent属性。

.keyboardShortcut(.downArrow)

A list of all equivalents is on the Apple Developer website. You can also toggle buttons by pressing the escape button or the enter button with:

所有等效项列表在Apple Developer网站上 。 您还可以通过按下退出按钮或输入按钮来切换按钮,方法是:

.keyboardShortcut(.cancelAction)

or

要么

.keyboardShortcut(.defaultAction)

.commands() (.commands())

With SwiftUI 2 you can also add buttons to your app’s menu bar. You can either add a new menu to the bar or buttons to existing menus. The commands are passed to directly to the scene in the app protocol.

使用SwiftUI 2,您还可以在应用程序的菜单栏中添加按钮。 您可以将新菜单添加到栏中,也可以将按钮添加到现有菜单。 这些命令将直接传递到应用协议中的场景。

  1. @main
  2. struct MyApp: App {
  3. var body: some Scene {
  4. WindowGroup {
  5. ContentView()
  6. }.commands {
  7. // Your commands here
  8. }
  9. }
  10. }

There are two options, either passing the commands directly to the .commands() modifier or putting them in a seperate struct.

有两个选项,可以将命令直接传递到.commands()修饰符,也可以将它们放在单独的结构中。

  1. struct AppCommands: Commands {
  2. @CommandsBuilder var body: some Commands {
  3. // Your commands here
  4. }
  5. }

Then you pass the commands to the scene with .commands { AppCommands() }.

然后,使用.commands { AppCommands() }将命令传递到场景。

命令菜单 (CommandMenu)

Command menu creates a new menu inside the menu bar.

命令菜单在菜单栏中创建一个新菜单。

  1. struct AppCommands: Commands {
  2. func action() {}
  3. func anotherAction() {}
  4. @CommandsBuilder var body: some Commands {
  5. CommandMenu("Menu") {
  6. Button(action: {
  7. action()
  8. }) {
  9. Text("Action")
  10. }
  11. Button(action: {
  12. anotherAction()
  13. }) {
  14. Text("Another action")
  15. }
  16. }
  17. }
  18. }

This creates a menu named “Menu” with two buttons, that perform an action. You can also add a keyboard shortcut to the button, that is displayed next to the label. Divider() can be used to group actions together. And of course you can also add a Picker() to the command menu. But you need to pass the @State as a @Binding to the AppCommands structure. When you want to add submenus, add a Menu() .

这将创建一个名为“ Menu”的菜单,其中包含两个按钮,它们执行一个操作。 您还可以在按钮旁边添加键盘快捷键,该快捷键显示在标签旁边。 Divider()可用于将动作分组在一起。 当然,您也可以在命令菜单中添加Picker() 。 但是您需要将@State作为@Binding传递给AppCommands结构。 当您想添加子菜单时,添加一个Menu()

指挥组 (CommandGroup)

Command groups can be used, to add buttons to existing menus. You can either add them before existing buttons or after existing buttons, or you replace existing buttons.

可以使用命令组将按钮添加到现有菜单。 您可以将它们添加到现有按钮之前或之后,也可以替换现有按钮。

  1. CommandGroup(before: CommandGroupPlacement) {
  2. // Put the buttons here
  3. }
  4. CommandGroup(after: CommandGroupPlacement) {
  5. // Put the buttons here
  6. }
  7. CommandGroup(replacing: CommandGroupPlacement) {
  8. // Put the buttons here
  9. }

Replace CommandGroupPlacement with the buttons you want. Some examples are .appInfo , .pasteboard or .undoRedo . A full list is on Apple’s developer website.

用所需的按钮替换CommandGroupPlacement 。 一些示例是.appInfo.pasteboard.undoRedo 。 完整列表位于Apple的开发人员网站上

更多 (More)

There is also a set of predefined commands.

还有一组预定义的命令。

  1. SidebarCommands()
  2. TextEditingCommands()
  3. TextFormattingCommands()
  4. ToolbarCommands()

翻译自: https://itnext.io/swiftui-keyboard-shortcuts-and-menu-bar-be22abbb3791

macos 菜单栏 快捷键

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/777576
推荐阅读
相关标签
  

闽ICP备14008679号