Search code examples
swiftmacoscocoagesture

How to disable three finger tap gesture through Terminal or Swift?


I am trying to disable the use of three finger taps to show a word definition in my macOS application. I am using Xcode and Swift to load a web view and a text view (along with buttons). I want the user to still be able to highlight text in the view but not use the three finger tap. I was thinking I could change the Apple defaults (of the three finger tap being enabled) by running a Terminal command or if it would be easier to run a Swift command in Xcode. I am looking for the Terminal command I could use or the Swift code to disable it (through a line of code in my Xcode project).

Thanks in advance!


Solution

  • You should first ask yourself why you want to do this. If and only if there is a good reason why you definitely don't want users to be able to look up definitions of words in your app (or a particular text view), read on.

    Changing settings with Terminal commands is a bad idea because this gesture is a system setting, not one specific to your app. It would not be acceptable to mess with user settings that apply to anything other than your app.

    The proper way to do this would be within the context of an NSTextView. Assuming you've already removed the lookup entry from the NSMenu associated with the text view (which can be accomplished by overriding textView(:menu:for:at:)), you may simply override quickLook(with:) inside a subclass of NSTextView and not call super in order to prevent the 3-finger tap from working.

    For example, you could create a custom NSTextView as such:

    class MyTextView: NSTextView {
    
        override func quickLook(with event: NSEvent) {
            print("preventing quick look")
        }
    
    }
    

    Note that if you're dealing with NSTextFields instead, you can swap out the field editor (which is an NSTextView). This is explained here.