UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Keyboard Shortcut With Multiple Keys

Forums > SwiftUI

Hi Hopefully there is a simple answer to this.

I can detect a keystroke of Command + 1 by using... > .keyboardShortcut("1", modifiers: .command)

How do I detect a keystroke of Command + 1 + 2??

Thanks

2      

UPDATE: I see my answer is totally useless. You can combine modifiers. But you want to combine keystrokes. Will leave this here. Just in case.

Here's a nice article:

See> SwiftUI Keyboard Shortcuts

Looks like you pass in an array.

struct ContentView: View {
    var body: some View {
        Button("Print message") {
            print("Hello World!")
        }.keyboardShortcut("p", modifiers: [.command, .shift])
    }
}

2      

How do I detect a keystroke of Command + 1 + 2??

Basically, you don't; that's two keystrokes. So you have to detect ⌘+1 followed by ⌘+2. You can combine modifiers, as @Obelix has shown, but you can't combine the actual keys.

2      

@rooster:

Have been thinking about this since my first response. I am not savvy with Combine framework. But wondered if the .throttle() modifier could help in a situation like this?

Throttle

Throttle acts similarly to debounce, in that it collects multiple results over time and sends out a single result – but it does so with fixed time windows.

If throttle collect multiple results (key stroke combinations) over a fixed time window (one right after the other)... then collects it and sends out a single result....

Seems like this might work? Though, I am not sure how someone might wire this up.

2      

My gut says "no", but... maybe? I almost certain it can't be done in vanilla SwiftUI; you would probably have to drop into UIKit to handle it. Unfortunately, keyboard interaction is one of SwiftUI's rough spots.

2      

Hi Thanks for the responses, glad it wasnt an easy fix that I was being dumb about. Basically I have multiple users each with a third party keypad triggering the app. The idea was to receive two keystrokes one to identify the user and one to show the action. These would need to be as one message in case two users tap at the same time.

I may go down the route of using a different modifier per user but this makes things clunky and limits the number of users.

Thanks anyway

2      

Can you explain your use case with a bit more detail?

The Cmd-1, Cmd-2 combination would be used where exactly? On a single field? or globally on an entire View?

Are the third party keypads connected to the same application on the same Mac, or iPad?

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.