0

I have a SwiftUI view I wrap in a NSHostingController in a mac app. This view consists of a vertical ScrollView with LazyVStack in it with various content (divided into Section). This content is long so the view is always scrollable.

I'm trying to implement better keyboard navigation, I can navigate buttons using Tab key, but I also want to support basic scrolling using up and down arrow keys.

This is something that works out-of-the-box in AppKit when e.g. using NSTextView in NSScrollView.

How do I enable scrolling up and down using arrow keys for SwiftUI.ScrollView on macOS?

I'm to looking for scrolling to an identifiable element but rather to scroll by x points up and down. Like the NSScrollView works.

Ideally, including modifier keys ALT+Up to page up and CMD+Up to scroll to the top.

struct ContentView: View {

    var body: some View {
        ScrollView(.vertical) {
            LazyVStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)

                Text("Hello, world!")

                Section("Lorem Ipsum") {
                    Text(Strings.longText)
                }
            }
            .padding()
        }
    }
}
Tom Kraina
  • 3,569
  • 1
  • 38
  • 58
  • Does this answer your question? [How to scroll through items in scroll view using keyboard arrows in SwiftUI?](https://stackoverflow.com/questions/74403270/how-to-scroll-through-items-in-scroll-view-using-keyboard-arrows-in-swiftui) or this: https://stackoverflow.com/questions/73252399/swiftui-keyboard-navigation-in-lists-on-macos/73256905#73256905 – workingdog support Ukraine Jan 11 '23 at 09:50

0 Answers0