プロジェクトの準備
- File -> New -> Project -> macOS -> Cocoa App でプロジェクトを作る
- /System/Library/PrivateFrameworks/MultitouchSupport.framework を
Embedded Binaries
にCopy items if neededで追加する(同時にLinked Frameworks and Libraries
に追加される) - M5MultitouchSupportをcloneしてビルドし,生成されたFrameworksをMultitouchSupport.frameworkと同様の手順でプロジェクトに追加する(podを使っても良い)
-
TARGETS -> Capabilities -> App SandBox をOFF
にする
~.entitlements
にKeycom.apple.security.temporary-exception.sbpl
を加えて,Valueに(allow iokit-open)
を記入する
コーディング(最低限)
ViewController.swift
import Cocoa
import M5MultitouchSupport
class ViewController: NSViewController {
var manager: M5MultitouchManager! = nil
var listener: M5MultitouchListener! = nil
override func viewDidLoad() {
super.viewDidLoad()
manager = M5MultitouchManager.shared()
listener = manager.addListener(callback: { [weak self] (event) in
if let e = event, let touches = e.touches as NSArray as? [M5MultitouchTouch] {
DispatchQueue.main.async {
self?.process(touches)
}
}
})
}
override func viewWillDisappear() {
super.viewWillDisappear()
listener.listening = false
}
override var representedObject: Any? {
didSet {
}
}
func process(_ touches: [M5MultitouchTouch]) {
var currentInfo: String = "["
currentInfo += touches.map({ (touch) -> String in
return String(format: "ID: %d, state: %@, Position(x, y) = (%0.4f, %0.4f), Velocity(x, y) = (%+0.4f, %+0.4f), Size: %0.4f, Axis(major, minor) = (%0.4f, %0.4f), Angle: %0.4f",
touch.identifier,
stateString(touch.state),
touch.posX,
touch.posY,
touch.velX,
touch.velY,
touch.size,
touch.majorAxis,
touch.minorAxis,
touch.angle)
}).joined(separator: "\n ")
currentInfo += "]"
Swift.print(currentInfo)
}
func stateString(_ state: M5MultitouchTouchState) -> String {
switch state {
case .notTouching: return "notTouching"
case .starting: return "starting"
case .hovering: return "hovering"
case .making: return "making"
case .touching: return "touching"
case .breaking: return "breaking"
case .lingering: return "lingering"
case .leaving: return "leaving"
default: return "unknown"
}
}
}
取得できるデータ
11点までタッチ点を取得可能
また,タッチ点それぞれについて,
- ID
- 接触状態
- 絶対座標(範囲はタッチパッド領域の上下・左右それぞれに対して0~1)
- 移動速度
- 長径と短径(タッチ点を楕円とみなしている)
- 長軸の傾き
が取得可能