Question

One of my projects is an testing app where a student shouldn't be able to easily look up words while they're typing.

It's relatively easy to turn off automatic spelling checking in a NSTextView via setContinuousSpellCheckingEnabled: and setAutomaticSpellingCorrectionEnabled:.

I just discovered that it's very trivial for students to simply tap with three fingers upon any selected word in any app and up pops a helpful window containing a dictionary, thesaurus and even a Wikipedia entry if the word can be found there.

What Lookup functionality looks like

This is great functionality for 99% of MacOS apps but not appropriate for my testing app.

Now after a few months, Apple has provided me with a (undocumented and subtle) solution that works for 10.8 only and I may eventually provide it in the answers down below, but I need to have a solution that works for 10.7 as well (which is where this functionality came in).

There are three possible plans of attack on this problem but I'm not sure how to approach any of these three:

1)

I need to block this Lookup functionality from happening in this text view.

2)

I've already tried to delete the dictionary preferences (if they exist; if the user never opened Dictionary.app, there are no preferences) and the dictionary cache files (in "~/Library/Cache", but this doesn't seem to improve the situation.

3)

Or is there a way to be able to detect the Trackpad setting that says "Use Lookup when doing a three fingered tap"? It's probably in some com.apple.*.plist somewhere or detectable via "defaults" but I'm not certain where.

EDIT:

Only a bit of time left to hopefully solve this problem and award a bounty. Here is the approach that I was attempting with "defaults":

defaults write com.apple.driver.AppleBluetoothMultitouch.trackpad TrackpadThreeFingerTapGesture -bool false
defaults write com.apple.driver.AppleBluetoothMultitouch.trackpad TrackpadThreeFingerDoubleTapGesture -bool false

But I'm not 100% certain these are the correct gestures/keywords to type in. And even after typing them in (and verifying they were correctly saved via "defaults read com.apple.driver.AppleBluetoothMultitouch.trackpad"), the dictionary LookUp window still appears.

Now here is the only thing that works, but it works only under MacOS 10.8 (which is where these methods were exposed/brought in). Simply override these two methods in your NSTextView subclass:

- (void)quickLookWithEvent:(NSEvent *)event;
- (void)quickLookPreviewItems:(id)sender;
Was it helpful?

Solution

I suspect that the dictionary lookup is implemented in one of two ways, as a Service or via the NSTextInputClient protocol. So, I would override -validRequestorForSendType:returnType: in your text subclass to see if it's invoked for a three-finger tap. Anyway, it sounds like for your app you want to return NO for all services. You should also override writeSelectionToPasteboard:types: to return NO.

If that doesn't do it, try overriding the methods of NSTextInputClient to see if they're being called. In particular, the -attributedString and -attributedSubstringForProposedRange:actualRange: methods would be how the dictionary lookup is obtaining the text. Making both of those return nil should prevent it from working. Mind you, the NSTextInputClient protocol is central to the use of input methods and the press-and-hold access to characters with accents and diacritics, so you may break that. I don't know if you'll be able to distinguish between requests from a normal input method vs. from the dictionary lookup gesture.

If all else fails, you can implement a custom text view rather than using NSTextView. It's not trivial, but you'll have full control over its behavior.

OTHER TIPS

This answer to a similar question fixes the problem without throwing exceptions with the trade off that it use a private API: https://stackoverflow.com/a/20618984/959140

Simply override -(void)quickLookWithEvent:(NSEvent *)event in your subclass and don't do anything with the event.

When the Dictionary screen pops up, go to File (or maybe it was edit) / Preferences and uncheck all the options so that no actual dictionary options are checked, and close the app, and it no longer shows up - for the most part - once in a while while trying to use the mousepad to select a string of text for copy and/or cutting, it shows up, but not constantly like before...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top