Last active
August 29, 2015 14:04
-
-
Save bjhomer/3c384d3c22b0cbcfdf43 to your computer and use it in GitHub Desktop.
Looking for the best way to use switch based on a single character. Seems like I have to convert everything to Int(), which can be very verbose.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)keyDown:(NSEvent *)event | |
{ | |
unichar ch = [[event charactersIgnoringModifiers] characterAtIndex:0]; | |
if (ch == NSUpArrowFunctionKey && (event.modifierFlags & NSCommandKeyMask)) { | |
// Scroll to top | |
return; | |
} | |
else if (ch == NSDownArrowFunctionKey && (event.modifierFlags & NSCommandKeyMask)) { | |
// Scroll to bottom | |
return; | |
} | |
switch (ch) { | |
case NSRightArrowFunctionKey: | |
// Select the current row | |
return; | |
case ' ': | |
// Scroll down one page | |
return; | |
default: | |
break; | |
} | |
[super keyDown:event]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func keyDown(theEvent: NSEvent) { | |
let char = Int(theEvent.charactersIgnoringModifiers.utf16[0]) // <----- This seems ugly | |
let hasCommand = (theEvent.modifierFlags & .CommandKeyMask).value != 0 | |
switch char { | |
case NSUpArrowFunctionKey where hasCommand == true: | |
// Scroll to top | |
break | |
case NSDownArrowFunctionKey where hasCommand == true: | |
// Scroll to bottom | |
break | |
case NSRightArrowFunctionKey where hasCommand == true: | |
// Select the current row | |
break | |
case Int(" ".utf16[0]): // <---- Surely there's a better way of doing this? | |
// Scroll down one page | |
break | |
default: | |
super.keyDown(theEvent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment