gpt4 book ai didi

java - android - 获取软键盘按键

转载 作者:行者123 更新时间:2023-11-29 05:12:11 25 4
gpt4 key购买 nike

我正在尝试按下软键盘上的键,但我无法这样做。目前我正在使用以下代码

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

它正在捕获硬件键盘而非虚拟键盘的按键事件。谁能帮帮我

最佳答案

来自Android Official Page

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观察软键盘上按下的字符的接口(interface),示例:

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

关于java - android - 获取软键盘按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28040110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com