iOS 7硬件键盘事件 [英] iOS 7 Hardware Keyboard Events

查看:86
本文介绍了iOS 7硬件键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Now that we have iOS 7, Apple apparently has removed the option to receive keyboard notifications from -sendEvent:. This is a huge pain to people who are wanting to write something that captures all key events and sends it off to a remote computer, E.G. a VNC client. UIKeyCommand does not provide the functionality needed. There are many bug reports submitted to apple, but they will not listen. The bug report apple is closing all reports as a duplicate is rdar://14129420.

什么是最好的解决方案?

What is the best solution for this?

推荐答案

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

I have at least been able to get these events back the private API way, however the keyup doesn't return any useful info such as the key that was released. Maybe this is something which can be pulled from UIEvent?

需要添加的代码是UIPhysicalKeyboardEvent的以下定义.

The code that's needed to be added 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

要监听事件,请在UIResponder中使用以下内容.我不确定响应者是否需要成为关键.

To listen for events, use the following in a UIResponder. I am not sure if the responder needs to be key or not.

- (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确定何时按下命令键,选项键和控制键.我玩的还不是很多,但是似乎是获得活动的好方法.

I'm hoping that this code will at least be of some help to people who needs this. You can determine when the command key, option key, and control key is pressed using _modifierFlags. I haven't played around with it much, but seems to be an good way to get the events.

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

You could possibly borrow code from http://nacho4d-nacho4d.blogspot.com/2012/01/catching-keyboard-events-in-ios.html to improve. If anyone else finds a better way, please post them!

由于这是使用私有API,因此最好将所有内容包装在respondsToSelector中. _unmodifiedInput之类的东西.

Since this is using private APIs, it might be better for you to wrap everything in respondsToSelector. Things such as _unmodifiedInput.

我不确定苹果是否会接受实现此功能的应用程序,因此使用后果自负.

I am not sure if apple would accept an application that implements this, so use at your own risk.

这篇关于iOS 7硬件键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆