我想用蓝牙设备强制键盘

时间:2013-10-17 05:17:22

标签: ios bluetooth keyboard

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

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

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

2 个答案:

答案 0 :(得分:13)

即使连接了蓝牙键盘,我们也可以显示设备虚拟键盘。我们需要使用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显示。

如果这有帮助,如果您需要更多详细信息,请与我们联系。

答案 1 :(得分:0)

按照UIKeyInput协议创建UIView子类。

json_src = open('data/{}.json'.format(search_hash), mode='r').read()
json_strng = json.loads(json_src)

csv_file = open('data/{}.csv'.format(search_hash), mode='w')

write_obj = csv.writer(csv_file)

fields = ['created_at', 'text', 'screen_name', 'location',
          'y_coord', 'x_coord']
print "CSV Headers created"
write_obj.writerow(fields)


for line in json_strng:

    write_obj.writerow([line.get('created_at'),
                        line.get('text'),
                        line.get('user').get('screen_name'),
                        line.get('user').get('location'),
                        line.get('geo').get('coordinates').get('0'),
                        line.get('geo').get('coordinates').get('1')])

csv_file.close()
print "CSV File Completed"

并在实施文件(.m)

@interface SomeInputView : UIView <UIKeyInput> {

每当你想要显示键盘时

-(BOOL)canBecomeFirstResponder {
    return YES;
}

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

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