Making custom key-commands

  • Hello All,

    I'm new to Cocoa and Objective-C, having recently started a project to
    port a music notation-based GUI I've been writing in Java/Processing,
    over to Cocoa. There are a number of reasons for the change, the main
    one being that Processing is really hard on the cpu!

    Things are basically going fine, but I'm a bit stumped about handling
    key strokes, particularly in the sense of creating my own key-
    commands. Can anyone clarify the best way to go about this? Even just
    starting with something simple, like using backspace and/or delete to
    delete the selected object (from a custom view - I don't need help
    with the deleting part... just registering the delete/backspace). Some
    places say to override keyDown, while others suggest this isn't a good
    idea, and I'm finding Apple's documentation more than a little bit
    confusing on what best-practice really is.
    Other examples of the things I need to do would be things like
    creating particular subclasses of objects if a given key is held at
    the time the object is created, or toggling an attribute of a given
    object using a specific key. This stuff is all really simple in
    Processing, and probably simple enough in Cocoa, but I've no idea
    where to start...

    Any help or pointers greatly appreciated - and code snippets even more
    so! ;-)

    thanks,

    J.
  • On Dec 17, 2007, at 12:50 PM, James Maxwell wrote:

    > Things are basically going fine, but I'm a bit stumped about
    > handling key strokes, particularly in the sense of creating my own
    > key-commands. Can anyone clarify the best way to go about this?

    Normally the way you do this is to add menu items for the commands,
    and then assign a keystroke to the menu items. For delete, for
    example, you'd add a "Delete" menu item with the backspace key (_not_
    the delete key; that confuses people and can't be easily accessed on
    laptop keyboards) assigned to the item.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • On Dec 17, 2007, at 1:11 PM, Nick Zitzmann wrote:

    > On Dec 17, 2007, at 12:50 PM, James Maxwell wrote:
    >
    >> Things are basically going fine, but I'm a bit stumped about
    >> handling key strokes, particularly in the sense of creating my own
    >> key-commands. Can anyone clarify the best way to go about this?
    >
    > Normally the way you do this is to add menu items for the commands,
    > and then assign a keystroke to the menu items. For delete, for
    > example, you'd add a "Delete" menu item with the backspace key
    > (_not_ the delete key; that confuses people and can't be easily
    > accessed on laptop keyboards) assigned to the item.

    You mean the key to the right of the +/= key that's labeled "delete"
    on all the Apple keyboards (at least the 4, plus 2 MacAlly ones) I
    have here?  Yeah, no confusion there. :)
  • On Dec 17, 2007, at 5:11 PM, Randall Meadows wrote:

    > You mean the key to the right of the +/= key that's labeled "delete"
    > on all the Apple keyboards (at least the 4, plus 2 MacAlly ones) I
    > have here?  Yeah, no confusion there. :)

    Yeah, but that key is really the backspace key careless of how it's
    been labeled. The key labeled "delete" that's located below the help
    key on ANSI keyboards is the _real_ delete key. Plus, it says "delete"
    on both keys, but they do two different things. That's why there's
    confusion...

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • Okay, I've been trying to get this working, but I'm not getting
    anywhere. I think this is because I'm creating a document-based
    application, and I can't figure out how to get the main menu to talk
    to my document's view.
    Does anyone know of a piece of sample code for getting key commands
    and their associated menu items working in a document-based app?

    This is a bit of a nightmare, right now, but I'm hoping it will become
    clearer soon...

    thanks,

    J.

    On 17-Dec-07, at 2:10 PM, Nick Zitzmann wrote:

    >
    > On Dec 17, 2007, at 3:08 PM, <jbmaxwell...> wrote:
    >
    >> The only thing that bothers me a bit about this is that some of the
    >> commands I have in mind change depending on the selected object.
    >> Would that just be a matter of having different classes handle the
    >> menu commands differently?
    >
    > You can deal with that by using the NSMenu delegate methods, added
    > to Panther & later.
    >
    >> BTW, Are the menu key-commands set-up in IB, or are they coded?
    >
    >
    > You can do it either way.
    >
    > Nick Zitzmann
    > <http://www.chronosnet.com/>
    >
  • Not aware of a sample showing this, but I'll try to explain what's
    needed - assuming your using IB3/Leopard.

    1) Add the menu item in your MainMenu nib. The title can be whatever,
    the key should be the key you want to capture.
    2) Select the First Responder in that nib. Open the "Identity"
    inspector. Add an action in the "Class Actions" section at the top.
    The name of the action doesn't matter, but it has to end with a ":".
    3) Connect your menu item to the First Responder, to the new action
    you added.
    4) In your document class, implement the action.

    When your document window is active, your document will be in the
    responder chain and receive events. If it has implemented the actions
    associated with the events, its implementation will be called unless
    another object in the chain handles them first. Rather than say more,
    the best thing is to look at http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/Eve
    ntArchitecture/chapter_2_section_6.html

      for a more in depth explanation of how this works :)

    -wil

    On Dec 18, 2007, at 10:41 AM, James Maxwell wrote:

    > Okay, I've been trying to get this working, but I'm not getting
    > anywhere. I think this is because I'm creating a document-based
    > application, and I can't figure out how to get the main menu to talk
    > to my document's view.
    > Does anyone know of a piece of sample code for getting key commands
    > and their associated menu items working in a document-based app?
    >
    > This is a bit of a nightmare, right now, but I'm hoping it will
    > become clearer soon...
    >
    > thanks,
    >
    > J.
    >
    >
    > On 17-Dec-07, at 2:10 PM, Nick Zitzmann wrote:
    >
    >>
    >> On Dec 17, 2007, at 3:08 PM, <jbmaxwell...> wrote:
    >>
    >>> The only thing that bothers me a bit about this is that some of
    >>> the commands I have in mind change depending on the selected
    >>> object. Would that just be a matter of having different classes
    >>> handle the menu commands differently?
    >>
    >> You can deal with that by using the NSMenu delegate methods, added
    >> to Panther & later.
    >>
    >>> BTW, Are the menu key-commands set-up in IB, or are they coded?
    >>
    >>
    >> You can do it either way.
    >>
    >> Nick Zitzmann
    >> <http://www.chronosnet.com/>
    >>

  • Thanks William.

    I was trying to implement the method in my controller class, not the
    document class - that did the trick!

    thanks _sooooo_ much for the tip!
    (ps - I'm guessing this is necessary because it's the document that's
    first responder, yes?)

    J.

    On 18-Dec-07, at 12:11 PM, William Turner wrote:

    > Not aware of a sample showing this, but I'll try to explain what's
    > needed - assuming your using IB3/Leopard.
    >
    > 1) Add the menu item in your MainMenu nib. The title can be
    > whatever, the key should be the key you want to capture.
    > 2) Select the First Responder in that nib. Open the "Identity"
    > inspector. Add an action in the "Class Actions" section at the top.
    > The name of the action doesn't matter, but it has to end with a ":".
    > 3) Connect your menu item to the First Responder, to the new action
    > you added.
    > 4) In your document class, implement the action.
    >
    > When your document window is active, your document will be in the
    > responder chain and receive events. If it has implemented the
    > actions associated with the events, its implementation will be
    > called unless another object in the chain handles them first. Rather
    > than say more, the best thing is to look at http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/Eve
    ntArchitecture/chapter_2_section_6.html

    > for a more in depth explanation of how this works :)
    >
    > -wil
    >
    > On Dec 18, 2007, at 10:41 AM, James Maxwell wrote:
    >
    >> Okay, I've been trying to get this working, but I'm not getting
    >> anywhere. I think this is because I'm creating a document-based
    >> application, and I can't figure out how to get the main menu to
    >> talk to my document's view.
    >> Does anyone know of a piece of sample code for getting key commands
    >> and their associated menu items working in a document-based app?
    >>
    >> This is a bit of a nightmare, right now, but I'm hoping it will
    >> become clearer soon...
    >>
    >> thanks,
    >>
    >> J.
    >>
    >>
    >> On 17-Dec-07, at 2:10 PM, Nick Zitzmann wrote:
    >>
    >>>
    >>> On Dec 17, 2007, at 3:08 PM, <jbmaxwell...> wrote:
    >>>
    >>>> The only thing that bothers me a bit about this is that some of
    >>>> the commands I have in mind change depending on the selected
    >>>> object. Would that just be a matter of having different classes
    >>>> handle the menu commands differently?
    >>>
    >>> You can deal with that by using the NSMenu delegate methods, added
    >>> to Panther & later.
    >>>
    >>>> BTW, Are the menu key-commands set-up in IB, or are they coded?
    >>>
    >>>
    >>> You can do it either way.
    >>>
    >>> Nick Zitzmann
    >>> <http://www.chronosnet.com/>
    >>>

    >
  • Glad that helped. In general (but not always) menu actions will go
    through the first responder. The document is _in_ the responder chain,
    but so are views, windows, window controllers, the app, the app
    delegate, etc. - the responder chain section of the document I refer
    to below is going to explain it better than I can :)

    In your case, it's possible that you could handle the action in your
    controller - if it is part of the chain, such as being the
    NSWindowController managing your document window.

    -wil

    On Dec 18, 2007, at 12:33 PM, James Maxwell wrote:

    > Thanks William.
    >
    > I was trying to implement the method in my controller class, not the
    > document class - that did the trick!
    >
    > thanks _sooooo_ much for the tip!
    > (ps - I'm guessing this is necessary because it's the document
    > that's first responder, yes?)
    >
    > J.
    >
    >
    > On 18-Dec-07, at 12:11 PM, William Turner wrote:
    >
    >> Not aware of a sample showing this, but I'll try to explain what's
    >> needed - assuming your using IB3/Leopard.
    >>
    >> 1) Add the menu item in your MainMenu nib. The title can be
    >> whatever, the key should be the key you want to capture.
    >> 2) Select the First Responder in that nib. Open the "Identity"
    >> inspector. Add an action in the "Class Actions" section at the top.
    >> The name of the action doesn't matter, but it has to end with a ":".
    >> 3) Connect your menu item to the First Responder, to the new action
    >> you added.
    >> 4) In your document class, implement the action.
    >>
    >> When your document window is active, your document will be in the
    >> responder chain and receive events. If it has implemented the
    >> actions associated with the events, its implementation will be
    >> called unless another object in the chain handles them first.
    >> Rather than say more, the best thing is to look at http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/Eve
    ntArchitecture/chapter_2_section_6.html

    >> for a more in depth explanation of how this works :)
    >>
    >> -wil
    >>
    >> On Dec 18, 2007, at 10:41 AM, James Maxwell wrote:
    >>
    >>> Okay, I've been trying to get this working, but I'm not getting
    >>> anywhere. I think this is because I'm creating a document-based
    >>> application, and I can't figure out how to get the main menu to
    >>> talk to my document's view.
    >>> Does anyone know of a piece of sample code for getting key
    >>> commands and their associated menu items working in a document-
    >>> based app?
    >>>
    >>> This is a bit of a nightmare, right now, but I'm hoping it will
    >>> become clearer soon...
    >>>
    >>> thanks,
    >>>
    >>> J.
    >>>
    >>>
    >>> On 17-Dec-07, at 2:10 PM, Nick Zitzmann wrote:
    >>>
    >>>>
    >>>> On Dec 17, 2007, at 3:08 PM, <jbmaxwell...> wrote:
    >>>>
    >>>>> The only thing that bothers me a bit about this is that some of
    >>>>> the commands I have in mind change depending on the selected
    >>>>> object. Would that just be a matter of having different classes
    >>>>> handle the menu commands differently?
    >>>>
    >>>> You can deal with that by using the NSMenu delegate methods,
    >>>> added to Panther & later.
    >>>>
    >>>>> BTW, Are the menu key-commands set-up in IB, or are they coded?
    >>>>
    >>>>
    >>>> You can do it either way.
    >>>>
    >>>> Nick Zitzmann
    >>>> <http://www.chronosnet.com/>
    >>>>

    >>

  • Hi William,

    It looks like you're doing more teaching than working here, so just
    one more quick question. I've been looking at the document you linked
    me, but from what it says, it looks like my controller should be in
    the chain, yet I can't seem to call methods in the controller from my
    menu directly... Basically, your recommendation got the function
    itself working - I can now change tools with a key command - but the
    associated NSSegmentedControl I'm using to mouse-pick the tools is not
    updating when I change the tool via the menu/key command. Is there a
    quick and dirty way to do this? Once I've seen it done, I'm sure it
    will sink in.

    thanks again,

    J.

    On 18-Dec-07, at 12:59 PM, William Turner wrote:

    > Glad that helped. In general (but not always) menu actions will go
    > through the first responder. The document is _in_ the responder
    > chain, but so are views, windows, window controllers, the app, the
    > app delegate, etc. - the responder chain section of the document I
    > refer to below is going to explain it better than I can :)
    >
    > In your case, it's possible that you could handle the action in your
    > controller - if it is part of the chain, such as being the
    > NSWindowController managing your document window.
    >
    > -wil
    >
    > On Dec 18, 2007, at 12:33 PM, James Maxwell wrote:
    >
    >> Thanks William.
    >>
    >> I was trying to implement the method in my controller class, not
    >> the document class - that did the trick!
    >>
    >> thanks _sooooo_ much for the tip!
    >> (ps - I'm guessing this is necessary because it's the document
    >> that's first responder, yes?)
    >>
    >> J.
    >>
    >>
    >> On 18-Dec-07, at 12:11 PM, William Turner wrote:
    >>
    >>> Not aware of a sample showing this, but I'll try to explain what's
    >>> needed - assuming your using IB3/Leopard.
    >>>
    >>> 1) Add the menu item in your MainMenu nib. The title can be
    >>> whatever, the key should be the key you want to capture.
    >>> 2) Select the First Responder in that nib. Open the "Identity"
    >>> inspector. Add an action in the "Class Actions" section at the
    >>> top. The name of the action doesn't matter, but it has to end with
    >>> a ":".
    >>> 3) Connect your menu item to the First Responder, to the new
    >>> action you added.
    >>> 4) In your document class, implement the action.
    >>>
    >>> When your document window is active, your document will be in the
    >>> responder chain and receive events. If it has implemented the
    >>> actions associated with the events, its implementation will be
    >>> called unless another object in the chain handles them first.
    >>> Rather than say more, the best thing is to look at http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/Eve
    ntArchitecture/chapter_2_section_6.html

    >>> for a more in depth explanation of how this works :)
    >>>
    >>> -wil
    >>>
    >>> On Dec 18, 2007, at 10:41 AM, James Maxwell wrote:
    >>>
    >>>> Okay, I've been trying to get this working, but I'm not getting
    >>>> anywhere. I think this is because I'm creating a document-based
    >>>> application, and I can't figure out how to get the main menu to
    >>>> talk to my document's view.
    >>>> Does anyone know of a piece of sample code for getting key
    >>>> commands and their associated menu items working in a document-
    >>>> based app?
    >>>>
    >>>> This is a bit of a nightmare, right now, but I'm hoping it will
    >>>> become clearer soon...
    >>>>
    >>>> thanks,
    >>>>
    >>>> J.
    >>>>
    >>>>
    >>>> On 17-Dec-07, at 2:10 PM, Nick Zitzmann wrote:
    >>>>
    >>>>>
    >>>>> On Dec 17, 2007, at 3:08 PM, <jbmaxwell...> wrote:
    >>>>>
    >>>>>> The only thing that bothers me a bit about this is that some of
    >>>>>> the commands I have in mind change depending on the selected
    >>>>>> object. Would that just be a matter of having different classes
    >>>>>> handle the menu commands differently?
    >>>>>
    >>>>> You can deal with that by using the NSMenu delegate methods,
    >>>>> added to Panther & later.
    >>>>>
    >>>>>> BTW, Are the menu key-commands set-up in IB, or are they coded?
    >>>>>
    >>>>>
    >>>>> You can do it either way.
    >>>>>
    >>>>> Nick Zitzmann
    >>>>> <http://www.chronosnet.com/>
    >>>>>

    >>>

    >
  • In IB, check to see if your controller - is it an NSWindowController
    subclass? - is the delegate of the window. The window controller
    should get the messages because it is the delegate of the window. As
    far as updating the NSSegmentedControl, I assume you're doing that
    programmatically in your controller, which is why you need the
    controller's implementation to be called. I try to avoid "dirty", but
    if all else fails you might be able to call your controller's
    implementation from your document class, assuming there's a reference
    to the controller there.

    -wil

    PS - maybe teaching _is_ working, for me :)
    On Dec 18, 2007, at 1:23 PM, James Maxwell wrote:

    > Hi William,
    >
    > It looks like you're doing more teaching than working here, so just
    > one more quick question. I've been looking at the document you
    > linked me, but from what it says, it looks like my controller should
    > be in the chain, yet I can't seem to call methods in the controller
    > from my menu directly... Basically, your recommendation got the
    > function itself working - I can now change tools with a key command
    > - but the associated NSSegmentedControl I'm using to mouse-pick the
    > tools is not updating when I change the tool via the menu/key
    > command. Is there a quick and dirty way to do this? Once I've seen
    > it done, I'm sure it will sink in.
    >
    > thanks again,
    >
    > J.
    >
    >
    > On 18-Dec-07, at 12:59 PM, William Turner wrote:
    >
    >> Glad that helped. In general (but not always) menu actions will go
    >> through the first responder. The document is _in_ the responder
    >> chain, but so are views, windows, window controllers, the app, the
    >> app delegate, etc. - the responder chain section of the document I
    >> refer to below is going to explain it better than I can :)
    >>
    >> In your case, it's possible that you could handle the action in
    >> your controller - if it is part of the chain, such as being the
    >> NSWindowController managing your document window.
    >>
    >> -wil
    >>
    >> On Dec 18, 2007, at 12:33 PM, James Maxwell wrote:
    >>
    >>> Thanks William.
    >>>
    >>> I was trying to implement the method in my controller class, not
    >>> the document class - that did the trick!
    >>>
    >>> thanks _sooooo_ much for the tip!
    >>> (ps - I'm guessing this is necessary because it's the document
    >>> that's first responder, yes?)
    >>>
    >>> J.
    >>>
    >>>
    >>> On 18-Dec-07, at 12:11 PM, William Turner wrote:
    >>>
    >>>> Not aware of a sample showing this, but I'll try to explain
    >>>> what's needed - assuming your using IB3/Leopard.
    >>>>
    >>>> 1) Add the menu item in your MainMenu nib. The title can be
    >>>> whatever, the key should be the key you want to capture.
    >>>> 2) Select the First Responder in that nib. Open the "Identity"
    >>>> inspector. Add an action in the "Class Actions" section at the
    >>>> top. The name of the action doesn't matter, but it has to end
    >>>> with a ":".
    >>>> 3) Connect your menu item to the First Responder, to the new
    >>>> action you added.
    >>>> 4) In your document class, implement the action.
    >>>>
    >>>> When your document window is active, your document will be in the
    >>>> responder chain and receive events. If it has implemented the
    >>>> actions associated with the events, its implementation will be
    >>>> called unless another object in the chain handles them first.
    >>>> Rather than say more, the best thing is to look at http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/Eve
    ntArchitecture/chapter_2_section_6.html

    >>>> for a more in depth explanation of how this works :)
    >>>>
    >>>> -wil
    >>>>
    >>>> On Dec 18, 2007, at 10:41 AM, James Maxwell wrote:
    >>>>
    >>>>> Okay, I've been trying to get this working, but I'm not getting
    >>>>> anywhere. I think this is because I'm creating a document-based
    >>>>> application, and I can't figure out how to get the main menu to
    >>>>> talk to my document's view.
    >>>>> Does anyone know of a piece of sample code for getting key
    >>>>> commands and their associated menu items working in a document-
    >>>>> based app?
    >>>>>
    >>>>> This is a bit of a nightmare, right now, but I'm hoping it will
    >>>>> become clearer soon...
    >>>>>
    >>>>> thanks,
    >>>>>
    >>>>> J.
    >>>>>
    >>>>>
    >>>>> On 17-Dec-07, at 2:10 PM, Nick Zitzmann wrote:
    >>>>>
    >>>>>>
    >>>>>> On Dec 17, 2007, at 3:08 PM, <jbmaxwell...> wrote:
    >>>>>>
    >>>>>>> The only thing that bothers me a bit about this is that some
    >>>>>>> of the commands I have in mind change depending on the
    >>>>>>> selected object. Would that just be a matter of having
    >>>>>>> different classes handle the menu commands differently?
    >>>>>>
    >>>>>> You can deal with that by using the NSMenu delegate methods,
    >>>>>> added to Panther & later.
    >>>>>>
    >>>>>>> BTW, Are the menu key-commands set-up in IB, or are they coded?
    >>>>>>
    >>>>>>
    >>>>>> You can do it either way.
    >>>>>>
    >>>>>> Nick Zitzmann
    >>>>>> <http://www.chronosnet.com/>
    >>>>>>

    >>>>

    >>

previous month december 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
31            
Go to today