【问题标题】:Android - Is it possible to intercept the input event without the input field and an open soft- keyboard?Android - 是否可以在没有输入字段和打开软键盘的情况下拦截输入事件?
【发布时间】:2014-08-28 13:55:40
【问题描述】:

如果应用程序不显示 EditText(文本框)并且没有软键盘,是否有可能以某种方式捕获文本输入事件?

操作系统:Android 4.1.2

附:让我解释一下为什么这是必要的:有一个设备 - 摩托罗拉 TC55 -(数据收集终端,Android 4.1.2 上的智能手机,带有条形码阅读器(激光)),它的左侧有一个扫描按钮。

按下后,激光扫描仪打开,读取条形码,读取条形码符号后,以编程方式“按下”。

【问题讨论】:

标签: android event-handling android-softkeyboard keyboard-events motorola-droid


【解决方案1】:

另一种解决方案应该是利用这些设备在读取条形码时提供的 Intent。

您可以在developer documentation for the TC55 中找到更多信息,这只是对设备进行配置,以便将带有条形码数据的 Intent 发送到您的应用程序。

此配置可以导出并在不同设备上重复使用。另一种选择是让您的应用程序使用TC55's Profile API 以编程方式设置配置。通过这种方式,您可以针对您的用例微调条形码解码器。

使用 DataCapture Intents 比使用模拟键盘输入要干净得多。

【讨论】:

    【解决方案2】:

    您是否尝试过覆盖 onKeyDown 方法?

     @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        Log.d("MyApp", "key downed : " + keyCode);
        if ( keyCode == KeyEvent.KEYCODE_SCANNER /* HERE AN OBSVIOUSLY FALSE CONSTANT*/ ) {
    
           // perform your desired action here 
    
           // return 'true' to prevent further propagation of the key event 
           return true; 
        } 
    
        // let the system handle all other key events 
        return super.onKeyDown(keyCode, event);
    } 
    

    现在您只需在此列表中跟踪并找到正确的键码: http://developer.android.com/reference/android/view/KeyEvent.html

    有关详细信息,请参阅此回复: https://stackoverflow.com/a/19215087/1802663

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 2015-01-23
      • 1970-01-01
      • 2012-08-24
      相关资源
      最近更新 更多