[Openmcl-devel] Two quick questions

Ralf Stoye stoye at stoye.com
Mon Nov 5 16:51:52 PST 2007


> I'm interested in keyboard events in particular but I'm pretty sure  
> I could extrapolate from mouse events).

Last time i had to "get key events", i defined a sendEvent: message  
for my windowclass, (search for "Event Dispatch" in xcode/Documentation)
The events i had to capture were initiated by an external (serial  
line attached) keyboard, the only purpose of the window/application  
was to
inform another process about actions to be taken. It is not  
recommended to do it like this, it just shows how to get these  
events. To get the key events for a window, i would try keyUp: or  
keyDown:, just look at the Documentation for NSWindow: Event Handling  
in XCode


// from MyWindow.h:
@interface MyWindow : NSWindow {
...
}
- (void)sendEvent:(NSEvent *)theEvent;


// from MyWindow.m:
- (void)sendEvent:(NSEvent *)theEvent
{
     //NSLog(@"MyWindow: sendEvent with %@", theEvent);
     //[super sendEvent:theEvent];
         if([theEvent type] == NSKeyDown) {
         //NSLog(@"MyWindow: sendEvent with NSKeyDown: %@", theEvent);
         switch ([theEvent keyCode]) {
			
	    case 0:    // "anfang" (a")-> special1
		//NSLog(@"MyWindow: i got 51!!");
                 [dnc postNotification:mynoteGoStart];
                 return;
                 break;
				
             case 49:    // space -> nextMovie
                 //NSLog(@"MyWindow: i got space!!");
                 //[controller goNext:nil];
		[dnc postNotification:mynoteGoNext];
                 return;
                 break;
				
	     case 126:    // cursor up
			[dnc postNotification:mynoteGoUp];
                 return;
                 break;
             default:
                 [super sendEvent:theEvent];
                 return;
         }

     } else {
         [super sendEvent:theEvent];
     }

}




More information about the Openmcl-devel mailing list