GSSendEvent-注入触摸事件iOS
我想在iPhone中注入触摸事件。我通过网络插座获取触摸事件的坐标。GSSendEvent似乎是个不错的选择。但是,它需要GSEventRecord作为输入之一 有人知道如何准备GSEventRecord吗?我根据一些示例编写了它,但在GSSendEvent调用后应用程序崩溃 谢谢你的帮助GSSendEvent-注入触摸事件iOS,ios,api,touch,private,iphone-privateapi,Ios,Api,Touch,Private,Iphone Privateapi,我想在iPhone中注入触摸事件。我通过网络插座获取触摸事件的坐标。GSSendEvent似乎是个不错的选择。但是,它需要GSEventRecord作为输入之一 有人知道如何准备GSEventRecord吗?我根据一些示例编写了它,但在GSSendEvent调用后应用程序崩溃 谢谢你的帮助 -(void) handleMouseEventAtPoint:(CGPoint) point { static mach_port_t port_; // structure of touch GSEve
-(void) handleMouseEventAtPoint:(CGPoint) point
{
static mach_port_t port_;
// structure of touch GSEvent
struct GSTouchEvent {
GSEventRecord record;
GSHandInfo handInfo;
} ;
struct GSTouchEvent *touchEvent = (struct GSTouchEvent *) malloc(sizeof(struct GSTouchEvent));
bzero(touchEvent, sizeof(touchEvent));
// set up GSEvent
touchEvent->record.type = kGSEventHand;
touchEvent->record.windowLocation = point;
touchEvent->record.timestamp = GSCurrentEventTimestamp();
touchEvent->record.infoSize = sizeof(GSHandInfo) + sizeof(GSPathInfo);
touchEvent->handInfo.type = getHandInfoType(0, 1);
touchEvent->handInfo.pathInfosCount = 1;
bzero(&touchEvent->handInfo.pathInfos[0], sizeof(GSPathInfo));
touchEvent->handInfo.pathInfos[0].pathIndex = 1;
touchEvent->handInfo.pathInfos[0].pathIdentity = 2;
touchEvent->handInfo.pathInfos[0].pathProximity = 1 ? 0x03 : 0x00;
touchEvent->handInfo.pathInfos[0].pathLocation = point;
port_ = GSGetPurpleSystemEventPort();
GSSendEvent((GSEventRecord*)touchEvent ,port_);
}
static GSHandInfoType getHandInfoType(int touch_before, int touch_now){
if (!touch_before) {
return (GSHandInfoType) kGSHandInfoType2TouchDown;
}
if (touch_now) {
return (GSHandInfoType) kGSHandInfoType2TouchChange;
}
return (GSHandInfoType) kGSHandInfoType2TouchFinal;
}
仅在iOS 6上测试 你实际上走在正确的轨道上。问题是你必须弄清楚你应该给这些变量分配什么值 首先,您需要导入GraphicsServices.h。然后,您可以使用可以从中获取的端口尝试以下代码 我不是iOS专家,苹果也没有提供任何文档,所以我无法解释这里发生了什么。(这对我来说正好行。) 无论如何,您可以使用xcode调试模式来查看引擎盖下发生了什么
struct GSTouchEvent * touchEvent = (struct GSTouchEvent*) &gsTouchEvent;
bzero(touchEvent, sizeof(touchEvent));
touchEvent->record.type = kGSEventHand;
touchEvent->record.subtype = kGSEventSubTypeUnknown;
touchEvent->record.location = point;
touchEvent->record.windowLocation = point;
touchEvent->record.infoSize = sizeof(GSHandInfo) + sizeof(GSPathInfo);
touchEvent->record.timestamp = GSCurrentEventTimestamp();
touchEvent->record.window = winRef;
touchEvent->record.senderPID = 919;
bzero(&touchEvent->handInfo, sizeof(GSHandInfo));
bzero(&touchEvent->handInfo.pathInfos[0], sizeof(GSPathInfo));
GSHandInfo touchEventHandInfo;
touchEventHandInfo._0x5C = 0;
touchEventHandInfo.deltaX = 0;
touchEventHandInfo.deltaY = 0;
touchEventHandInfo.height = 0;
touchEventHandInfo.width = 0;
touchEvent->handInfo = touchEventHandInfo;
touchEvent->handInfo.type = handInfoType;
touchEvent->handInfo.deltaX = 1;
touchEvent->handInfo.deltaY = 1;
touchEvent->handInfo.pathInfosCount = 0;
touchEvent->handInfo.pathInfos[0].pathIndex = 1;
touchEvent->handInfo.pathInfos[0].pathIdentity = 2;
touchEvent->handInfo.pathInfos[0].pathProximity = (handInfoType == kGSHandInfoTypeTouchDown || handInfoType == kGSHandInfoTypeTouchDragged || handInfoType == kGSHandInfoTypeTouchMoved) ? 0x03: 0x00;
touchEvent->handInfo.x52 = 1;
touchEvent->handInfo.pathInfos[0].pathLocation = point;
touchEvent->handInfo.pathInfos[0].pathWindow = winRef;
GSEventRecord* record = (GSEventRecord*) touchEvent;
record->timestamp = GSCurrentEventTimestamp();
GSSendEvent(record, port);
要使用此代码,必须多次调用它。对于一次轻触,有向下触碰、拖动触碰和向上触碰
还请注意,当触碰启动时,PathApproxity为0
就我所记得的,winRef并不重要
希望这有帮助
编辑:根据Bugivore的评论,问题是:
我通过malloc分配touchEvent的方式是错误的。应该按照EntryLevelDev显示的那样完成——“静态uint8_t手工作业[sizeof(GSEventRecord)+sizeof(GSHandInfo)+sizeof(GSPathInfo)]
EntryLevelDev的答案是正确的,但其中一些值并不重要。我从其他地方获得了下面的代码,并做了一些尝试和错误,下面是我的代码(一直为最新的ios6工作) 现在有人在为IOS7工作吗?我无法让它工作。请看我的帖子:
嗨,埃尔德-谢谢你的帮助。我试过了,但它在objc_msgSend中崩溃了。好的,请查看我在github上的项目…工作得很好!非常感谢。我通过malloc分配touchEvent的方式是错误的。应该按照EntryLevelDev所展示的那样做——“静态uint8_t手工作业[sizeof(GSEventRecord)+sizeof(GSHandInfo)+sizeof(GSPathInfo)]”,但是如何模拟多点触控事件呢?太棒了。很高兴知道。ThxNo,不适用于Graphicsservices。我们正在使用另一种方法来模拟用户触摸。嗨,user2485972,你能分享一些关于你的ios 7触摸注入的信息吗?user2485972想回答吗?@user2485972
由于ios 7上的系统智能触摸是可能的,我的答案是否定的
——你是说你不知道在ios 7上模拟系统范围触摸事件的方法吗?
static int prev_click = 0;
if (!click && !prev_click)
{
//which should never enter
NSLog(@"***error, postHandEvent cancel");
return;
}
CGPoint location = CGPointMake(x, y);
struct GSTouchEvent {
GSEventRecord record;
GSHandInfo handInfo;
} * event = (struct GSTouchEvent*) &touchEvent;
bzero(touchEvent, sizeof(touchEvent));
event->record.type = kGSEventHand;
event->record.windowLocation = location;
event->record.timestamp = GSCurrentEventTimestamp();
//NSLog(@"Timestamp GSCurrentEventTimestamp: %llu",GSCurrentEventTimestamp());
event->record.infoSize = sizeof(GSHandInfo) + sizeof(GSPathInfo);
event->handInfo.type = getHandInfoType(prev_click, click);
//must have the following line
event->handInfo.x52 = 1;
//below line is for ios4
//event->handInfo.pathInfosCount = 1;
bzero(&event->handInfo.pathInfos[0], sizeof(GSPathInfo));
event->handInfo.pathInfos[0].pathIndex = 2;
//following 2 lines, they are by default
event->handInfo.pathInfos[0].pathMajorRadius = 1.0;
event->handInfo.pathInfos[0].pathPressure = 1.0;
//event->handInfo.pathInfos[0].pathIdentity = 2;
event->handInfo.pathInfos[0].pathProximity = click ? 0x03 : 0x00;
//event->handInfo.pathInfos[0].pathProximity = action;
event->handInfo.pathInfos[0].pathLocation = location;
// send GSEvent
GSEventRecord *event1 = (GSEventRecord*) event;
sendGSEvent(event1);