Events for iOS 7 hardware keyboard

Now that we have iOS 7, Apple has obviously removed the ability to receive keyboard notifications from -sendEvent: This is a huge pain for people who want to write something that captures all the key events and sends them to a remote computer, the VNC EG client. UIKeyCommand does not provide the necessary functionality. Apple has a lot of bug reports, but they will not listen. The kernel of the error report closes all reports because the duplicate: rdar: // 14129420.

What is the best solution for this?

+4
source share
2 answers

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.

+5
source

Override this method in UIApplication and have fun :)

 - (void)handleKeyUIEvent:(UIEvent *)event { size_t s = malloc_size((__bridge const void *)(event)); NSLog(@"%s enter... %ld", __func__, s); // unsigned char *ptr = (unsigned char *)(__bridge void *)event; unsigned long *ptr = (unsigned long *)(__bridge void *)event; // //#define OFF_KEY_MASK 12 //#define OFF_KEY_SCANCODE 15 //#define OFF_KEY_CHAR 17 // NSLog(@"type: %lx off: %d", *(ptr + 2), 2); // NSLog(@"MASK: %lx off: %d", *(ptr + OFF_KEY_MASK), OFF_KEY_MASK); // NSLog(@"SCAN: %lx off: %d", *(ptr + OFF_KEY_SCANCODE), OFF_KEY_SCANCODE); // NSLog(@"CHAR: %lx off: %d", *(ptr + OFF_KEY_CHAR), OFF_KEY_CHAR); NSLog(@"sizeof unsigned long: %lx", sizeof(unsigned long)); for (int i = 0; i < s / 4; ++i) { // NSLog(@"... [%d] = %x", i, *(unsigned char *)(ptr + i)); NSLog(@"... [%d] = %lx", i, *(unsigned long *)(ptr + i)); } #define OFF_DUMP 8 unsigned long *dump = (unsigned long *) *(ptr + OFF_DUMP); s = malloc_size((const void *)*(ptr + OFF_DUMP)); NSLog(@"... *[%d] size: %ld", OFF_DUMP, malloc_size((const void *)*(ptr + OFF_DUMP))); for (int i = 0; i < s / 4; ++i) { NSLog(@"..... [%d] = %lx", i, *(unsigned long *)(dump + i)); } struct objc_super super_data = { self, [UIApplication class] }; objc_msgSendSuper(&super_data, @selector(handleKeyUIEvent:), event); // [super handleKeyUIEvent:event]; } 
+4
source

Source: https://habr.com/ru/post/1501708/


All Articles