At least I was able to return these events to a private API, however keyup does not return any useful information, such as the key that was issued. Maybe this is something that can be pulled out of UIEvent
?
The code you need to add is the following definition of UIPhysicalKeyboardEvent
.
@interface PhysicalKeyboardEvent : UIEvent {//UIPhysicalButtonsEvent int _inputFlags; NSString *_modifiedInput; NSString *_unmodifiedInput; NSString *_shiftModifiedInput; NSString *_commandModifiedInput; NSString *_markedInput; long long _modifierFlags; NSString *_privateInput; } + (id)_eventWithInput:(id)arg1 inputFlags:(int)arg2; @property(retain, nonatomic) NSString *_privateInput; // @synthesize _privateInput; @property(nonatomic) int _inputFlags; // @synthesize _inputFlags; @property(nonatomic) long long _modifierFlags; // @synthesize _modifierFlags; @property(retain, nonatomic) NSString *_markedInput; // @synthesize _markedInput; @property(retain, nonatomic) NSString *_commandModifiedInput; // @synthesize _commandModifiedInput; @property(retain, nonatomic) NSString *_shiftModifiedInput; // @synthesize _shiftModifiedInput; @property(retain, nonatomic) NSString *_unmodifiedInput; // @synthesize _unmodifiedInput; @property(retain, nonatomic) NSString *_modifiedInput; // @synthesize _modifiedInput; @property(readonly, nonatomic) long long _gsModifierFlags; - (void)_privatizeInput; - (void)dealloc; - (id)_cloneEvent; - (_Bool)isEqual:(id)arg1; - (_Bool)_matchesKeyCommand:(id)arg1; //- (void)_setHIDEvent:(struct __IOHIDEvent *)arg1 keyboard:(struct __GSKeyboard *)arg2; @property(readonly, nonatomic) long long _keyCode; @property(readonly, nonatomic) _Bool _isKeyDown; - (long long)type; @end
To listen to events, use. I'm not sure if the defendant should be key or not.
- (id)_keyCommandForEvent:(PhysicalKeyboardEvent *)event { //Some reason it gets called twice and it not because of keyup. Keyup seems to not mention it original key. [NSObject cancelPreviousPerformRequestsWithTarget:self]; [self performSelector:@selector(processEvent:) withObject:event afterDelay:0]; return [super _keyCommandForEvent:event]; } - (void)processEvent:(PhysicalKeyboardEvent *)event { NSLog(@"%@", [event _unmodifiedInput]); NSLog(@"%d", [event _isKeyDown]); NSLog(@"%d", [event _inputFlags]); if ([event _isKeyDown] && [[event _unmodifiedInput] isEqualToString:@"s"] && [event _modifierFlags]==206158430208) { NSLog(@"Hello"); } }
I hope this code is at least useful to people who need it. You can determine if the command key, selection key, and control key are pressed using _modifierFlags
. I have not played with him much, but it seems to be a good way to get events.
You could take the code from http://nacho4d-nacho4d.blogspot.com/2012/01/catching-keyboard-events-in-ios.html for improvement. If someone finds a better way, write about it!
Since this uses private APIs, you might be better off wrapping everything in respondsToSelector
. Things like _unmodifiedInput
.
I'm not sure that Apple will accept an application that implements it, so use it at your own risk.