我正在使用 Swift 5.7 构建我想用 Wacom Intuos Pro 数位板控制的 macOS 应用程序。
在我运行 macOS 13.0 Ventura 的 Mac Studio 上,Wacom 的 ScribbleDemo 项目(基于 Objective-C)构建并运行良好,他们的应用程序成功检测到平板电脑(并输出一条日志消息“我看到 1 台平板电脑已连接”),并捕获他们应用程序中所有数位板笔的事件流(例如压力、倾斜 X、倾斜 Y、吸收 X、吸收 Y)。这是 GitHub 上的 ScribbleDemo 项目: https://github.com/Wacom-Developer/wacom-device-kit-macos-scribble-demo/tree/master/ScribbleDemo
我已经将 Wacom 项目中的 Wacom 驱动程序文件添加到我的项目中,并使用声明名称文件的 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) {
// Insert code here to initialize your application
self.tabletInfo()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
func tabletInfo() {
let numTablets = WacomTabletDriver.tabletCount()
print("I see \(numTablets) tablets attached")
}
}
我已经确认我的项目能够调用 WacomTabletDriver 类中的函数。
类似于 Wacom 项目,我尝试将以下函数 sendEvent 添加到主应用程序类,但它似乎没有被调用:
func sendEvent(with event: NSEvent) {
print("sendEvent called!")
}
我也尝试过子类化 NSApplication,并使其与 NSApplicationDelegate 的子类共享,但是 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) // Call the superclass method to handle the event
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let application = MyApplication.shared
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
我认为问题与应用程序的结构(NSApplication 和/或 NSApplication)有关,但是我无法从此处的其他帖子和其他文档中解决这个问题。任何建议将不胜感激!
如果您希望 WacomTabletDriver.tabletCount() 返回 1 而不是 0,请将“隐私 - AppleEvents 发送使用说明”添加到 info.plist。在 xxx.entitlements 中将 Sandbox 设置为 NO,并将 Apple Events 设置为 YES。将以下两行添加到 WacomTabletDriver.m 的 +tabletCount 方法中,你就会明白为什么它有效。
reply = [event sendExpectingReplyWithPriority:kAEHighPriority andTimeout:kTabletDriverAETimeout];
NSLog(@"%@",reply);
NSLog(@"%@",[reply descriptorForKeyword:keyDirectObject]);
如果不向 info.plist 添加额外的行,则返回 null。添加额外的行后,它具有正确的值。