You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift macOS应用中Wacom Intuos平板检测与事件获取问题

Swift开发macOS应用对接Wacom Intuos Pro平板的问题与解决

问题背景

我使用Swift 5.7开发一款macOS应用,需要通过Wacom Intuos Pro平板实现控制。在运行macOS 13.0 Ventura的Mac Studio设备上,Wacom官方的Objective-C版ScribbleDemo项目可以正常构建运行,能成功检测到平板(输出日志"I see 1 tablets attached"),并完整捕获笔的事件流(包括压力、倾斜X/Y、绝对坐标X/Y等数据)。

我已将该项目中的驱动文件添加到自己的项目中,并通过Objective-C桥接文件引入相关头文件:

#import "NSAppleEventDescriptorHelperCategory.h"
#import "TabletAEDictionary.h"
#import "TabletEvents.h"
#import "Wacom.h"
#import "WacomTabletDriver.h"

尝试实现与遇到的问题

我复刻官方项目的核心逻辑,调用WacomTabletDriver.tabletCount()检测平板,但返回结果始终为0,代码如下:

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        self.tabletInfo()
    }
    func applicationWillTerminate(_ aNotification: Notification) {
        // 应用终止时的清理逻辑
    }
    func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
        return true
    }
    func tabletInfo() {
        let numTablets = WacomTabletDriver.tabletCount()
        print("I see \(numTablets) tablets attached")
    }
}

确认项目可以正常调用WacomTabletDriver类的方法后,我尝试在主应用类中添加sendEvent方法,但该方法从未被触发:

func sendEvent(with event: NSEvent) {
    print("sendEvent called!")
}

随后我尝试子类化NSApplication并关联代理,sendEvent方法仍无日志输出,平板数量的检测日志也未打印:

import Cocoa

class MyApplication: NSApplication {
    override init() {
        super.init()
        let numTablets = WacomTabletDriver.tabletCount()
        print("I see \(numTablets) tablets attached")
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override func sendEvent(_ event: NSEvent) {
        print("sendEvent is called!")
        super.sendEvent(event) // 调用父类方法处理事件
    }
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let application = MyApplication.shared
    }
    func applicationWillTerminate(_ aNotification: Notification) {
        // 应用终止时的清理逻辑
    }
}

我判断问题与应用结构(NSApplication相关)有关,但始终找不到解决方案,恳请提供建议。

问题解决更新

在他人建议下,我成功检测到Wacom平板。针对通过NSEvent收发平板数据的问题,根据Wacom官方支持的要求,需在项目配置中添加以下权限:

<key>com.apple.security.automation.apple-events</key>
<true/>
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
    <string>com.wacom.TabletDriver</string>
</array>

现在,添加相同的Wacom驱动文件、桥接文件内容后,在NSViewController中加入以下代码即可正常获取平板事件数据:

func handleMouseEvent(with event: NSEvent) {

    if (event.isTabletPointerEvent()) {
        let eventType = Int(event.type.rawValue)
        var mPressure: Float = 0.0
      
        if eventType != NSEvent.EventType.mouseMoved.rawValue {
            mPressure = event.pressure;
        } else {
            mPressure = 0.0
        }
      
        let loc = event.locationInWindow
        let mMouseX = loc.x
        let mMouseY = loc.y
        let mTabletScaledPressure = event.pressure
        let mAbsX = Int32(event.absoluteX)
        let mAbsY = Int32(event.absoluteY)
        let tilt = event.tilt
        let mTiltX = tilt.x
        let mTiltY = tilt.y
        let mRotDeg = event.rotation
        let mRotRad = event.rotationInRadians()
      
        print("x:", mMouseX, "y:", mMouseY, "xAbs:", mAbsX, "yAbs:", mAbsY,
            "p:", mPressure, "pScaled:", mTabletScaledPressure,
            "tX:", mTiltX, "tY:", mTiltY, "mRotDeg:", mRotDeg, "mRotRad", mRotRad)
    }
}

override func mouseDown(with event: NSEvent) {
    handleMouseEvent(with: event)
}

override func mouseDragged(with event: NSEvent) {
    handleMouseEvent(with: event)
}

override func mouseUp(with event: NSEvent) {
    handleMouseEvent(with: event)
}

内容的提问来源于stack exchange,提问作者JSN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 16:44:59