11 #include <AppKit/AppKit.h>
18 "title" :
"VuoKeyboard",
68 NSWindow *targetWindow = (NSWindow *)windowRef;
69 if (! targetWindow || targetWindow == [event window] || [[targetWindow contentView] isInFullScreenMode])
72 [event modifierFlags],
77 NSString *unicodeString = [NSString stringWithUTF8String:unicodeBytes];
79 unsigned long long flags = [event modifierFlags];
82 if(flags & NSEventModifierFlagCommand) modifiers |= VuoModifierKey_Command;
83 if(flags & NSEventModifierFlagOption) modifiers |= VuoModifierKey_Option;
84 if(flags & NSEventModifierFlagControl) modifiers |= VuoModifierKey_Control;
85 if(flags & NSEventModifierFlagShift) modifiers |= VuoModifierKey_Shift;
87 for (NSUInteger i = 0; i < [unicodeString length]; ++i)
90 NSString *characterAsString = [unicodeString substringWithRange:NSMakeRange(i, 1)];
92 if(typedCharacter) typedCharacter(character, modifiers);
94 unichar characterAsUnichar = [characterAsString characterAtIndex:0];
95 if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:characterAsUnichar])
102 if(typedWord) typedWord(word);
105 [context->wordInProgress deleteCharactersInRange:NSMakeRange(0, [context->wordInProgress length])];
107 else if (character[0] ==
'\b' && character[1] == 0)
109 NSInteger wordLength = [context->wordInProgress length];
111 [context->wordInProgress deleteCharactersInRange:NSMakeRange(wordLength - 1, 1)];
115 [context->wordInProgress appendString:characterAsString];
118 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:characterAsUnichar])
122 if(typedLine) typedLine(line);
124 [context->lineInProgress deleteCharactersInRange:NSMakeRange(0, [context->lineInProgress length])];
126 else if (character[0] ==
'\b' && character[1] == 0)
128 NSInteger lineLength = [context->lineInProgress length];
130 [context->lineInProgress deleteCharactersInRange:NSMakeRange(lineLength - 1, 1)];
134 [context->lineInProgress appendString:characterAsString];
149 bool shouldFireForRepeat)
151 NSWindow *targetWindow = (NSWindow *)windowRef;
152 NSEventType type = [event type];
154 bool isARepeat =
false;
155 if (type == NSEventTypeKeyDown || type == NSEventTypeKeyUp)
156 isARepeat = [event isARepeat];
158 if ((! targetWindow || targetWindow == [event window] || [[targetWindow contentView] isInFullScreenMode]) &&
160 (shouldFireForRepeat || !isARepeat))
162 CGEventFlags flags = CGEventGetFlags([event CGEvent]);
163 bool isKeyInFlags = (type == NSEventTypeFlagsChanged &&
164 (((key == VuoKey_Command) && (flags & kCGEventFlagMaskCommand)) ||
165 ((key == VuoKey_CapsLock) && (flags & kCGEventFlagMaskAlphaShift)) ||
166 ((key == VuoKey_Shift || key == VuoKey_RightShift) && (flags & kCGEventFlagMaskShift)) ||
167 ((key == VuoKey_Control || key == VuoKey_RightControl) && (flags & kCGEventFlagMaskControl)) ||
168 ((key == VuoKey_Option || key == VuoKey_RightOption) && (flags & kCGEventFlagMaskAlternate)) ||
169 ((key == VuoKey_Function) && (flags & kCGEventFlagMaskSecondaryFn))));
173 if (type == NSEventTypeKeyDown || isKeyInFlags)
196 id monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^(NSEvent *event) {
197 VuoKeyboard_fireTypingIfNeeded(event, context, typedLine, typedWord, typedCharacter, window);
214 ^(
VuoText line) { typedLine(line); },
215 ^(
VuoText word) { typedWord(word); },
229 bool shouldFireForRepeat)
233 id monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown|NSEventMaskKeyUp|NSEventMaskFlagsChanged handler:^(NSEvent *event) {
234 VuoKeyboard_fireButtonsIfNeeded(event, pressed, released, window, key, modifierKey, shouldFireForRepeat);
250 dispatch_sync(dispatch_get_main_queue(), ^{
254 [NSEvent removeMonitor:context->monitor];
259 [context->wordInProgress release];
260 [context->lineInProgress release];