Search code examples
cocoansmenunsmenuitemarrow-keys

how to set focus on NSMenu (make it get key events)?


I have a status menu with a NSSearchField and menu items. When search field is active and user presses up & down arrows, I'd like to start menu tracking, that is that the user will be able to move with arrow keys between menu items. I can catch moveUp and moveDown selectors. How can I cause the menu to become a key responder?

Thanks,

Nava


Solution

  • Fire a kCGEventMouseMoved event on mouseUp/Down. Show/hide cursor on your choice... The main challenge here is find the right origin (global mouse point). Someone has an idea how to find a NSMenuItem rect?

    CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, NSPointToCGPoint(globalOrigin) , kCGMouseButtonLeft);
    CGEventPost(kCGHIDEventTap, mouseEvent);
    CFRelease(mouseEvent);