Last active
December 3, 2019 08:48
-
-
Save danoli3/70df96d818b9eb8dea820755409bc1c6 to your computer and use it in GitHub Desktop.
iOS Smart Keyboard (BlueTooth) For Games and Apps (KeyPress / KeyReleased)
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
// ... | |
// Attach to your main ViewController that you want to respond to the iOS Smart Keyboard | |
- (BOOL)canBecomeFirstResponder { | |
return YES; | |
} | |
// Overloaded _keyCommandForEvent (UIResponder.h) // Only exists in iOS 9+ so this function can be unguarded added to iOS 8 targets | |
-(UIKeyCommand *)_keyCommandForEvent:(UIEvent *)event // UIPhysicalKeyboardEvent | |
{ | |
#ifndef NDEBUG | |
// debug mode only | |
NSLog(@"%s\nKeyCode:%@\nisKeyDown:%@\n\n", "_keyCommandForEvent:", [event valueForKey:@"_keyCode"], [event valueForKey:@"_isKeyDown"]); | |
#endif | |
int theKeyCode = [[event valueForKey:@"_keyCode"] intValue]; | |
BOOL isTheKeyDown = [[event valueForKey:@"_isKeyDown"] boolValue]; | |
// use the following switch if you want to quickly filter out keys. Else just call handleCommand | |
BOOL filterKeys = YES; | |
if(filterKeys) { | |
switch (theKeyCode) { | |
case 80: // Left Arrow | |
case 79: // Right Arrow | |
case 4: // A | |
case 7: // D | |
case 26: // W | |
case 1: // S | |
case 42: // ESC | |
case 43: // TAB | |
case 40: // Enter / return | |
case 44: // Space | |
case 82: // Up Arrow | |
case 81: // Down Arrow | |
{ | |
// Do Keyevent Handling if it's one of the above keys only | |
[self handleCommand:theKeyCode isKeyDown:isTheKeyDown]; | |
break; | |
} | |
default: | |
// Nothing. | |
break; | |
} | |
} else { | |
[self handleCommand:theKeyCode isKeyDown:isTheKeyDown]; | |
} | |
return nil; | |
} | |
- (void)handleCommand:(int)keyCode isKeyDown:(BOOL)isTheKeyDown | |
{ | |
// do your own magic here while listening to drum and bass ;D https://www.youtube.com/watch?v=kjBKad1z81M | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment