Instance Property インスタンスプロパティ

keyboardShortcut

The keyboard shortcut that buttons in this environment will be triggered with.

Declaration 宣言

var keyboardShortcut: KeyboardShortcut? { get }

Discussion 議論

This is particularly useful in button styles when a button’s appearance depends on the shortcut associated with it. On macOS, for example, when a button is bound to the Return key, it is typically drawn with a special emphasis. This happens automatically when using the built-in button styles, and can be implemented manually in custom styles using this environment key:


private struct MyButtonStyle: ButtonStyle {
    @Environment(\.keyboardShortcut)
    private var shortcut: KeyboardShortcut?


    func makeBody(configuration: Configuration) -> some View {
        let labelFont = Font.body
            .weight(shortcut == .defaultAction ? .bold : .regular)
        configuration.label
            .font(labelFont)
    }
}

If no keyboard shortcut has been applied to the view or its ancestor, then the environment value will be nil.

See Also 参照

View Attributes