Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b671394

Browse files
committed
Added modifier key event handling for the macosx backend and cleaned up key event handler.
1 parent 8ec9555 commit b671394

1 file changed

Lines changed: 65 additions & 53 deletions

File tree

src/_macosx.m

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ - (const char*)convertKeyEvent:(NSEvent*)event;
391391
- (void)keyDown:(NSEvent*)event;
392392
- (void)keyUp:(NSEvent*)event;
393393
- (void)scrollWheel:(NSEvent *)event;
394-
- (void)flagsChanged:(NSEvent*)event;
394+
//- (void)flagsChanged:(NSEvent*)event;
395395
@end
396396

397397
@interface ScrollableButton : NSButton
@@ -5439,66 +5439,75 @@ - (void)removeRubberband
54395439
rubberband = NSZeroRect;
54405440
}
54415441

5442+
5443+
54425444
- (const char*)convertKeyEvent:(NSEvent*)event
54435445
{
5444-
NSString* text = [event charactersIgnoringModifiers];
5445-
unichar uc = [text characterAtIndex:0];
5446-
int i = (int)uc;
5447-
if ([event modifierFlags] & NSNumericPadKeyMask)
5448-
{
5449-
if (i > 256)
5450-
{
5451-
if (uc==NSLeftArrowFunctionKey) return "left";
5452-
else if (uc==NSUpArrowFunctionKey) return "up";
5453-
else if (uc==NSRightArrowFunctionKey) return "right";
5454-
else if (uc==NSDownArrowFunctionKey) return "down";
5455-
else if (uc==NSF1FunctionKey) return "f1";
5456-
else if (uc==NSF2FunctionKey) return "f2";
5457-
else if (uc==NSF3FunctionKey) return "f3";
5458-
else if (uc==NSF4FunctionKey) return "f4";
5459-
else if (uc==NSF5FunctionKey) return "f5";
5460-
else if (uc==NSF6FunctionKey) return "f6";
5461-
else if (uc==NSF7FunctionKey) return "f7";
5462-
else if (uc==NSF8FunctionKey) return "f8";
5463-
else if (uc==NSF9FunctionKey) return "f9";
5464-
else if (uc==NSF10FunctionKey) return "f10";
5465-
else if (uc==NSF11FunctionKey) return "f11";
5466-
else if (uc==NSF12FunctionKey) return "f12";
5467-
else if (uc==NSScrollLockFunctionKey) return "scroll_lock";
5468-
else if (uc==NSBreakFunctionKey) return "break";
5469-
else if (uc==NSInsertFunctionKey) return "insert";
5470-
else if (uc==NSDeleteFunctionKey) return "delete";
5471-
else if (uc==NSHomeFunctionKey) return "home";
5472-
else if (uc==NSEndFunctionKey) return "end";
5473-
else if (uc==NSPageUpFunctionKey) return "pageup";
5474-
else if (uc==NSPageDownFunctionKey) return "pagedown";
5475-
}
5476-
else if ((char)uc == '.') return "dec";
5477-
}
5478-
5479-
switch (i)
5480-
{
5481-
case 127: return "backspace";
5482-
case 13: return "enter";
5483-
case 3: return "enter";
5484-
case 27: return "escape";
5485-
default:
5486-
{
5487-
static char s[2];
5488-
s[0] = (char)uc;
5489-
s[1] = '\0';
5490-
return (const char*)s;
5491-
}
5492-
}
5446+
NSDictionary* specialkeymappings = @{
5447+
@(NSLeftArrowFunctionKey): @"left",
5448+
@(NSRightArrowFunctionKey): @"right",
5449+
@(NSUpArrowFunctionKey): @"up",
5450+
@(NSDownArrowFunctionKey): @"down",
5451+
@(NSF1FunctionKey): @"f1",
5452+
@(NSF2FunctionKey): @"f2",
5453+
@(NSF3FunctionKey): @"f3",
5454+
@(NSF4FunctionKey): @"f4",
5455+
@(NSF5FunctionKey): @"f5",
5456+
@(NSF6FunctionKey): @"f6",
5457+
@(NSF7FunctionKey): @"f7",
5458+
@(NSF8FunctionKey): @"f8",
5459+
@(NSF9FunctionKey): @"f9",
5460+
@(NSF10FunctionKey): @"f10",
5461+
@(NSF11FunctionKey): @"f11",
5462+
@(NSF12FunctionKey): @"f12",
5463+
@(NSF13FunctionKey): @"f13",
5464+
@(NSF14FunctionKey): @"f14",
5465+
@(NSF15FunctionKey): @"f15",
5466+
@(NSF16FunctionKey): @"f16",
5467+
@(NSF17FunctionKey): @"f17",
5468+
@(NSF18FunctionKey): @"f18",
5469+
@(NSF19FunctionKey): @"f19",
5470+
@(NSScrollLockFunctionKey): @"scroll_lock",
5471+
@(NSBreakFunctionKey): @"break",
5472+
@(NSInsertFunctionKey): @"insert",
5473+
@(NSDeleteFunctionKey): @"delete",
5474+
@(NSHomeFunctionKey): @"home",
5475+
@(NSEndFunctionKey): @"end",
5476+
@(NSPageDownFunctionKey): @"pagedown",
5477+
@(NSPageUpFunctionKey): @"pageup",
5478+
@(NSDeleteCharacter): @"backspace",
5479+
@(NSEnterCharacter): @"enter",
5480+
@(NSTabCharacter): @"tab",
5481+
@(NSCarriageReturnCharacter): @"enter",
5482+
@(NSBackTabCharacter): @"backtab",
5483+
@27: @"escape"
5484+
};
5485+
5486+
NSMutableString* returnkey = [NSMutableString string];
5487+
if ([event modifierFlags] & NSControlKeyMask)
5488+
[returnkey appendString:@"ctrl+" ];
5489+
if ([event modifierFlags] & NSAlternateKeyMask)
5490+
[returnkey appendString:@"alt+" ];
5491+
if ([event modifierFlags] & NSCommandKeyMask)
5492+
[returnkey appendString:@"cmd+" ];
5493+
if ([event modifierFlags] & NSShiftKeyMask)
5494+
[returnkey appendString:@"shift+" ];
5495+
5496+
unichar uc = [[event charactersIgnoringModifiers] characterAtIndex:0];
5497+
NSString* specialchar = [specialkeymappings objectForKey:@(uc)];
5498+
// NSString* specialchar = specialkeymappings[@(uc)];
5499+
if (specialchar)
5500+
[returnkey appendString:specialchar];
5501+
else
5502+
[returnkey appendString:[event charactersIgnoringModifiers]];
54935503

5494-
return NULL;
5504+
return [returnkey UTF8String];
54955505
}
54965506

54975507
- (void)keyDown:(NSEvent*)event
54985508
{
54995509
PyObject* result;
55005510
const char* s = [self convertKeyEvent: event];
5501-
/* TODO: Handle ctrl, alt, super modifiers. qt4 has implemented these. */
55025511
PyGILState_STATE gstate = PyGILState_Ensure();
55035512
if (s==NULL)
55045513
{
@@ -5520,7 +5529,6 @@ - (void)keyUp:(NSEvent*)event
55205529
{
55215530
PyObject* result;
55225531
const char* s = [self convertKeyEvent: event];
5523-
/* TODO: Handle ctrl, alt, super modifiers. qt4 has implemented these. */
55245532
PyGILState_STATE gstate = PyGILState_Ensure();
55255533
if (s==NULL)
55265534
{
@@ -5561,6 +5569,9 @@ - (void)scrollWheel:(NSEvent*)event
55615569
PyGILState_Release(gstate);
55625570
}
55635571

5572+
/* This is all wrong. Address of pointer is being passed instead of pointer, keynames don't
5573+
match up with what the front-end and does the front-end even handle modifier keys by themselves?
5574+
55645575
- (void)flagsChanged:(NSEvent*)event
55655576
{
55665577
const char *s = NULL;
@@ -5580,6 +5591,7 @@ - (void)flagsChanged:(NSEvent*)event
55805591
55815592
PyGILState_Release(gstate);
55825593
}
5594+
*/
55835595
@end
55845596

55855597
@implementation ScrollableButton

0 commit comments

Comments
 (0)