我想在iPhone中注入触摸事件.我通过网络套接字获取触摸事件的坐标. GSSendEvent似乎是不错的选择.但是,它需要GSEventRecord作为输入之一.
有谁知道如何准备GSEventRecord?我根据一些例子准备了它,但是在GSSendEvent调用之后应用程序崩溃了.
感谢任何帮助.
-(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.然后,您可以使用从How to find the purple port for the front most application in IOS 5 and above?获得的端口尝试以下代码.
我不是iOS专家,Apple也没有提供任何文档,所以我无法解释这里发生了什么. (它恰好适合我.)
无论如何,你可以使用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);
要使用此代码,您必须多次调用它.对于一个水龙头,有触摸,触摸拖动,然后触摸.
另请注意,触摸启动时pathProximity为0.
据我记忆,winRef并不重要.
希望这可以帮助.
编辑:从Bugivore的评论来看,问题是:
The way I allocated touchEvent via malloc was wrong. It should be done as EntryLevelDev showed – “static uint8_t handJob[sizeof(GSEventRecord) + sizeof(GSHandInfo) + sizeof(GSPathInfo)];”