我正在努力将PTHotKeyLib修改为 64 位友好,但我在代码中遇到了一个问题,我不确定如何解决。在 PTHotKeyCenter 中, registerHotKey 方法创建一个 EventHotKeyID 实例,然后将 PTHotKey 对象填充到 id 属性中。原代码用了很长。我根据 Apple 的 64 位编程指南将其转换为 NSInteger。
- (BOOL)registerHotKey:(PTHotKey *)theHotKey {
OSStatus error;
EventHotKeyID hotKeyID;
EventHotKeyRef carbonHotKey;
NSValue *key = nil;
if ([[self allHotKeys] containsObject:theHotKey])
[self unregisterHotKey:theHotKey];
if (![[theHotKey keyCombo] isValidHotKeyCombo])
return YES;
hotKeyID.signature = kHotKeySignature;
hotKeyID.id = (NSInteger)theHotKey;
... //Rest is not relevant
}
当用户触发热键时,它会调用 sendCarbonEvent: 方法,该方法将尝试将 PTHotKey 实例从 EventHotKeyID 中拉出。它在 32 位域中工作,但是在针对 64 位进行编译时,它会给出“从不同大小的整数转换为指针”的警告
- (OSStatus)sendCarbonEvent:(EventRef)event {
OSStatus error;
EventHotKeyID hotKeyID;
SGHotKey *hotKey;
NSAssert(GetEventClass(event) == kEventClassKeyboard, @"Unknown event class");
error = GetEventParameter(event,
kEventParamDirectObject,
typeEventHotKeyID,
nil,
sizeof(EventHotKeyID),
nil,
&hotKeyID);
if (error)
return error;
NSAssert(hotKeyID.signature == kHotKeySignature, @"Invalid hot key id" );
NSAssert(hotKeyID.id != 0, @"Invalid hot key id");
hotKey = (SGHotKey *)hotKeyID.id; // warning: cast to pointer from integer of different size
// Omitting the rest of the code
}
从 x86_64 切换回 i386 会删除警告,并且所有内容都已编译并正常运行。在 x86_64 下它会导致崩溃,我不确定如何解决这个问题。关于如何解决它的任何建议?