16

I'm working on OS X project and i want to programmatically generate Touchpad gesture event like NSEventTypeSwipe or NSEventTypeRotate So I can rotate/zoom etc on other applications.

I found out how to generate mouse/keyboard events but not touchpad.

Any idea?

Gil
  • 371
  • 4
  • 15
  • Anyone has sample code how to implement this gestures generation in macOS? I am trying to use CGEventCreate() and pass Event Types number corresponding to appropriate touch gesture but cannot get it working. – Michał Ziobro Aug 25 '16 at 11:44

4 Answers4

5

There is no public API for generating those events.

You can find some work on synthesizing those events in this project: calftrail/Touch.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanks for the link! I've summarized how that Touch stuff works (worked? might have finally broken on Mavvrx…) over at http://stackoverflow.com/a/6793870/179583 – natevw Dec 09 '13 at 19:16
3

Reference: Cocoa Event Handling Guide

The above Mac Developer Library guide does not state any known methods to programmatically generate Touchpad Gestures.

It goes so far as to say the touchpad gestures themselves occur outside of the OS:

"The trackpad hardware includes built-in support for interpreting common gestures and for mapping movements..."

That guide also explicitly mentions apps should not rely on that sole input mechanism, and it's best to include support for keyboard and mouse for that reason.

Now that Mac and Windows (i.e., Windows 8) are supporting touchscreen monitors at the OS level, it's a matter of time before programmatically touchpad & touchscreen gestures can be incorporated into services like your project or remote desktop control using the appropriate API when it becomes available.

arttronics
  • 9,957
  • 2
  • 26
  • 62
0

I think that touch gesture events cannot be easily generated as there is no public, official Apple API. This NSEventTypeMagnify, NSEventTypeRotate, NSEventTypeSwipe are I think only for read only purposes while handling exitsting system events. Maybe Apple for some reason don't want to make from Magic Mouse a magic touchpad by 3rd party developers. This Project rob mayoff mentioned is corrently not working as apple probably changes something in structure of event data. So relaying on such hacking isn't futuristic. But If you think a little more you could achieve what touch events will do by other means.

  1. magnification (pinch gesture) -> its just zoom in, zoom out -> most programs is using shortcut for this like CMD and +, CMD and -.
  2. rotation it is usable with photos and there is shortcut like CMD and L, CMD and R in preview app.
  3. swiping - changing spaces (desktops) -> use CTRL and arrows <- or ->
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
-1

When I see the xcode simulator, I understand that touch events could be generated programmatically, and that xcode simulator is using those routines and functions to transfer the cursor touches and translate them to touch events, however based on what @robMayoff said, it seems that Apple did not make that library open for public. Emulating the same behavior and creating functions from scratch for that would be a bit challenging.

This is the NSTouch class reference: http://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSTouch_Class/Reference/Reference.html#//apple_ref/occ/cl/NSTouch

Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
  • I'm not the one who downvoted you, but I do want to point out that the iOS simulator translates clicks and drags to touches; it does not require a multi-touch input device, nor does it translate touches on that input device directly to touches on the simulated iOS device. You can only tap on things in the Simulator without pressing the hardware trackpad button if “tap to click” is turned on in the Trackpad pane in System Preferences, and I don't know whether that's available for a Magic Mouse. – Peter Hosey Jan 01 '13 at 02:48
  • @PeterHosey this is what I wanted to say, in the simulator you can simulate two touches with the mouse when clicking `opt/shift` : http://stackoverflow.com/questions/4633908/ipad-simulator-multitouch-input – Mehdi Karamosly Jan 01 '13 at 21:55