|
| 1 | +#import "../Headers/Input.h" |
| 2 | + |
| 3 | + |
| 4 | +@implementation Input |
| 5 | +static BOOL inited; |
| 6 | +API_AVAILABLE(ios(14.0)) static GCKeyboard* keyboard; |
| 7 | +NSMutableSet *pressedKeys; |
| 8 | + |
| 9 | ++(void)Init |
| 10 | +{ |
| 11 | + if(inited) |
| 12 | + return; |
| 13 | + if (@available(iOS 14.0, *)) { |
| 14 | + keyboard = GCKeyboard.coalescedKeyboard; |
| 15 | + pressedKeys = [NSMutableSet init]; |
| 16 | + [[NSNotificationCenter defaultCenter] addObserver:[Input class] |
| 17 | + selector:@selector(OnKeyboardConnected:) |
| 18 | + name:GCKeyboardDidConnectNotification |
| 19 | + object:nil]; |
| 20 | + [[NSNotificationCenter defaultCenter] addObserver:[Input class] |
| 21 | + selector:@selector(OnKeyboardDisconnected:) |
| 22 | + name:GCKeyboardDidDisconnectNotification |
| 23 | + object:nil]; |
| 24 | + } |
| 25 | + inited = YES; |
| 26 | +} |
| 27 | + |
| 28 | ++(void)OnKeyboardConnected:(NSNotification *)notification |
| 29 | +{ |
| 30 | + if (@available(iOS 14.0, *)) { |
| 31 | + keyboard = notification.object; |
| 32 | + keyboard.keyboardInput.keyChangedHandler = ^(GCKeyboardInput * _Nonnull keyboard, GCControllerButtonInput * _Nonnull key, GCKeyCode keyCode, BOOL pressed) |
| 33 | + { |
| 34 | + NSNumber *KeyCodeNumber = [NSNumber numberWithLong:keyCode]; |
| 35 | + if(pressed){ |
| 36 | + [pressedKeys addObject:KeyCodeNumber]; |
| 37 | + } |
| 38 | + else{ |
| 39 | + [pressedKeys removeObject:KeyCodeNumber]; |
| 40 | + } |
| 41 | + }; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | ++(void)OnKeyboardDisconnected:(NSNotification *)notification |
| 46 | +{ |
| 47 | + if (@available(iOS 14.0, *)) { |
| 48 | + |
| 49 | + if(keyboard != nil) |
| 50 | + keyboard.keyboardInput.keyChangedHandler = nil; |
| 51 | + keyboard = nil; |
| 52 | + |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | ++(BOOL)IsKeyboardSupported |
| 57 | +{ |
| 58 | + if (@available(iOS 14.0, *)) |
| 59 | + { |
| 60 | + [Input Init]; |
| 61 | + return keyboard != nil; |
| 62 | + } else { |
| 63 | + return NO; |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | ++(BOOL)IsAnyKeyPressed |
| 69 | +{ |
| 70 | + if (@available(iOS 14.0, *)) |
| 71 | + { |
| 72 | + [Input Init]; |
| 73 | + return keyboard.keyboardInput.anyKeyPressed; |
| 74 | + } else { |
| 75 | + return false; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | ++(BOOL)GetKey:(GCKeyCode)keyCode |
| 80 | +API_AVAILABLE(ios(14.0)) |
| 81 | +{ |
| 82 | + if (@available(iOS 14.0, *)){ |
| 83 | + [Input Init]; |
| 84 | + NSNumber *KeyCodeNumber = [NSNumber numberWithLong:keyCode]; |
| 85 | + return [pressedKeys containsObject:KeyCodeNumber]; |
| 86 | + } |
| 87 | + return NO; |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +@end |
0 commit comments