GSSendEvent - 注入触摸事件的iOS [英] GSSendEvent - Inject Touch Event iOS

查看:2091
本文介绍了GSSendEvent - 注入触摸事件的iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone注入触摸事件。我经由网络套接字获得触摸事件的坐标。 GSSendEvent似乎是不错的选择。然而,它需要GSEventRecord作为一个输入。

有谁知道如何prepare GSEventRecord?我prepared它基于一些例子,但GSSendEvent调用后的应用程序崩溃。

鸭preciate任何帮助。

   - (无效)handleMouseEventAtPoint:(CGPoint)点
{
静态mach_port_t port_;//触控结构GSEvent
结构GSTouchEvent {
    GSEventRecord记录;
    GSHandInfo handInfo;
};结构GSTouchEvent *的TouchEvent =(结构GSTouchEvent *)malloc的(的sizeof(结构GSTouchEvent));bzero(的TouchEvent,sizeof的(的TouchEvent));//设置GSEvent
touchEvent-> record.type = kGSEventHand;
touchEvent-> record.windowLocation =点;
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? ×03:0×00;
touchEvent-> handInfo.pathInfos [0] = .pathLocation点;port_ = GSGetPurpleSystemEventPort();GSSendEvent((GSEventRecord *)的TouchEvent,port_);
}
静态GSHandInfoType getHandInfoType(INT touch_before,诠释touch_now){
如果(!touch_before){
    返回(GSHandInfoType)kGSHandInfoType2TouchDown;
}
如果(touch_now){
    返回(GSHandInfoType)kGSHandInfoType2TouchChange;
}
返回(GSHandInfoType)kGSHandInfoType2TouchFinal;
}


解决方案

仅在iOS 6测试

您实际上是在正确的轨道上。问题是你要搞清楚什么值,你应该分配给这些变量。

首先,您需要导入GraphicsServices.h。然后,你可以尝试用端口的以下code,你可以从<一个得到href=\"http://stackoverflow.com/questions/16156831/how-to-find-the-purple-port-for-the-front-most-application-in-ios-5-and-above/16157532#16157532\">How寻找紫端口前大多数应用在iOS 5及以上?。

我不是专家的iOS和苹果没有提供任何文件,所以我不能解释很多东西是怎么回事。 (它发生在我很好地工作。)

无论如何,你可以用它使用X code调试模式看引擎盖下会发生什么发挥。

 结构GSTouchEvent *的TouchEvent =(结构GSTouchEvent *)及gsTouchEvent;
bzero(的TouchEvent,sizeof的(的TouchEvent));
touchEvent-&GT; record.type = kGSEventHand;
touchEvent-&GT; record.subtype = kGSEventSubTypeUnknown;
touchEvent-&GT; record.location =点;
touchEvent-&GT; record.windowLocation =点;
touchEvent-&GT; record.infoSize = sizeof的(GSHandInfo)+的sizeof(GSPathInfo);
touchEvent-&GT; record.timestamp = GSCurrentEventTimestamp();
touchEvent-&GT; record.window = winRef;
touchEvent-&GT; record.senderPID = 919;
bzero(安培; touchEvent-&GT; handInfo,sizeof的(GSHandInfo));
bzero(安培; touchEvent-&GT; handInfo.pathInfos [0],sizeof的(GSPathInfo));
GSHandInfo touchEventHandInfo;
touchEventHandInfo._0x5C = 0;
touchEventHandInfo.deltaX = 0;
touchEventHandInfo.deltaY = 0;
touchEventHandInfo.height = 0;
touchEventHandInfo.width = 0;
touchEvent-&GT; handInfo = touchEventHandInfo;
touchEvent-&GT; handInfo.type = handInfoType;
touchEvent-&GT; handInfo.deltaX = 1;
touchEvent-&GT; handInfo.deltaY = 1;
touchEvent-&GT; handInfo.pathInfosCount = 0;
touchEvent-&GT; handInfo.pathInfos [0] .pathIndex = 1;
touchEvent-&GT; handInfo.pathInfos [0] .pathIdentity = 2;
touchEvent-&GT; handInfo.pathInfos [0] .pathProximity =(handInfoType == kGSHandInfoTypeTouchDown || handInfoType == kGSHandInfoTypeTouchDragged || handInfoType == kGSHandInfoTypeTouchMoved)? ×03:0×00;
touchEvent-&GT; handInfo.x52 = 1;
touchEvent-&GT; handInfo.pathInfos [0] = .pathLocation点;
touchEvent-&GT; handInfo.pathInfos [0] .pathWindow = winRef;
GSEventRecord *记录=(GSEventRecord *)的TouchEvent;
录音&GT;时间戳= GSCurrentEventTimestamp();
GSSendEvent(记录,口);

要使用code,你必须调用它多次。对于一个水龙头,有向下触摸,触摸拖动,然后触摸起来。

另外请注意,pathProximity为0时,触摸到了。

据我记得,在winRef无所谓。

希望这有助于。

编辑:从Bugivore的评论,问题是:


  

我通过的malloc分配的TouchEvent的方式是错误的。它应作为EntryLevelDev显示 - 静态uint8_t有灰机[的sizeof(GSEventRecord)+的sizeof(GSHandInfo)+的sizeof(GSPathInfo);


I want to inject touch event in iPhone. I get the coordinates of touch event via network socket. GSSendEvent seems to be good choice. However, it needs GSEventRecord as one of the inputs.

Does anyone know how to prepare GSEventRecord? I prepared it based on some examples but the app crashes after GSSendEvent call.

Appreciate any help.

-(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;
}

解决方案

Only tested on iOS 6

You are actually on the right track. The problem is you have to figure out what values you should assign to these variables.

First of all, you need to import GraphicsServices.h. Then, you can try the following code with the port which you can get from How to find the purple port for the front most application in IOS 5 and above?.

I am not an iOS expert and Apple doesn't provide any documentation so I can't explain much what's going on here. (It happens to work fine for me.)

Anyway, you can play with it using xcode debug mode to see what happens under the hood.

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);

To use this code, you have to call it multiple times. For one tap, there are touch-down, touch-drag and then touch-up.

Also note that pathProximity is 0 when touch is up.

As far as I remember, the winRef doesn't matter.

Hope this helps.

Edit: From Bugivore's comment, the problem is:

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)];"

这篇关于GSSendEvent - 注入触摸事件的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆