iOS 7硬件键盘事件

时间:2013-09-11 17:35:51

标签: events keyboard ios7

现在我们有iOS 7,Apple显然已经删除了从-sendEvent:接收键盘通知的选项。对于那些想要写出捕获所有关键事件并将其发送到远程计算机的人来说,这是一个巨大的痛苦。一个VNC客户端。 UIKeyCommand未提供所需的功能。有许多提交给苹果的错误报告,但他们不会听。错误报告apple正在关闭所有报告,因为副本是rdar:// 14129420。

最佳解决方案是什么?

2 个答案:

答案 0 :(得分:5)

我至少能够以私有API的方式获取这些事件,但是keyup不会返回任何有用的信息,例如发布的密钥。也许这可以从UIEvent

中提取出来

需要添加的代码是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

要收听事件,请在UIResponder中使用以下内容。我不确定响应者是否需要密钥。

- (id)_keyCommandForEvent:(PhysicalKeyboardEvent *)event {
    //Some reason it gets called twice and it's not because of keyup. Keyup seems to not mention it's 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");
    }
}

我希望这段代码至少对需要此功能的人有所帮助。您可以使用_modifierFlags确定何时按下命令键,选项键和控制键。我没有玩过很多东西,但似乎是获得这些活动的好方法。

您可以借用http://nacho4d-nacho4d.blogspot.com/2012/01/catching-keyboard-events-in-ios.html中的代码进行改进。如果其他人找到更好的方法,请发布它们!

由于这是使用私有API,因此最好将所有内容包装在respondsToSelector中。像_unmodifiedInput

这样的事情

我不确定苹果是否会接受实现此功能的应用程序,因此请自行承担风险。

答案 1 :(得分:4)

在UIApplication上覆盖此方法并享受乐趣:)

- (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];
}