How can I override the 3-finger tap behavior in a NSTextView?

前端 未结 2 821
故里飘歌
故里飘歌 2020-12-16 18:04

On Mac OS X, doing a 3-finger tap on a word pops up a window with a definition of the word.

\"Image

相关标签:
2条回答
  • 2020-12-16 18:23

    Reacting on a triple tap in a NSTextView can most easily be done by overriding quickLookWithEvent:.

    -(void)quickLookWithEvent:(NSEvent *)event
    {
        NSLog(@"Look at me! %@", event);
    }
    

    It also taught me that you can triple tap anything to invoke Quick Look on it.

    0 讨论(0)
  • 2020-12-16 18:28

    Subclass NSTextView and override mouse down event (this is where the view usually handle the click/tap events):

    -(void)mouseDown:(NSEvent*)event
    {
      if (event.clickCount == 3)
      {
        //Do your thing
      }
    }
    

    Hope this helps.

    If the triple click does not work for you (I am not currently in from of my mac to check), you could try something else. I know it works in iOS, I don't know about the trackpad gestures.

    You could try adding a UITapGestureRecognizer to your view:

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGesture.numberOfTouchesRequired = 3;
    
    //....
    
    -(void)viewTapped:(UITapGestureRecognizer*)tap
    {
      if(tap.state == UIGestureRecognizerStateRecognized)
      {
        //you detected a three finger tap, do work
      } 
    }
    

    LATER EDIT:

    I found this article in the apple documentation. Based on a sample from this article, here is some code which should be useful (from listing 8-5):

    - (void)touchesBeganWithEvent:(NSEvent *)event {
        if (!self.isEnabled) return;
    
        NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseTouching inView:self.view];
    
        if (touches.count == 3) 
        {
           //Three finger touch detected
        }
    
        [super touchesBeganWithEvent:event];
    }
    
    0 讨论(0)
提交回复
热议问题