Android的 - 让软键盘按键presses [英] android - getting soft keyboard key presses

查看:176
本文介绍了Android的 - 让软键盘按键presses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得pssed软键盘上的键$ P $,但我不能这样做。
目前我使用以下code

I am trying to get the key pressed on soft keyboard but am unable to do so. Currently i am using the following code

@Override
public boolean dispatchKeyEvent(KeyEvent KEvent) 
{
int keyaction = KEvent.getAction();

if(keyaction == KeyEvent.ACTION_DOWN)
{
    int keycode = KEvent.getKeyCode();
    int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
    char character = (char) keyunicode;

    System.out.println("DEBUG MESSAGE KEY=" + character + " KEYCODE=" +  keycode);
}


return super.dispatchKeyEvent(KEvent);

}

据捕捉的硬件键盘,但不是为虚拟的键事件。有人可以帮助我

It is catching key events for the hardware keyboard but not for the virtual one. Can someone help me out

推荐答案

Android的官方网页

请注意:在处理与该KeyEvent类和相关API的键盘事件,你应该想到,这样的键盘事件从硬件键盘只来。你永远不应该依靠对软输入法接收关键事件的任意键(屏幕上的键盘)。

Note: When handling keyboard events with the KeyEvent class and related APIs, you should expect that such keyboard events come only from a hardware keyboard. You should never rely on receiving key events for any key on a soft input method (an on-screen keyboard).

所以,你应该使用 TextWatcher 接口,观察人物pressed上SoftKeyboard,例如:

So you should use TextWatcher Interface to observe the characters pressed on the SoftKeyboard, example:

myEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });

这篇关于Android的 - 让软键盘按键presses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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