macOS 开发交流 秋秋群:644096295,V : ez-code
MASShortcut 是一款快捷键管理工具,替代和兼容 ShortcutRecorder
单独使用一个 MASShortcutView
你可以修改 它的属性,来改变显示的样式
MASShortcutView *shortCutView = [[MASShortcutView alloc] initWithFrame:NSMakeRect(10, 10, 200, 50)];
[self.window.contentView addSubview:shortCutView];
shortCutView.wantsLayer = YES;
shortCutView.layer.backgroundColor = [NSColor blueColor].CGColor;
MASShortcutValidator.m
- (BOOL) isShortcutAlreadyTakenBySystem:(MASShortcut *)shortcut explanation: (NSString**) explanation
{
CFArrayRef globalHotKeys;
if (CopySymbolicHotKeys(&globalHotKeys) == noErr) {
// Enumerate all global hotkeys and check if any of them matches current shortcut
for (CFIndex i = 0, count = CFArrayGetCount(globalHotKeys); i < count; i++) {
CFDictionaryRef hotKeyInfo = CFArrayGetValueAtIndex(globalHotKeys, i);
CFNumberRef code = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyCode);
CFNumberRef flags = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyModifiers);
CFNumberRef enabled = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyEnabled);
if (([(__bridge NSNumber *)code integerValue] == [shortcut keyCode]) &&
([(__bridge NSNumber *)flags unsignedIntegerValue] == [shortcut carbonFlags]) &&
([(__bridge NSNumber *)enabled boolValue])) {
if (explanation) {
*explanation = MASLocalizedString(@"This combination cannot be used because it is already used by a system-wide "
@"keyboard shortcut.\nIf you really want to use this key combination, most shortcuts "
@"can be changed in the Keyboard & Mouse panel in System Preferences.",
@"Message for alert when shortcut is already used by the system");
}
return YES;
}
}
CFRelease(globalHotKeys);
}
return [self isShortcut:shortcut alreadyTakenInMenu:[NSApp mainMenu] explanation:explanation];
}
CopySymbolicHotKeys
来自 Carbon – HiToolbox – CarbonEvents.h
- (void) handleEvent: (EventRef) event
{
if (GetEventClass(event) != kEventClassKeyboard) {
return;
}
EventHotKeyID hotKeyID;
OSStatus status = GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID);
if (status != noErr || hotKeyID.signature != MASHotKeySignature) {
return;
}
[_hotKeys enumerateKeysAndObjectsUsingBlock:^(MASShortcut *shortcut, MASHotKey *hotKey, BOOL *stop) {
if (hotKeyID.id == [hotKey carbonID]) {
if ([hotKey action]) {
dispatch_async(dispatch_get_main_queue(), [hotKey action]);
}
*stop = YES;
}
}];
}
https://music.163.com/#/song?id=865632948
伊织 2023-12-22