2

I am working on an app which needs to be able to use external USB or Bluetooth keyboard wedges. I then capture the KeyEvents and cache them as needed (starting/end sentinels).

So at this point I have lists of KeyEvents that I need to turn into strings.

So I need to convert these ordered KeyEvents into a correct string.

List<KeyEvent> events = new ArrayList<KeyEvent>();
String eventString = eventsToString(events); //??????

Any ideas?

4

3 回答 3

5

经过大量研究,这就是我想出的。我使用了一个KeyListener接受关键事件的。Editable然后它基于关键事件(考虑班次等)附加到一个。随着关键事件的到来,下面的代码将简单地回显不断增长的字符串。

KeyListener tkl = new TextKeyListener(Capitalize.NONE, false);
Editable et = Editable.Factory.getInstance().newEditable("");

View.OnKeyListener key = new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
            tkl.onKeyDown(null, et, keyCode, event);
        else if (event.getAction() == KeyEvent.ACTION_UP)
            tkl.onKeyUp(null, et, keyCode, event);
        else
            tkl.onKeyOther(null, et, event); //NOTE: My devices never used KeyEvent.ACTION_MULTIPLE so I don't know if it should get fired here or with the key down event.

        Log.i(("Test: " + et.toString());
    }
};

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (key.onKey(null, event.getKeyCode(), event))
        return true;
    else
        return super.dispatchKeyEvent(event);
}
于 2013-10-29T20:06:24.320 回答
1

为每个事件使用 getDisplayLabel() 怎么样?就像是:

List<KeyEvent> events = new ArrayList<KeyEvent>();
StringBuilder strEvents = new StringBuilder();
for (KeyEvent k : events) {
    strEvents.append(k.getDisplayLabel())
    strEvents.append(",");
}

String eventString = etrEvents.toString();
于 2013-10-27T04:13:59.710 回答
0

仅使用 getUnicodeChar() 在 vaimvolk 循环上构建? http://developer.android.com/reference/android/view/KeyEvent.html#getUnicodeChar()

于 2013-10-28T16:52:55.947 回答