一个应用程序的全系统热键
我有3个按钮一个简单的窗口,我尝试添加一个全系统热键,所以我可以preSS,而无需这些按钮切换到该应用程序,preSS一个按钮,然后回去我在做什么。
I have a simple window with 3 buttons and I am trying to add a system-wide hot key so i can "press" those buttons without having to switch to that app, press a button and then go back to what I was doing.
类似 Cmd的骨节病> + 移骨节病> + 1 骨节病> preSS按钮1, Cmd的骨节病> + 移骨节病> + 2 骨节病> preSS按钮2,等等。
Something like Cmd + Shift + 1 press button 1, Cmd + Shift + 2 press button 2, etc.
有什么办法可可实现这一目标(与Objective-C的)?
谢谢,code是AP preciated因为我可可总新手。
Is there any way to achieve this in Cocoa (with Objective-C)? Thanks, code is appreciated since I am a total newbie on Cocoa.
我也不喜欢PTHotKey,所以我最后写一个新的包装,可在这里:
I also didn't like PTHotKey, so I ended up writing a new wrapper, available here:
http://github.com/davedelong/DDHotKey
修改
2档你需要的是:
- DDHotKeyCenter.h
- DDHotKeyCenter.m
和你使用它是这样的:
- (IBAction) registerHotkey:(id)sender {
DDHotKeyCenter * c = [[DDHotKeyCenter alloc] init];
if (![c registerHotKeyWithKeyCode:kVK_ANSI_1 modifierFlags:(NSCommandKeyMask | NSShiftKeyMask) target:self action:@selector(hotkeyWithEvent:) object:nil]) {
NSLog(@"unable to register hotkey");
} else {
NSLog(@"registered hotkey");
}
[c release];
}
- (void) hotkeyWithEvent:(NSEvent *)hkEvent {
NSLog(@"Hotkey event: %@", hkEvent);
}