Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bjhomer/3c384d3c22b0cbcfdf43 to your computer and use it in GitHub Desktop.
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.
-(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];
}
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