Get NSTextView to trigger selector on key combination

Discussion in 'Mac Programming' started by Chirone, Mar 15, 2011.

  1. Chirone macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    I have a NSTextView and I want something to happen when a button is pushed. That's a very trivial task to do...
    I also want it so when someone hits shift+return the selector the button triggers get used.
    how would i do that?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Set the key equivalent of your button to shift + return.
     
  3. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #3
    i imagine if the textview could actually give up it's focus that would work

    but shift+return just makes new lines in the nstextview
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    If you want to handle it directly within the NSTextView, you can override insertNewline: in an NSTextView subclass:

    Code:
    - ([color=#aa0d91]void[/color])insertNewline:([color=#aa0d91]id[/color])sender
    {
        [color=#aa0d91]if[/color] ([[[[color=#aa0d91]self[/color] [color=#2e0d6e]window[/color]] [color=#2e0d6e]currentEvent[/color]] [color=#2e0d6e]modifierFlags[/color]] & [color=#2e0d6e]NSShiftKeyMask[/color]) {
            [color=#007400]// do something[/color]
        } [color=#aa0d91]else[/color] {
            [[color=#aa0d91]super[/color] [color=#2e0d6e]insertNewline[/color]:sender];
        }
    }
     
  5. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #5
    works perfectly!
    thanks!

    the insertNewline: function doesn't appear to be in the documentation...
     
  6. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #6
    NSTextView is a very complex and elaborate class. If you need to implement a behavior, you can look for a vector (method to override) in its superclasses and protocols (when the docs say an object conforms to a protocol, you can expect that it at least implements the required methods for that protocol). In this case, the method in question is in NSResponder.
     

Share This Page