我想用蓝牙设备强制键盘

use*_*312 11 keyboard bluetooth ios

我有一个蓝牙条码设备.如果将蓝牙设备连接到iPhone,我无法用iPhone键盘写任何东西.你已经知道iPhone键盘没有显示,因为蓝牙设备是识别键盘.

但!!!当iphone连接蓝牙设备时,我必须通过键盘将一些东西写入文本框.

请让我知道怎么做!:)谢谢〜

小智 13

即使连接了蓝牙键盘,我们也可以显示设备虚拟键盘.我们需要用inputAccessoryView它.

我们需要在app delegate.h中添加以下代码

@property (strong, nonatomic) UIView *inputAccessoryView;
Run Code Online (Sandbox Code Playgroud)

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsdelegate.m中的 - 方法中添加以下通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

当我们关注a时,这将调用以下方法textField.

//This function responds to all `textFieldBegan` editing
// we need to add an accessory view and use that to force the keyboards frame
// this way the keyboard appears when the bluetooth keyboard is attached.
-(void) textFieldBegan: (NSNotification *) theNotification
{

        UITextField *theTextField = [theNotification object];

        if (!inputAccessoryView) {
            inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
            [inputAccessoryView setBackgroundColor:[UIColor lightGrayColor]];
        }

        theTextField.inputAccessoryView = inputAccessoryView;

        [self performSelector:@selector(forceKeyboard) withObject:nil afterDelay:0];
}
Run Code Online (Sandbox Code Playgroud)

和"forceKeyboard"的代码是,

-(void) forceKeyboard
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    inputAccessoryView.superview.frame = CGRectMake(0, 420, screenHeight, 352);

}
Run Code Online (Sandbox Code Playgroud)

这对我们来说很好.我们使用隐藏文本字段从蓝牙键盘获取输入,对于所有其他文本字段,我们使用显示的设备虚拟键盘inputAccessoryView.

如果这有帮助,如果您需要更多详细信息,请告诉我.

  • 不幸的是,这不适用于IOS8 (4认同)
  • 这不适用于iOS8和9,还有其他解决方案吗? (3认同)