Wenn Sie in den Voreinstellungen darauf zugreifen möchten, verwenden Sie die Vorlage "Voreinstellungsfenster". Wenn Sie es in der Statusleiste möchten, erstellen Sie eine normale Anwendung, legen Sie den LSUIElement-Schlüssel in Info.plist auf 1 fest, und verwenden Sie NSStatusItem, um das Element zu erstellen.
Um Verknüpfungen global zu erfassen, müssen Sie auch das Carbon-Framework einschließen. Verwenden Sie RegisterEventHotKey
und UnregisterEventHotKey
, um sich für die Ereignisse zu registrieren.
OSStatus HotKeyEventHandlerProc(EventHandlerCallRef inCallRef, EventRef ev, void* inUserData) {
OSStatus err = noErr;
if(GetEventKind(ev) == kEventHotKeyPressed) {
[(id)inUserData handleKeyPress];
} else if(GetEventKind(ev) == kEventHotKeyReleased) {
[(id)inUserData handleKeyRelease];
} else err = eventNotHandledErr;
return err;
}
//EventHotKeyRef hotKey; instance variable
- (void)installEventHandler {
static BOOL installed = NO;
if(installed) return;
installed = YES;
const EventTypeSpec hotKeyEvents[] = {{kEventClassKeyboard,kEventHotKeyPressed},{kEventClassKeyboard,kEventHotKeyReleased}};
InstallApplicationEventHandler(NewEventHandlerUPP(HotKeyEventHandlerProc),GetEventTypeCount(hotKeyEvents),hotKeyEvents,(void*)self,NULL);
}
- (void)registerHotKey {
[self installEventHandler];
UInt32 virtualKeyCode = ?; //The virtual key code for the key
UInt32 modifiers = cmdKey|shiftKey|optionKey|controlKey; //remove the modifiers you don't want
EventHotKeyID eventID = {'abcd','1234'}; //These can be any 4 character codes. It can be used to identify which key was pressed
RegisterEventHotKey(virtualKeyCode,modifiers,eventID,GetApplicationEventTarget(),0,(EventHotKeyRef*)&hotKey);
}
- (void)unregisterHotKey {
if(hotkey) UnregisterEventHotKey(hotKey);
hotKey = 0;
}
- (void)handleHotKeyPress {
//handle key press
}
- (void)handleHotKeyRelease {
//handle key release
}
+1 nette antwort :) –