问题
I'm trying to get touch events in my application. So I used the IOHIDFamily callback to get the events. My code is like this:
void handle_event(void* target, void* refcon, IOHIDServiceRef service, IOHIDEventRef event)
{
printf("Received event of type %2d from service %p.\n",
IOHIDEventGetType(event), service);
}
- (void)viewDidLoad
{
[super viewDidLoad];
void *ioHIDEventSystem = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
IOHIDEventSystemClientScheduleWithRunLoop(system, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOHIDEventSystemClientRegisterEventCallback(system, handle_event, NULL, NULL);
CFRunLoopRun();
}
I got an error when executing:
IOHIDEventSystemClientScheduleWithRunLoop(system, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOKit`IOHIDEventSystemClientScheduleWithRunLoop:
...
0x32f8fd14: cmp.w r10, #0
0x32f8fd18: strd r10, r11, [r4, #116] <---EXC_BAD_ACCESS(code=EXC_ARM_DA_ALIGN)
0x32f8fd1c: beq 0x32f8fdac ; IOHIDEventSystemClientScheduleWithRunLoop + 168
0x32f8fd1e: ldr r1, [r4, #96]
0x32f8fd20: cbz r1, 0x32f8fd2a ; IOHIDEventSystemClientScheduleWithRunLoop + 38
0x32f8fd22: mov r0, r10
...
Did I use IOHIDFamily in the wrong way?
回答1:
There's at least a couple problems I see in the code posted:
First, you are calling
CFRunLoopRun();
in the viewDidLoad
method, which is going to be called on the main/UI thread. I see no reason for that, so just remove that line. I'd normally expect to see that call if you had a method that you were running on a background thread, and you needed to start a background run loop. Or, if you were registering for callbacks directly in main()
, as in this answer.
Then, you have this:
void *ioHIDEventSystem = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
IOHIDEventSystemClientScheduleWithRunLoop(system, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
I'm guessing the second line should be
IOHIDEventSystemClientScheduleWithRunLoop(ioHIDEventSystem, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
I don't know what the system
variable actually refers to, but it doesn't look right.
Take a look at this recent answer, as it seems to use IOKit correctly.
来源:https://stackoverflow.com/questions/15629514/iohideventsystemclientschedulewithrunloop-with-exc-bad-access