8

It's possible to simulate touch events on iOS, and you can receive various system wide notifications when in the background using CTTelephonyCenterAddObserver and CFNotificationCenterAddObserver, eg:

I've yet to find a way to get touch notifications while in the background though. Is there a "touch event" that can be used with CFNotificationCenterAddObserver, a different notification center that can be used, or a completely different approach?

I'd be happy with low level touch information (eg. x, y coordinates and touch type), but higher level information (eg. key pressed, back button pressed etc) would be even better!

Community
  • 1
  • 1
Ben Dowling
  • 17,187
  • 8
  • 87
  • 103
  • Perhaps this might help you http://stackoverflow.com/questions/8303235/is-it-possible-to-capture-touch-events-in-the-background-on-a-jailbroken-ios-dev. Also do a search for `GSEvents` – Rog Mar 21 '13 at 01:08

1 Answers1

18

You can use the IOHID stuff in IOKit to get the x, y coordinates.

#include <IOHIDEventSystem.h>

create IOHIDEventSystemClient:

void *ioHIDEventSystem = IOHIDEventSystemClientCreate(kCFAllocatorDefault);

register callback:

IOHIDEventSystemClientScheduleWithRunLoop(ioHIDEventSystem, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOHIDEventSystemClientRegisterEventCallback(ioHIDEventSystem, handle_event, NULL, NULL);

unregister callback:

IOHIDEventSystemClientUnregisterEventCallback(ioHIDEventSystem, handle_event, NULL, NULL);
IOHIDEventSystemClientUnscheduleWithRunLoop(ioHIDEventSystem, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

callback:

void handle_event (void* target, void* refcon, IOHIDServiceRef service, IOHIDEventRef event) {
   if (IOHIDEventGetType(event)==kIOHIDEventTypeDigitizer){
       IOHIDFloat x=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerX);
       IOHIDFloat y=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerY);
       int width = [[UIScreen mainScreen] bounds].size.width;
       int height = [[UIScreen mainScreen] bounds].size.height;
       NSLog(@"click : %f, %f", x*width, y*height) ;
   }
}

Also, you can check this out: IOHIDEventSystemCreate on iOS6 failed. Hope this helps.

EDIT: Please see the result from the log. Tested on iPhone 4 and 5.

void handle_event (void* target, void* refcon, IOHIDServiceRef service, IOHIDEventRef event) {
    NSLog(@"handle_event : %d", IOHIDEventGetType(event));
if (IOHIDEventGetType(event)==kIOHIDEventTypeDigitizer){
    IOHIDFloat x=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerX);
    IOHIDFloat y=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerY);
    NSLog(@" x %f : y %f", x, y);
//2013-03-28 10:02:52.169 MyIOKit[143:907] handle_event : 11
//2013-03-28 10:02:52.182 MyIOKit[143:907]  x 0.766754 : y 0.555023
}
}
Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69
  • Awesome! I'm getting the events, but my x,y values are enormous, eg: 1050201600.000000, 1052667392.000000. Any tips? – Ben Dowling Mar 28 '13 at 00:19
  • Have you checked x and y before multiplying? I am not in front of my Mac now but as far as I remember I got x and y in from of %. Eg if you touch around the top right corner, x = 0.9 and y 0.1. – pt2121 Mar 28 '13 at 02:19
  • Yeah the huge number is the x,y without multiplying. – Ben Dowling Mar 28 '13 at 03:30
  • OK. I'll get back to you when I have a mac. – pt2121 Mar 28 '13 at 04:17
  • On iOS6 I'm seeing the huge values still, eg: x 1064502784.000000 : y 1034551296.000000 :( – Ben Dowling Mar 31 '13 at 03:07
  • @BenDowling This is weird. I did test on iOS6. – pt2121 Mar 31 '13 at 03:13
  • Just downloaded your test project and built that and that does work exactly how you've described, so it must just be a problem with the code in my project - will dig into it. Thanks for all your help! – Ben Dowling Mar 31 '13 at 03:24
  • Good to hear that. Good luck! – pt2121 Mar 31 '13 at 03:25
  • It gives me error that 'IOHIDEventSystem.h' file not found ... any idea ? – Mihir Mehta Apr 11 '13 at 09:54
  • But how can capture multi touch events? – Suge Jul 08 '13 at 06:51
  • Does anybody got the answer for this question? I need to inject system wide touch in iOS8. I have already asked similar question here: http://stackoverflow.com/questions/26915920/ios8-touch-injection-programatically but nobody replied to it. I really need answer asap. – TechFlitter Nov 27 '14 at 22:04