10 #ifndef NS_RETURNS_INNER_POINTER
11 #define NS_RETURNS_INNER_POINTER
13 #include <AppKit/AppKit.h>
20 "title" :
"VuoKeyboard",
70 NSWindow *targetWindow = (NSWindow *)windowRef;
71 if (! targetWindow || targetWindow == [event window] || [[targetWindow contentView] isInFullScreenMode])
74 [event modifierFlags],
79 NSString *unicodeString = [NSString stringWithUTF8String:unicodeBytes];
81 unsigned long long flags = [event modifierFlags];
84 if(flags & NSCommandKeyMask) modifiers |= VuoModifierKey_Command;
85 if(flags & NSAlternateKeyMask) modifiers |= VuoModifierKey_Option;
86 if(flags & NSControlKeyMask) modifiers |= VuoModifierKey_Control;
87 if(flags & NSShiftKeyMask) modifiers |= VuoModifierKey_Shift;
89 for (NSUInteger i = 0; i < [unicodeString length]; ++i)
92 NSString *characterAsString = [unicodeString substringWithRange:NSMakeRange(i, 1)];
94 if(typedCharacter) typedCharacter(character, modifiers);
96 unichar characterAsUnichar = [characterAsString characterAtIndex:0];
97 if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:characterAsUnichar])
104 if(typedWord) typedWord(word);
107 [context->wordInProgress deleteCharactersInRange:NSMakeRange(0, [context->wordInProgress length])];
109 else if (character[0] ==
'\b' && character[1] == 0)
111 NSInteger wordLength = [context->wordInProgress length];
113 [context->wordInProgress deleteCharactersInRange:NSMakeRange(wordLength - 1, 1)];
117 [context->wordInProgress appendString:characterAsString];
120 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:characterAsUnichar])
124 if(typedLine) typedLine(line);
126 [context->lineInProgress deleteCharactersInRange:NSMakeRange(0, [context->lineInProgress length])];
128 else if (character[0] ==
'\b' && character[1] == 0)
130 NSInteger lineLength = [context->lineInProgress length];
132 [context->lineInProgress deleteCharactersInRange:NSMakeRange(lineLength - 1, 1)];
136 [context->lineInProgress appendString:characterAsString];
151 bool shouldFireForRepeat)
153 NSWindow *targetWindow = (NSWindow *)windowRef;
154 NSEventType type = [event type];
156 bool isARepeat =
false;
157 if (type == NSKeyDown || type == NSKeyUp)
158 isARepeat = [event isARepeat];
160 if ((! targetWindow || targetWindow == [event window] || [[targetWindow contentView] isInFullScreenMode]) &&
162 (shouldFireForRepeat || !isARepeat))
164 CGEventFlags flags = CGEventGetFlags([event CGEvent]);
165 bool isKeyInFlags = (type == NSFlagsChanged &&
166 (((key == VuoKey_Command) && (flags & kCGEventFlagMaskCommand)) ||
167 ((key == VuoKey_CapsLock) && (flags & kCGEventFlagMaskAlphaShift)) ||
168 ((key == VuoKey_Shift || key == VuoKey_RightShift) && (flags & kCGEventFlagMaskShift)) ||
169 ((key == VuoKey_Control || key == VuoKey_RightControl) && (flags & kCGEventFlagMaskControl)) ||
170 ((key == VuoKey_Option || key == VuoKey_RightOption) && (flags & kCGEventFlagMaskAlternate)) ||
171 ((key == VuoKey_Function) && (flags & kCGEventFlagMaskSecondaryFn))));
175 if (type == NSKeyDown || isKeyInFlags)
198 id monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event) {
199 VuoKeyboard_fireTypingIfNeeded(event, context, typedLine, typedWord, typedCharacter, window);
216 ^(
VuoText line) { typedLine(line); },
217 ^(
VuoText word) { typedWord(word); },
231 bool shouldFireForRepeat)
235 id monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask|NSKeyUpMask|NSFlagsChangedMask handler:^(NSEvent *event) {
236 VuoKeyboard_fireButtonsIfNeeded(event, pressed, released, window, key, modifierKey, shouldFireForRepeat);
252 dispatch_sync(dispatch_get_main_queue(), ^{
256 [NSEvent removeMonitor:context->monitor];
261 [context->wordInProgress release];
262 [context->lineInProgress release];