Posts

Post marked as solved
1 Replies
2.1k Views
I'm making a macOS app with SwiftUI. I would like the window to listen for keyboard events, and to determine which key was pressed. The problem I have now is that app plays the 'funk' error sound every time I hit a key, but other than that it works fine. How do I get it to stop playing the sound?This is my code, which I found online:struct KeyEventHandling: NSViewRepresentable { class KeyView: NSView { override var acceptsFirstResponder: Bool { true } override func keyDown(with event: NSEvent) { super.keyDown(with: event) print(">> key \(event.keyCode)") } } func makeNSView(context: Context) -> NSView { let view = KeyView() DispatchQueue.main.async { // wait till next event cycle view.window?.makeFirstResponder(view) } return view } func updateNSView(_ nsView: NSView, context: Context) { } } struct ContentView: View { var body: some View { KeyEventHandling() } }The code prints each keyCode correctly, the only issue is the error sound that it plays. There are similar questions on Stack Overflow, but those are using pure AppKit with view controllers, so how do I implement it here, in SwiftUI?Thanks!
Posted
by qitianshi.
Last updated
.
Post not yet marked as solved
1 Replies
1k Views
Hi! I'm making a macOS app with SwiftUI and I want to be able to detect the x-y coordinates of the user's finger on the trackpad. How can I do this?I'd like it to be able to detect as long as the finger is resting on the trackpad, not just when it's pressed down or dragging. Also, I'd ideally like to be able to get the coordinates of more than one finger. To illustrate what I want, the AudioSwift app uses the trackpad as a MIDI controller (video).Everything I've found online was from 10 years ago back in the days of Obj-C. Can some kind soul provide and updated code that preferably works with SwiftUI? Also, I'm really new to app development in general, so I'd appreciate if you could explain it simply. Thanks!
Posted
by qitianshi.
Last updated
.
Post not yet marked as solved
1 Replies
1.1k Views
Hello. I've noticed that setting the showsIndicators parameter to false in a ScrollView will cause the scroll to stop working on macOS.The following code creates a ScrollView of a list of numbers:ScrollView(showsIndicators: false) { VStack { ForEach(0..<100) { Text("\($0)") } } .frame(width: 200) }With showsIndicators set to false, the view does not scroll. When trying to scroll upwards (when already at the top of the view), it bounces back down to indicate that it is at the top of the view, as expected. When trying to scroll downwards, there is no reponse at all. As a result, the ScrollView is stuck at the top of the view and is unable to scroll.This only happens when showsIndicators is set to false and while developing macOS apps. The exact same code on iOS works normally, and it works for both macOS and iOS when showsIndicators is true.Is this a SwiftUI bug? If not, what did I do wrong?macOS version: 10.15.4Xcode version: 11.4.1
Posted
by qitianshi.
Last updated
.