DispatchKeyEvent监听空格键是pressed [英] DispatchKeyEvent to listen for Spacebar being pressed

查看:281
本文介绍了DispatchKeyEvent监听空格键是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android项目,我需要检查时,空格键为pressed SOS我可以执行一定的功能。

I am working on an Android project and I need to check when the spacebar is pressed sos I can execute a certain function.

问题是,它工作在模拟器上,但不是我的实际设备上。我想可能是因为我的模拟器使用物理键盘,而不是屏幕上的虚拟键盘,但在实际设备上进行测试时,其使用虚拟键盘。

The problem is, it was working on the emulator but not on my actual device. I think it might be because my emulator was using a physical keyboard, not the onscreen virtual keyboard but when testing on an actual device, its using a virtual keyboard.

我试图派遣的KeyEvent

I'm trying the dispatch keyevent

@Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {

        if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE
                && event.getAction() == KeyEvent.ACTION_UP)
        {
            QueryEditor queryEditor = (QueryEditor)getSupportFragmentManager().findFragmentById(R.id.fragment_queryEditor);
            queryEditor.formatQueryText();
            return true;
        }
        }

我也试过上键,

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            disconnectDatabase();
        }
        else if (keyCode == KeyEvent.KEYCODE_DEL)
        {
            QueryEditor queryEditor = (QueryEditor)getSupportFragmentManager().findFragmentById(R.id.fragment_queryEditor);
            queryEditor.formatQueryText();
        }

        return super.onKeyDown(keyCode, event);
    }

这些都不被解雇,虽然除非后退按钮是pressed但我需要空格键来触发事件。

Neither of these get fired though unless the back button is pressed but I need the spacebar to trigger the event.

感谢您的帮助,您可以提供。

Thanks for any help you can provide.

更新

下面是我的code为QueryEditor碎片如何创建和事件处理程序创建

Below is my code for how QueryEditor Fragment is created and the event handler is created

@Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        iQueryEditor = (IQueryEditor)this;
        iErrorHandler = (IErrorHandler)this;

        txtQueryEditor = (EditText)getActivity().findViewById(R.id.query_txtQueryEditor);

        btnSubmitQuery = (ImageButton)getActivity().findViewById(R.id.query_btnPerformQuery);
        btnClearQuery = (ImageButton)getActivity().findViewById(R.id.query_btnDeleteQuery);

        txtQueryEditor.addTextChangedListener(new QueryTextChanged(getActivity(), txtQueryEditor, iQueryEditor));
        setDatabaseUsed();
        txtQueryEditor.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                        || actionId == EditorInfo.IME_ACTION_DONE)
                {
                    executeQuery();
                    return true;
                }
                else if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE && event.getAction() == KeyEvent.ACTION_UP)
                {
                    Toast.makeText(getActivity(), "Space Bar Pressed", Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });

        btnSubmitQuery.setOnClickListener(mBtnSubmitQueryListener);
        btnClearQuery.setOnClickListener(mBtnDeleteQueryListener);
    }

txtQueryEditor的是,我想收到关于空格键事件的EditText。

txtQueryEditor is the EditText that I am trying to receive the space bar event on.

推荐答案

部分答案在于的 KeyEvent的API文档

作为软输入的方法可以使用​​输入的多个和创造性的方式
  文字,也不能保证一个软键盘上的任意键preSS将
  生成一个键事件:这是留给IME的决定,并在
  事实上发送此类事件气馁。你永远不应该依赖
  接收的KeyEvent对软输入法的任意键。尤其是,
  默认的软件键盘绝不会任意键事件发送到任何
  应用打靶果冻豆或更高版本,并只发送事件
  对于删除和返回键的一些presses应用程序
  打靶冰淇淋三明治或更早版本。要知道,其他软件
  输入法可能从来不管版本发送关键事件。
  考虑使用像IME_ACTION_DONE编辑器操作如果需要
  与软件键盘的特异性相互作用,因为它提供了更
  可见以用户为你的应用程序将如何应对键入
  presses。

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses.

下面是一个评论离开由谷歌工程师:

Here is a comment left by a Google Engineer:

答案很简单。应用程序应该的从不的依靠关键
  事件来管理用户输入。这样做的原因是,它会导致一
  非常差的用户体验。

The answer is quite simple. Applications should never rely on key events to manage user input. The reason for this is, it results in a very poor user experience.

要开始,有太多的情况下,这是行不通的。它
  不会对CJK语言的适当工作。这是行不通的
  正确地与手势输入,或与语音输入,或用切换
  输入,或任何新的创造性的输入法开发者可能会想出
  在将来。事实上,在那里工作的唯一情况是只
  最贫穷的输入体验限制情况:遗产
  硬件键盘状输入方法,该方法是一个差适合于
  移动时代。

To start with, there are too many cases where it does not work. It does not work appropriately for CJK languages. It does not work correctly with gesture input, or with voice input, or with toggle input, or any new inventive input method developers may come up with in the future. In fact, the only case where it works is for only restricted cases of the poorest input experience : a legacy hardware-keyboard-like input method, which is a poor fit for the mobile era.

这就是为什么Android的定义了丰富的API,通过它的IME沟通
  到的应用程序。这是方法无关,语言无关,和
  所有的软件输入法正在实施它。应用程序是
  应该利用这个API,并且采用的EditText是最简单的
  (关于这个下文)这样做的方式。

This is why Android defines a rich API through which IMEs communicate to the applications. It is method agnostic and language agnostic, and all software input methods are implementing it. Applications are supposed to make use of this API, and using EditText is the simplest way of doing this (more on this below).

请停止依赖于传统的关键事件,用于文本输入 - 在所有。它
  是很好的支持他们,但要求他们是坏的每一个人。它
  休息的一致性,锁定某些语言你的应用程序,它
  迫使用户进入比他们预期的更差的体验
  Android系统。而在评论#14指出,输入法是明确
  没有义务在所有发送的关键事件,虽然有些可能会选择
  做到这一点。

Please stop relying on legacy key events for text entry -- AT ALL. It is good to support them, but requiring them is bad for everyone. It breaks consistency, it locks some languages out of your app, and it forces users into a much poorer experience than they expect on Android. And as noted in comment #14, input methods are explicitly under no obligation at all to send key events, though some may choose to do it.

有实现富文本输入的API两种简单的方法。
  双方将需要一些这是用Java做的土地。第一个是
  简单地使用的EditText。如果你的应用需要做自己的渲染,你
  可以继承它,如果适合你,或使其隐藏,而你
  渲染文本的任何更改。您也可以使用DynamicLayout和
  可编辑的,以达到相同的效果。另一种可能性是
  通过扩展实现直接InputConnection API
  BaseInputConnection,然而,这是一个的的辛勤工作,并应
  一般只需要由具有非常特殊的应用进行
  与问候需要文本版,像一个IDE或文字处理器。

There are two simple ways of implementing the rich text input APIs. Both will require some of this is done in Java land. The first one is to simply use EditText. If your app needs to do its own rendering, you can either subclass it if that suits you, or make it hidden while you render any changes to the text. You could also use DynamicLayout and Editable to achieve the same effect. Another possibility is to implement directly the InputConnection API by extending BaseInputConnection, however this is a LOT of hard work and should generally only need to be done by applications that have very specific needs with regards to text edition, like an IDE or a word processor.

这篇关于DispatchKeyEvent监听空格键是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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