如何将关键事件作为单词

时间:2016-06-07 15:27:40

标签: android keyevent

我需要将关键事件作为单词而不是按钮发送 例如,而不是这个     Instrumentation m_Instrumentation = new Instrumentation();     m_Instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_D); 我希望它像这样     Instrumentation m_Instrumentation = new Instrumentation();     m_Instrumentation.sendKeyDownUpSync(" Hello");

1 个答案:

答案 0 :(得分:0)

正如约瑟夫·埃文斯的评论所指出的那样,创建一种方法来循环你的单词并发送相应的KeyEvent - 请参阅下面的代码

public class InstrumentationHelper {

    public static void sendKeyEventsFor(String word) {

        Instrumentation instrumentation = new Instrumentation();

        KeyCharacterMap mKeyCharacterMap =
                KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

        KeyEvent[] events = mKeyCharacterMap.getEvents(word.toCharArray());

        for (KeyEvent keyEvent : events) {
            instrumentation.sendKeyDownUpSync(keyEvent.getKeyCode());
        }
    }
}