这可能是你正在寻找的东西
https://github.com/jaz303/JFHotkeyManager
你也可以看看苹果公司提供的这个示例,使用RegisterEventHotKey API调用,我认为这将指引你朝着正确的方向。
https://developer.apple.com/library/prerelease/mac/samplecode/FunkyOverlayWindow/Listings/FunkyOverlayWindow_OverlayWindow_m.html#//apple_ref/doc/uid/DTS10000391-FunkyOverlayWindow_OverlayWindow_m-DontLinkElementID_8
或者你可以尝试这段代码
#import <Carbon/Carbon.h>
EventHandlerUPP hotKeyFunction;
pascal OSStatus hotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData)
{
Notify *obj = userData;
[obj foo];
return noErr;
}
@implementation Notify
- (id)init
{
self = [super init];
if (self) {
hotKeyFunction = NewEventHandlerUPP(hotKeyHandler);
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyReleased;
InstallApplicationEventHandler(hotKeyFunction,1,&eventType,self,NULL);
UInt32 keyCode = 80;
EventHotKeyRef theRef = NULL;
EventHotKeyID keyID;
keyID.signature = 'FOO ';
keyID.id = 1;
RegisterEventHotKey(keyCode,0,keyID,GetApplicationEventTarget(),0,&theRef);
}
return self;
}
- (void)foo
{
}
@end
和标题
#include "notify.mm"
@interface Notify
- (id)init;
- (void)foo;
@end
简单来说,这只是一个带有方法和构造函数的对象,在Objective-C中称为init或initialize及其变体。使用“new”直接调用即可。
E.x
#include "notify.h"
int main(){
Notify* object = new Notify();
}
然而,需要一些基本的Objective-C理解。在我看来,这主要是语法上的差异。但我自己并不是Objective-C专家。无论如何,有很多方法可以解决它,这可能不是最好的想法。您还可以从自己的C++类中调用Objective-C代码。请查看下面的链接,了解如何实现。
https://el-tramo.be/blog/mixing-cocoa-and-qt/
https://github.com/remko/mixing-cocoa-and-qt/
http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++
hotkey.h
和hotkey.mm
文件。在.mm
文件中,我添加了你粘贴的代码。 现在,由于我不理解提供的Objective-C代码,我需要在我的.h
文件中添加什么?你能否提供一个更详细的答案,并分别给出头文件和.mm
文件的内容? - Philipp Meissner