cocoa - 如何在 Mac OS X 下捕获/发布系统范围的键盘/鼠标事件?

标签 cocoa macos

对于脚本实用程序,我需要能够记录应用程序获得焦点时发生的一系列键盘和鼠标事件。第二部分是稍后能够将这些事件发送到事件窗口。

我不需要担心菜单或跟踪哪个窗口接收输入的标识符。

我知道如何在 Windows 下执行此操作,但不知道 Mac OS X 下的操作。

最佳答案

我要告诉您的第一件事是,如果用户没有在辅助功能控制面板中启用对辅助设备的支持,您无法执行此操作。这是 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);
}

关于cocoa - 如何在 Mac OS X 下捕获/发布系统范围的键盘/鼠标事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/399616/

相关文章:

ios - 如何在 swift 中将字符索引从布局管理器转换为字符串比例

c++ - 调整 native 窗口大小

c - IrDA 发现的默认设置(波特率、位、停止、奇偶校验、流量控制等)

macos - 从 Github for Mac 单独安装 Git

iphone - 如何测试方法的协议(protocol)?

cocoa - 如何处理键盘快捷键(Command + control 等快捷键)?

macos - 操作系统 X : Launching app from dock is a no-op if app is already running headless (via NSApplicationActivationPolicyProhibited)

swift - 本地化问题?从文本字段中提取时,数字在逗号处被修剪

ios - MacOS 外接显示器插入时如何收到通知

java - 如何监控系统范围内的用户 Activity ?