Как захватить / опубликовать общесистемные события клавиатуры / мыши в Mac OS X? - PullRequest
5 голосов
/ 30 декабря 2008

Для скриптовой утилиты мне нужно иметь возможность записывать серию событий клавиатуры и мыши, которые происходят, когда приложение находится в фокусе. Вторая часть может позже отправить эти события в активное окно.

Мне не нужно беспокоиться о меню или отслеживании идентификатора того окна, в которое поступает ввод.

Я знаю, как это сделать под Windows, но понятия не имею о Mac OS X.

Ответы [ 3 ]

10 голосов
/ 30 декабря 2008

Первое, что я скажу вам, это то, что вы НЕ МОЖЕТЕ делать это без включения пользователем поддержки вспомогательных устройств на панели управления доступом. Это какая-то защита, встроенная в OSX.

Вот фрагмент кода, который я использую в одном из своих приложений для этого:

//this method calls a carbon method to attach a global event handler
- (void)attachEventHandlers
{
    //create our event type spec for the keyup
    EventTypeSpec eventType;
    eventType.eventClass = kEventClassKeyboard;
    eventType.eventKind = kEventRawKeyUp;

    //create a callback for our event to fire in
    EventHandlerUPP handlerFunction = NewEventHandlerUPP(globalKeyPress);

    //install the event handler
    OSStatus err = InstallEventHandler(GetEventMonitorTarget(), handlerFunction, 1, &eventType, self, NULL);

    //error checking
    if( err )
    {
        //TODO: need an alert sheet here
        NSLog(@"Error registering keyboard handler...%d", err);
    }

    //create our event type spec for the mouse events
    EventTypeSpec eventTypeM;
    eventTypeM.eventClass = kEventClassMouse;
    eventTypeM.eventKind = kEventMouseUp;

    //create a callback for our event to fire in
    EventHandlerUPP handlerFunctionM = NewEventHandlerUPP(globalMousePress);

    //install the event handler
    OSStatus errM = InstallEventHandler(GetEventMonitorTarget(), handlerFunctionM, 1, &eventTypeM, self, NULL);

    //error checking
    if( errM )
    {
        //TODO: need an alert sheet here
        NSLog(@"Error registering mouse handler...%d", err);
    }
}

Вот пример метода обратного вызова, который я использую:

OSStatus globalKeyPress(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) 
{
    NSEvent *anEvent = [NSEvent eventWithEventRef:theEvent];
    NSEventType type = [anEvent type];
    WarStrokerApplication *application = (WarStrokerApplication*)userData;

    //is it a key up event?
    if( type == NSKeyUp)
    {
        //which key is it?
        switch( [anEvent keyCode] )
        {
            case NUMERIC_KEYPAD_PLUS: 
                //this is the character we are using for our toggle
                //call the handler function
                [application toggleKeyPressed];
                break;

                //Comment this line back in to figure out the keykode for a particular character                
            default:
                NSLog(@"Keypressed: %d, **%@**", [anEvent keyCode], [anEvent characters]);
                break;
        }
    }

    return CallNextEventHandler(nextHandler, theEvent);
}
4 голосов
/ 30 декабря 2008

Для последней части, публикуя события, используйте методы CGEvent, представленные в ApplicationServices / ApplicationServices.h

Вот пример функции для перемещения мыши в указанное абсолютное положение:

#include <ApplicationServices/ApplicationServices.h>

int to(int x, int y)
{
    CGPoint newloc;
    CGEventRef eventRef;
    newloc.x = x;
    newloc.y = y;

    eventRef = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, newloc,
                                        kCGMouseButtonCenter);
    //Apparently, a bug in xcode requires this next line
    CGEventSetType(eventRef, kCGEventMouseMoved);
    CGEventPost(kCGSessionEventTap, eventRef);
    CFRelease(eventRef);

    return 0;
}
1 голос
/ 30 декабря 2008

О событиях касания мыши см. http://osxbook.com/book/bonus/chapter2/altermouse/

Я не проверял это под 10.5 Leopard, но на 10.4 это работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...