先上图
绿色部分是N个按钮,点击任意一个按钮会变成红色
下面上主要的代码
- (void)viewDidLoad
{
[super viewDidLoad];
IOHIDEventQueueRef queue = IOHIDEventQueueCreate(kCFAllocatorDefault, 1, 14);
IOHIDEventQueueSetNotificationPort(queue, [self getPort]);
IOHIDEventQueueEnqueue(queue, [self getdownEvent]);
IOHIDEventQueueEnqueue(queue, [self getupEvent]);
}
- (mach_port_t)getPort
{
void *IOKit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW);
if (IOKit)
{
mach_port_t *kIOMasterPortDefault = dlsym(IOKit, “kIOMasterPortDefault”);
CFMutableDictionaryRef (*IOServiceMatching)(const char *name) = dlsym(IOKit, “IOServiceMatching”);
mach_port_t (*IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching) = dlsym(IOKit, “IOServiceGetMatchingService”);
CFTypeRef (*IORegistryEntryCreateCFProperty)(mach_port_t entry, CFStringRef key, CFAllocatorRef allocator, uint32_t options) = dlsym(IOKit, “IORegistryEntryCreateCFProperty”);
kern_return_t (*IOObjectRelease)(mach_port_t object) = dlsym(IOKit, “IOObjectRelease”);
if (kIOMasterPortDefault && IOServiceGetMatchingService && IORegistryEntryCreateCFProperty && IOObjectRelease)
{
mach_port_t platformExpertDevice = IOServiceGetMatchingService(*kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
return platformExpertDevice;
}
dlclose(IOKit);
}
return 0;
}
- (IOHIDEventRef)getdownEvent
{
uint64_t abTime = mach_absolute_time();
AbsoluteTime timeStamp = *(AbsoluteTime *) &abTime;
//iOS6 kIOHIDDigitizerTransducerTypeHand == 35
//iOS7 kIOHIDTransducerTypeHand == 3
IOHIDEventRef handEvent = IOHIDEventCreateDigitizerEvent(kCFAllocatorDefault, timeStamp, kIOHIDTransducerTypeHand, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
//Got on iOS7.
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerDisplayIntegrated, 1, -268435456); //-268435456
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldBuiltIn, 1, -268435456); //-268435456
int handEventMask = 0;
int handEventTouch = 0;
CGFloat rX = 50;
CGFloat rY = 50;
IOHIDEventRef fingerEvent = IOHIDEventCreateDigitizerFingerEventWithQuality(kCFAllocatorDefault, timeStamp,
1, 2, kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch, rX, rY, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0);
IOHIDEventAppendEvent(handEvent, fingerEvent);
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerEventMask, handEventMask, -268435456);
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerRange, handEventTouch, -268435456);
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerTouch, handEventTouch, -268435456);
return handEvent;
}
- (IOHIDEventRef)getupEvent
{
uint64_t abTime = mach_absolute_time();
AbsoluteTime timeStamp = *(AbsoluteTime *) &abTime;
//iOS6 kIOHIDDigitizerTransducerTypeHand == 35
//iOS7 kIOHIDTransducerTypeHand == 3
IOHIDEventRef handEvent = IOHIDEventCreateDigitizerEvent(kCFAllocatorDefault, timeStamp, kIOHIDTransducerTypeHand, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
//Got on iOS7.
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerDisplayIntegrated, 1, -268435456); //-268435456
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldBuiltIn, 1, -268435456); //-268435456
int handEventMask = 0;
int handEventTouch = 0;
CGFloat rX = 50;
CGFloat rY = 50;
IOHIDEventRef fingerEvent = IOHIDEventCreateDigitizerFingerEventWithQuality(kCFAllocatorDefault, timeStamp,
1, 2, kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch, rX, rY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
IOHIDEventAppendEvent(handEvent, fingerEvent);
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerEventMask, handEventMask, -268435456);
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerRange, handEventTouch, -268435456);
IOHIDEventSetIntegerValueWithOptions(handEvent, kIOHIDEventFieldDigitizerTouch, handEventTouch, -268435456);
return handEvent;
}
程序运行以后没有崩溃,但是也没有任何反应…
viewDidLoad里的代码是我自己看H文件找出来的,我也不知道这几句能不能把event发送出去…
getPort方法是在google找的,能返回一个5位数字,这应该是成功获取到port了吧?
生成evet的方法是参照SimulateTouch的源码写的,一头雾水哎…
路过的大神指点一下吧,就想实现个模拟点击,折腾了快俩月了,完全没头绪