keyboard events

  • Hi,

    I have a very basic cocoa application that has an NSTextField, a couple of buttons and a scroll view.
    The user enters some word in the textfield, then clicks one of the buttons and the application writes
    something to the scroll view. The other button just cleans the scroll view.

    The application works fine, but now I want to add some code so after entering the word the user can
    hit <enter> and then the results get generated in the scroll view (same as if he/she press the "execute"
    button).

    I have been reading a little bit about keyboard events and I am kind of confuse. It seems that I have
    to have an NSView class since its the one that implements NSResponder, which is the one that allows handling
    keyboard events.

    I have two classes:

    @interface AppController : NSObject

    and

    @interface Speller : NSObject

    As you can see both are NSObjects.

    What is the best way to add keyboard events to my application? Can you point me to some code example or
    document where I figure this out?

    Thanks in advance for your help,
    David
  • you may want to create a subclass of NSTextField and in interface
    builder you change the class of the textfield to your own class. in
    this class you can implement the methods like keyUp:

    Kind Regards
    Karsten

    Am 09.09.2007 um 07:13 schrieb David Rio Deiros:

    > Hi,
    >
    > I have a very basic cocoa application that has an NSTextField, a
    > couple of buttons and a scroll view.
    > The user enters some word in the textfield, then clicks one of the
    > buttons and the application writes
    > something to the scroll view. The other button just cleans the
    > scroll view.
    >
    > The application works fine, but now I want to add some code so after
    > entering the word the user can
    > hit <enter> and then the results get generated in the scroll view
    > (same as if he/she press the "execute"
    > button).
    >
    > I have been reading a little bit about keyboard events and I am kind
    > of confuse. It seems that I have
    > to have an NSView class since its the one that implements
    > NSResponder, which is the one that allows handling
    > keyboard events.
    >
    > I have two classes:
    >
    > @interface AppController : NSObject
    >
    > and
    >
    > @interface Speller : NSObject
    >
    > As you can see both are NSObjects.
    >
    > What is the best way to add keyboard events to my application? Can
    > you point me to some code example or
    > document where I figure this out?
    >
    > Thanks in advance for your help,
    > David
    >
    >
  • On 9 Sep 2007, at 06:13, David Rio Deiros wrote:

    > I have a very basic cocoa application that has an NSTextField, a
    > couple of buttons and a scroll view.
    > The user enters some word in the textfield, then clicks one of the
    > buttons and the application writes
    > something to the scroll view. The other button just cleans the
    > scroll view.
    >
    > The application works fine, but now I want to add some code so
    > after entering the word the user can
    > hit <enter> and then the results get generated in the scroll view
    > (same as if he/she press the "execute"
    > button).

    Do you have a default button on the window already?  If not, a simple
    solution might be to set the shortcut for the "Execute" button to
    Return (in Interface Builder).

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
  • > I have a very basic cocoa application that has an NSTextField, a
    > couple of buttons and a scroll view.
    > The user enters some word in the textfield, then clicks one of the
    > buttons and the application writes
    > something to the scroll view. The other button just cleans the
    > scroll view.
    >
    > The application works fine, but now I want to add some code so
    > after entering the word the user can
    > hit <enter> and then the results get generated in the scroll view
    > (same as if he/she press the "execute"
    > button).

    Sounds like you simply have to set the same target and action for
    your NSTextField as for your Execute-Button.
    (In Interface Build connect your NSTextField with "Control-dragging"
    to the desired target and select the desired action from the list.)

    Regards,
    Mani
    --
    http://www.mani.de
    iVolume - Loudness adjustment for iTunes.
    LittleSecrets - The encrypted notepad.
  • On Tue, Sep 11, 2007 at 12:44:17PM +0100, Alastair Houghton wrote:
    > On 9 Sep 2007, at 06:13, David Rio Deiros wrote:
    >
    >> I have a very basic cocoa application that has an NSTextField, a
    >> couple of buttons and a scroll view.
    >> The user enters some word in the textfield, then clicks one of the
    >> buttons and the application writes
    >> something to the scroll view. The other button just cleans the
    >> scroll view.
    >>
    >> The application works fine, but now I want to add some code so
    >> after entering the word the user can
    >> hit <enter> and then the results get generated in the scroll view
    >> (same as if he/she press the "execute"
    >> button).

    > Do you have a default button on the window already?

    No.

    > If not, a simple
    > solution might be to set the shortcut for the "Execute" button to
    > Return (in Interface Builder).

    Well... I would like the user to hit "enter", I don't think I can assign
    that shortcut.

    David
  • On Tue, Sep 11, 2007 at 06:04:38PM +0200, Manfred Schwind wrote:
    >> I have a very basic cocoa application that has an NSTextField, a
    >> couple of buttons and a scroll view.
    >> The user enters some word in the textfield, then clicks one of the
    >> buttons and the application writes
    >> something to the scroll view. The other button just cleans the
    >> scroll view.
    >>
    >> The application works fine, but now I want to add some code so
    >> after entering the word the user can
    >> hit <enter> and then the results get generated in the scroll view
    >> (same as if he/she press the "execute"
    >> button).
    >
    > Sounds like you simply have to set the same target and action for
    > your NSTextField as for your Execute-Button.
    > (In Interface Build connect your NSTextField with "Control-dragging"
    > to the desired target and select the desired action from the list.)

    The target is already in place an it is being executed. The problem is
    that it only works if I use the mouse to click "execute". I would like
    the user to be able to hit the enter key right after inserting the information
    in the NSTextField so they don't have to use the mouse.
  • On 11-Sep-07, at 11:29 PM, David Rio Deiros wrote:

    > On Tue, Sep 11, 2007 at 06:04:38PM +0200, Manfred Schwind wrote:
    >>> I have a very basic cocoa application that has an NSTextField, a
    >>> couple of buttons and a scroll view.
    >>> The user enters some word in the textfield, then clicks one of the
    >>> buttons and the application writes
    >>> something to the scroll view. The other button just cleans the
    >>> scroll view.
    >>>
    >>> The application works fine, but now I want to add some code so
    >>> after entering the word the user can
    >>> hit <enter> and then the results get generated in the scroll view
    >>> (same as if he/she press the "execute"
    >>> button).

    I don't have much experience with Cocoa and there are probably a
    multitude of reasons why you shouldn't do what I'm about to suggest.

    However, why not use an NSFormatter?

    - (BOOL) isPartialStringValid:(NSString*) partialString
    newEditingString:(NSString**) newString errorDescription:(NSString**)
    error
    {
      //You'll probably have to change this I'm not sure if '\n' is what
    you want.  What ever enter is represented as in NSString is what you
    want
      //I just did a quick look but couldn't find it.
      NSRange locationOfEnter = [partialString rangeOfString: @"\n"];
      //check to see if there was a \n found.  If there was the length
    will be > 0 otherwise it will be 0
      if (locationOfEnter.length > 0)
      {
      //execute your code here to update the scroll view
      }
    }

    Liam
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today