docs:programming:cocoa_and_objective-c:handling_key_events

Handling Key Events

  • any subclass of NSResponder can accept events
  • subclass NSWindow, with MyWindow
  • in IB, set the window to use the custom class, MyWindow
  • override the following method in MyWindow:
    - (void)keyDown:(NSEvent *)event
    {
    	switch ([event keyCode]) {
    		case 121:{ // page down
    			if([event modifierFlags] & NSCommandKeyMask) // only do something if the command key was also pressed
    				// do something
    			break;
    		}
    	}
    }
  • make the view the first responder
    • there may be a better way to do this, but you can use this:
      [view becomeFirstResponder];
      
      or
      
      [window makeFirstResponder:view];
  • override the keyDown method in the view (see NSWindow above for example)
  • docs/programming/cocoa_and_objective-c/handling_key_events.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1