📜 ⬆️ ⬇️

Multi-touch programming on Mac OS X

On the new MacBook, MacBookPro and MacBookAir there are these types of gestures (gestures):

two fingers - scroll
two fingers - magnify
two fingers - rotate

three fingers - vertical swipe
three fingers - swipe horizontal
')
four fingers - vertical swipe
four fingers - swipe horizontal

How to use these gestures is shown in detail in System Preferences-> TrackPad.

Documentation on multitouch, generally speaking, closed. They promised to open it in 10.6.
Below - in brief, what happened to find on the Internet and learn from their experiments.

The essence is as follows:
in the heir of NSResponder (in NSView or in NSWindow, as you like) we add the following functions (or some of them):

- (void)beginGestureWithEvent:(NSEvent*) anEvent; //
- (void)endGestureWithEvent:(NSEvent*) anEvent; //
- (void)magnifyWithEvent:(NSEvent*) anEvent; //
- (void)rotateWithEvent:(NSEvent*) anEvent; //
- (void)swipeWithEvent:(NSEvent*) anEvent; //


Read more about these features.

- (void)beginGestureWithEvent:(NSEvent*) anEvent;
It is called at the beginning of any gesture, including scrolling with two fingers, with the exception of gestures with four fingers, which are processed by the system and “do not let go” into the application. I didn’t work out what kind of gesture I’ve started

- (void)endGestureWithEvent:(NSEvent*) anEvent;
called at the end of any gesture (again, except for the four-toed fingers).

- (void)magnifyWithEvent:(NSEvent*) anEvent; //
In this function, the main parameter is [anEvent deltaZ] . It must be multiplied by a suitable coefficient.
In my program, the coefficient = 1000.

- (void)rotateWithEvent:(NSEvent*) anEvent; //
Main parameter: [anEvent rotation] . The angle of rotation is measured in degrees, the positive direction is counterclockwise.

- (void)swipeWithEvent:(NSEvent*) anEvent; //
This function works both in vertical and horizontal swipe.
The main parameters [anEvent deltaX] and [anEvent deltaY] can be 1.0 and -1.0, depending on the direction of the swipe.

There is a small problem, which is that when you start to do rotate, it can work and magnify. Therefore, it is necessary to create some kind of flag, which at the beginning of rotate blocks magnify, and vice versa.
The preview is most likely done in the same way.

The gestures of four fingers, as already written, intercepts the system and, apparently, the application cannot process them.

Everything is described in great detail here:
http://cocoadex.com/2008/02/nsevent-modifications-swipe-ro.html
http://www.cocoadev.com/index.pl?MultiTouchTrackpad

UPDATE.
As pointed out to himmelherz , four fingers work fine on not the newest MacBookPro too. Included by replacing AppleUSBMultitouch.kext.
Details here http://forums.macrumors.com/showthread.php?t=582801&page=8

Source: https://habr.com/ru/post/52699/


All Articles