【问题标题】:I want to force keyboard on with bluetooth device我想用蓝牙设备强制打开键盘
【发布时间】:2013-10-25 11:23:57
【问题描述】:

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

但是!!!当iphone连接蓝牙设备时,我必须通过键盘在文本框中写一些东西。

请告诉我该怎么做! :) 谢谢~

【问题讨论】:

  • 您找到解决方案了吗?同样的问题!

标签: ios bluetooth keyboard


【解决方案1】:

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

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

@property (strong, nonatomic) UIView *inputAccessoryView;

delegate.m

中添加以下通知 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];

当我们关注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];
}

“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);

}

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

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

【讨论】:

  • 这在 iOS8 & 9 上不起作用,有没有其他解决方案?
  • 有人找到解决方案了吗?
  • @AndyM,当您在 iOS 8/9 中实现上述代码时,它是否显示任何错误/警告?
  • 有人找到解决方案了吗? iOS 10 还是同样的问题。现在它只显示 inputAccessoryView。我想过设置一个按钮来强制键盘显示,但我不知道如何或是否有可能
【解决方案2】:

按照 UIKeyInput 协议创建一个 UIView 子类。

@interface SomeInputView : UIView <UIKeyInput> {

在实现文件(.m)中

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)insertText:(NSString *)text {
    //Some text entered by user
}

-(void)deleteBackward {
    //Delete key pressed
}

只要你想显示键盘就可以了

[myInputView becomeFirstResponder];

【讨论】:

    猜你喜欢
    • 2011-11-09
    • 1970-01-01
    • 2014-01-05
    • 2015-10-01
    • 1970-01-01
    • 2013-02-13
    • 2022-06-24
    • 2016-03-23
    • 2023-03-23
    相关资源
    最近更新 更多