Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:31:41+00:00 2026-06-05T13:31:41+00:00

I am writing my first Cocoa application. It’s a simple utility app — a

  • 0

I am writing my first Cocoa application. It’s a simple utility app — a calculator. I’ve got everything working, the last thing I want to add is I want the app to respond to key press events. So, you could click the “4” button with the mouse, or you could just press the “4” key on the keyboard. I’m reading all the event handling documentation, but I’m not picking up on the recommended approach for getting an NSResponder (or NSView) that handles the event.

I can make a subclass of NSWindow and tell the interface builder to make the main window of that type. Then when I override the keyDown message in my new NSWindow subclass, it gets the events, but I’m not sure how to connect it to my application delegate class (since I basically just want to switch on the key pressed and call the corresponding message in the delegate that the button “select” action targets.

Or, I think I heard something about doing this by changing the super class of the application delegate class from NSObject to NSView, or NSResponder or something. I haven’t poked at that idea much, but I’m not sure how to actually get the application delegate object then actually set up as the first responder (I think that’s what I’d want to do).

So, I’m hoping someone can give me specific directions on how/where I should implement my keyDown method, but I’d also like to understand what the recommended “best practice” is for doing this. What would Apple say is the right architecture for doing this? I’m as much interested in learning how to make this work as learning the Apple design pattern behind it.

The structure of my program is as follows:
I have one nib file (actualy a .xib) where I’ve designed my window with a textbox for display and a hand-ful of buttons. I’ve registered outlets and actions from there to the AppDelegate class (the one that the standard project template sets up for you). Following the MVC pattern, I then have a Calculator class that the AppDelegate initializes and all the actual logic of the calculator is in that class. The AppDelegate basically just responds to messages from the buttons, passes them along to methods in the Calculator class, and then asks the Calculator for the current display value and updates that back into the textbox. So, the graphical elements in my nib file are the “view”, the AppDelegate is the “controler” and the Calculator class is the “model”. Right?

So, who is supposed to be handling keyDown events? Seems like the view should capture them and send them to the controller, but how do I set that up?

I suppose a potentially related question is that i’ve seen mentions of “window controller” and “view controller” classes. I’m not sure I understand what the point of those classes are. Does my simple app have them? Should it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:31:41+00:00Added an answer on June 5, 2026 at 1:31 pm

    Use NSButton‘s setKeyEquivalent: method or set the key equivalent for each button in interface builder.

    I’m not sure of the “proper” way to do this, but custom key event handling can be put in a custom window contentView like so:

    - (void)keyDown:(NSEvent *)theEvent {
        NSLog(@"keyDown:%@", theEvent);
    
        //Put event handling code here.
    
        [super keyDown:theEvent];
    }
    
    - (BOOL)acceptsFirstResponder {
        return YES;
    }
    

    If anyone knows the correct way of doing this, feel free to chime in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing my first, very simple Rails application, a simple admin app to track work
I'm writing my first iphone app and through several examples I've got a UITableView
I'm writing a simple ObjC2.0/Cocoa application, and I'm getting a crash.. Not being familiar
Writing my first Android app. Thought I would start with a Corporate Directory application.
Hi I'm writing my first Java app and I've got a few Testcases (*.tc
Writing my first C# application...never touched the language before and not much of a
I'm writing my first WPF application and I'm trying to implement a fade animation
I'm writing my first gtk2hs+glade application, and I'm using a textview to display output
I am writing my first windows ce app. I am using an xml file
I'm writing my first Android application and trying to get my head around communication

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.