Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check state of Option-Key in SwiftUI (macOS)

Tags:

swiftui

I'm looking for a way to check the state of the option-key in SwiftUI on macOS.

I.e. depending on whether the option key is pressed or not I want to perform different actions in the .onTapGesture closure.

like image 522
mic Avatar asked Oct 31 '19 13:10

mic


1 Answers

macOS-only SwiftUI has .modifiers modifier to specify EventModifiers, so your case is covered like in below example:

Rectangle()
    .fill(Color.yellow)
    .frame(width: 100, height: 40)
    .gesture(TapGesture().modifiers(.option).onEnded {
        print("Do anyting on OPTION+CLICK")
    })
    .onTapGesture {
        print("Do anyting on CLICK")
    }
like image 167
Asperi Avatar answered Sep 22 '22 13:09

Asperi