Check state of Option-Key in SwiftUI (macOS)

北战南征 提交于 2021-02-10 12:43:01

问题


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.


回答1:


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")
    }


来源:https://stackoverflow.com/questions/58643925/check-state-of-option-key-in-swiftui-macos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!