Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
docs:programming:cocoa_and_objective-c:handling_key_events [2007/05/29 10:05] – billh | docs:programming:cocoa_and_objective-c:handling_key_events [2008/08/03 00:25] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Handling Key Events ====== | ||
+ | * any subclass of NSResponder can accept events | ||
+ | ===== key events from NSWindow ===== | ||
+ | * subclass NSWindow, with MyWindow | ||
+ | * in IB, set the window to use the custom class, MyWindow | ||
+ | * override the following method in MyWindow:< | ||
+ | - (void)keyDown: | ||
+ | { | ||
+ | switch ([event keyCode]) { | ||
+ | case 121:{ // page down | ||
+ | if([event modifierFlags] & NSCommandKeyMask) // only do something if the command key was also pressed | ||
+ | // do something | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== key events from a custom view ===== | ||
+ | * 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: | ||
+ | </ | ||
+ | * override the keyDown method in the view (see NSWindow above for example) |