| |||||||
Das Thema "Probleme mit KeyEvents in Input Method" befindet sich unter Android App Entwicklung auf Android-Hilfe.de.
|
| | Themen-Optionen | Ansicht |
| | #1 (permalink) |
| Neuer Benutzer Registriert seit: 10.08.2011
Beiträge: 1
Abgegebene Danke: 0
Erhielt 0 Danke für 0 Beiträge
| ich entwickle momentan eine Input Method für Google Android (API Level 11, d.h. Version 3.0) und habe dabei ein paar Verständnissschwierigkeiten bzw. Probleme im Zusammenhang mit der Klasse KeyEvent und deren Verwendung. Bei der Input Method handelt es sich um ein Software Keyboard, das in der Lage sein soll, Sondertasten des PCs an die daunterligende Applikation zu senden (z.B. ESC, CTRL, ALT, SHIFT). Um dies zu testen, verwende ich den Javascript Key Event Tester. Falls jemand eine bessere Möglichkeit weiß, wäre ich auch dafür schon einmal dankbar! Nun zu den eigentlichen Problemen. Das Senden von Zeichentasten (z.B. 'A', 'a', aber auch ENTER) habe ich mit der Methode InputConnection.commitText() realisiert. Dies funktioniert auch soweit. Bei den Sondertasten fangen die Probleme jedoch an. Ich habe mich an das offizielle Tutorial Creating an Input Method gehalten, das im Abschnitt Sending test to the application die Klasse KeyEvent nennt. Nun habe ich nachfolgenden Quelltest geschrieben, um zu Testen, ob auf diese Art und Weise die ESCAPE-Taste erkannt wird (analog zum Beispiel im Tutorial): Code: InputConnection ic = this.getCurrentInputConnection();
if (ic != null) {
long eventTime = SystemClock.uptimeMillis();
ic.sendKeyEvent(
new KeyEvent(
eventTime, // The time (in uptimeMillis()) at which this key code originally went down.
eventTime, // The time (in uptimeMillis()) at which this event happened.
KeyEvent.ACTION_DOWN, // Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
KeyEvent.KEYCODE_ESCAPE, // The key code.
0, // A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
0, // Flags indicating which meta keys are currently pressed.
KeyCharacterMap.ALPHA, // The device ID that generated the key event.
0, // Raw device scan code of the event.
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE // The flags for this key event.
)
);
ic.sendKeyEvent(
new KeyEvent(
eventTime, // The time (in uptimeMillis()) at which this key code originally went down.
eventTime, // The time (in uptimeMillis()) at which this event happened.
KeyEvent.ACTION_UP, // Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
KeyEvent.KEYCODE_ESCAPE, // The key code.
0, // A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
0, // Flags indicating which meta keys are currently pressed.
KeyCharacterMap.ALPHA, // The device ID that generated the key event.
0, // Raw device scan code of the event.
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE // The flags for this key event.
)
);
Log.w("KEYCODE_ESCAPE", String.valueOf(KeyEvent.KEYCODE_ESCAPE)); //$NON-NLS-1$
} Code: this.getCurrentInputConnection().commitText(
String.valueOf((char) 27), 1
); 1. Definition eines Attributs, dass eine Bitmaske für die aktuell gedrückten Metatasten darstellt: Code: /**
* The mask that includes the modifier key meta state bits <tt>{@link
* KeyEvent#META_ALT_ON}</tt>, <tt>{@link KeyEvent#META_CTRL_ON}</tt> and
* <tt>{@link KeyEvent#META_SHIFT_ON}</tt>.
*/
private int _iCurrentMetaKeyMask = 0; Code: /**
* Helper method to send a control key (<i>Alt<i>) to the current editor.
*/
private void _handleAlt() {
this._iCurrentMetaKeyMask ^= KeyEvent.META_ALT_ON;
}
/**
* Helper method to send a control key (<i>Ctrl<i>) to the current editor.
*/
private void _handleCtrl() {
this._iCurrentMetaKeyMask ^= KeyEvent.META_CTRL_ON;
}
/**
* Helper method to send <i>Shift</i> to the current editor.
*/
private void _handleShift() {
this._iCurrentMetaKeyMask ^= KeyEvent.META_SHIFT_ON;
} Code: /**
* Helper method to send a character key to hte current editor.
*
* @param primaryCode
* the unicode code of the key that was pressed.
*/
private void _handleCharacter(int primaryCode) {
Log.w("CURRENT_META_MASK", String.valueOf(this._iCurrentMetaKeyMask)); //$NON-NLS-1$
InputConnection ic = this.getCurrentInputConnection();
// Check whether the current bit mask of the meta keys is set.
if (0 != this._iCurrentMetaKeyMask) {
KeyEvent keyEvent = null;
long eventTime = SystemClock.uptimeMillis();
if ((this._iCurrentMetaKeyMask & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
this.sendDownKeyEvent(KeyEvent.KEYCODE_ALT_LEFT);
}
if ((this._iCurrentMetaKeyMask & KeyEvent.META_CTRL_ON) == KeyEvent.META_CTRL_ON) {
this.sendDownKeyEvent(KeyEvent.KEYCODE_CTRL_LEFT);
}
if ((this._iCurrentMetaKeyMask & KeyEvent.META_SHIFT_ON) == KeyEvent.META_SHIFT_ON) {
this.sendDownKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT);
}
// ToDo: Wie soll das gehen? Mapping über ASCII auf KeyCodes???
// Momentan wird immer A gesendet, falls Alt, Ctrl und/oder Shift
// gedrückt wurde.
keyEvent = new KeyEvent(
SystemClock.uptimeMillis(), eventTime,
KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_A,
this._iCurrentMetaKeyMask
);
ic.sendKeyEvent(keyEvent);
} else {
ic.commitText(
String.valueOf((char) primaryCode), 1
);
}
}
Gibt es dafür bereits vorhandene Methoden? Ansonsten müsste man sich ja z.B. eine Map bauen, die diese Zuordnungen enthält (unschön!). Ich hoffe jemand hier hat Erfahrungen mit Input Methods und KeyEvents und kann mir bei dem Einen oder Anderen Problem weiter helfen. Viele Grüße. teh_hahn |
| | |
![]() |
|
| Themen-Optionen | |
| Ansicht | |
| |
| ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Moto Input Method | bero55 | Motorola Defy Forum | 3 | 09.07.2011 12:25 |
| [Root] New Rooting method for 2.1 | updated for 2.0.2.A.0.24 | 9/01/2011 | UKSheep | Root / Hacking / Modding für Sony Ericsson Xperia X10 | 2 | 12.01.2011 17:51 |
| Probleme speech input better keyboard | wmf | Individualisierung | 0 | 10.02.2010 07:41 |
| Root mit 1.5 1-click-Method und Krankis Cyanogen Fragen! | Underhill | Root / Hacking / Modding für T-Mobile G1 | 50 | 17.01.2010 12:57 |
| touch input (htc tastatur) funzt nicht mit donut!? | canstar | Android OS Entwicklung / Customize | 10 | 19.11.2009 11:17 |