如何在swiftUI上绑定快捷键

2025-03-10

使用.keyboardShortcut()即可。比如Button{}.keyboardShortcut("n", modifiers: .command)代表这个button的快捷方式是cmd+n

完整代码

        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                Button {
                    chatViewModel.createNewSession()
                    userFinishedInput = false
                    message = ""
                } label : {
                    Label("新会话", systemImage: "plus.circle")
                        .font(.callout)
                }
                .keyboardShortcut("n", modifiers: .command)
            }
        }