Basic event handling with core graphics

I am trying to handle events with core graphics and core fundation on mac os.



My code:







#import 
    #import 
   
    static int counter = 0;
   
    CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
   
        printf("event 0x%i\n ", counter++);
   
        return event;
    }
   
   
    int main(int argc, const char * argv[]) {
   
   
        CFRunLoopRef runner = CFRunLoopGetCurrent();
   
   
   
   
   
        CFMachPortRef mach = CGEventTapCreate(kCGHIDEventTap,kCGTailAppendEventTap , kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
   
   
        CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, mach, 0);
   
        CFRunLoopAddSource(runner, runLoopSource, kCFRunLoopCommonModes);
   
        CGEventTapEnable(mach, true);
   
   
        CFRunLoopRun();
   
        printf("end\n");
   
   
   
    }


But It reacts only for touchpad-events. Keyboard events seems to be ignored by my program. I have tried changing event masks but I does not work. Can you give some advice?



PS



Is that good start to handle events? I want to make full screen OpenGL game

with core graphics without libraries like SFML or SDL.

I am using CGDisplayCapture and CGL to create OpeGL context.

Accepted Reply

The correct way is to create a ObjC application with a NSOpenGLView then subclass that and grab all its events like mouseDown / mouseUp. (https://developer.apple.com/documentation/appkit/nsopenglview)


You can do what you propose, thats kind of what AGL does at a much lower level but all of these API's are deprecated...


Start with Metal (https://www.clientresourcesinc.com/2018/04/30/rendering-graphics-with-metalkit-swift-4-part-1/)


You don't need to do all that controller crap but just get the basics in place and work from your sub-classed MetalView


FYI, there is no longer an equivelent to fullscreen in OS X its just a fullscreen window.



Here are all the NSView routines you need to subclass.




- (void) mouseDown:(NSEvent *)event

{


}


- (void) mouseDragged:(NSEvent *)event

{


}


- (void) mouseUp:(NSEvent *)event

{


}


- (void) mouseMoved:(NSEvent *)event

{


}


- (void) mouseEntered:(NSEvent *)event

{


}


- (void) mouseExited:(NSEvent *)event

{


}


- (void) rightMouseDragged:(NSEvent *)event

{


}


- (void) rightMouseUp:(NSEvent *)event

{


}


- (void) otherMouseDown:(NSEvent *)event

{


}


- (void) otherMouseDragged:(NSEvent *)event

{


}


- (void) otherMouseUp:(NSEvent *)event

{


}


- (void) scrollWheel:(NSEvent *)event

{


}


- (void) keyDown:(NSEvent *)event

{


}


- (void) keyUp:(NSEvent *)event

{


}

Replies

The correct way is to create a ObjC application with a NSOpenGLView then subclass that and grab all its events like mouseDown / mouseUp. (https://developer.apple.com/documentation/appkit/nsopenglview)


You can do what you propose, thats kind of what AGL does at a much lower level but all of these API's are deprecated...


Start with Metal (https://www.clientresourcesinc.com/2018/04/30/rendering-graphics-with-metalkit-swift-4-part-1/)


You don't need to do all that controller crap but just get the basics in place and work from your sub-classed MetalView


FYI, there is no longer an equivelent to fullscreen in OS X its just a fullscreen window.



Here are all the NSView routines you need to subclass.




- (void) mouseDown:(NSEvent *)event

{


}


- (void) mouseDragged:(NSEvent *)event

{


}


- (void) mouseUp:(NSEvent *)event

{


}


- (void) mouseMoved:(NSEvent *)event

{


}


- (void) mouseEntered:(NSEvent *)event

{


}


- (void) mouseExited:(NSEvent *)event

{


}


- (void) rightMouseDragged:(NSEvent *)event

{


}


- (void) rightMouseUp:(NSEvent *)event

{


}


- (void) otherMouseDown:(NSEvent *)event

{


}


- (void) otherMouseDragged:(NSEvent *)event

{


}


- (void) otherMouseUp:(NSEvent *)event

{


}


- (void) scrollWheel:(NSEvent *)event

{


}


- (void) keyDown:(NSEvent *)event

{


}


- (void) keyUp:(NSEvent *)event

{


}

thank you very much

🙂