Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable LookUp (Dictionary, Thesaurus, Wikipedia) functionality within a NSTextView?

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;
like image 331
Michael Dautermann Avatar asked Oct 11 '12 00:10

Michael Dautermann


People also ask

How do you use Dictionary in Safari?

If you're in Safari (and many other Mac OS X apps and browsers), hit control-command-D keystroke together while you hover over a word, a dictionary will pop up with that words definition!

How do you define a word on a Mac?

In most Mac applications—including Safari, Mail, Pages, TextEdit, Twitter, you name it—just position your cursor over the word you want to define and press Command-Control-D. A pop-up window appears containing the definition, synonyms, and any relevant Wikipedia entry.


1 Answers

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.

like image 151
Ken Thomases Avatar answered Sep 21 '22 06:09

Ken Thomases