2

我正在使用 EventTap,我需要在代码中设置 CGEvent 的压力(我正在制作压力平均应用程序)。

目前,除了将我的 CGEvent 转换为 NSEvent 来改变压力之外,我没有看到任何其他方法。现在的问题是,有时我会遇到“解除分配的实例错误”并且我的应用程序崩溃了。我正在使用ARC。我使用 Zombie Instrument 来追踪问题。这是仪器的屏幕截图。显然,CGEvent 或 NSEvent 的某个地方正在收到一条发布消息,但它已经发布了。我不知道如何才能更好地控制这个发布过程...... :(。

这是我在 handleCGEvent 方法中使用的代码

CGEventRef handleCGEvent(CGEventTapProxy proxy, CGEventType type,     CGEventRef eventRef, void *refcon)
{
    if(type == kCGEventLeftMouseDragged)
    {
        NSEvent *wacomEvent = [NSEvent eventWithCGEvent:eventRef];

    if (wacomEvent != nil && wacomEvent.subtype == NSTabletPointEventSubtype)
    {
        [pressureBuffer replaceObjectAtIndex:pressureBufferIndex withObject:[NSNumber numberWithFloat: wacomEvent.pressure]];
        pressureBufferIndex++;
        pressureBufferIndex %= pressureBufferSize;

        if (pressureBufferFilledCount < pressureBufferSize)
            pressureBufferFilledCount++;

        float sum = 0;

        for (NSUInteger i = 0; i < pressureBufferFilledCount; i++)
        {
            sum += [[pressureBuffer objectAtIndex:i] floatValue];
        }

        averagePressure = sum / pressureBufferFilledCount;

        NSEvent *newPressureEvent = [NSEvent mouseEventWithType:wacomEvent.type
                                                       location:wacomEvent.locationInWindow
                                                  modifierFlags:wacomEvent.modifierFlags
                                                      timestamp:wacomEvent.timestamp
                                                   windowNumber:wacomEvent.windowNumber
                                                        context:wacomEvent.context
                                                    eventNumber:wacomEvent.eventNumber
                                                     clickCount:wacomEvent.clickCount
                                                       pressure:averagePressure]; //averagePressure

        return [newPressureEvent CGEvent];
    }
}

return eventRef;
}

……

谢谢,非常感谢任何帮助!!!!

僵尸仪器截图!!!

4

0 回答 0