forked from flit/MidiKeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiKeysApplication.m
76 lines (66 loc) · 1.68 KB
/
MidiKeysApplication.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// MidiKeysApplication.m
// MidiKeys
//
// Created by Chris Reed on Sun Oct 27 2002.
// Copyright (c) 2002 Chris Reed. All rights reserved.
//
#import "MidiKeysApplication.h"
enum {
// NSEvent subtypes for hotkey events (undocumented).
kEventHotKeyPressedSubtype = 6,
kEventHotKeyReleasedSubtype = 9
};
static struct {
BOOL valid;
BOOL pressed;
BOOL released;
} cachedDelegateHandles = { NO, NO, NO };
@implementation MidiKeysApplication
- (void)sendEvent:(NSEvent *)theEvent
{
if ([theEvent type] == NSSystemDefined)
{
// cache the results of these -respondsToSelector: messages so
// we handle hot keys as fast as possible (even though it's only
// a few method calls, it does affect the note on latency)
if (!cachedDelegateHandles.valid)
{
if ([self delegate])
{
cachedDelegateHandles.pressed = [[self delegate] respondsToSelector:@selector(hotKeyPressed:)];
cachedDelegateHandles.released = [[self delegate] respondsToSelector:@selector(hotKeyReleased:)];
}
else
{
cachedDelegateHandles.pressed = NO;
cachedDelegateHandles.released = NO;
}
cachedDelegateHandles.valid = YES;
}
// pass the hot key event on to the delegate.
switch ([theEvent subtype])
{
case kEventHotKeyPressedSubtype:
if (cachedDelegateHandles.pressed)
{
[[self delegate] hotKeyPressed:[theEvent data1]];
}
break;
case kEventHotKeyReleasedSubtype:
if (cachedDelegateHandles.released)
{
[[self delegate] hotKeyReleased:[theEvent data1]];
}
break;
}
}
[super sendEvent:theEvent];
}
// invalidate the cache
- (void)setDelegate:(id)delegate
{
cachedDelegateHandles.valid = NO;
[super setDelegate:delegate];
}
@end