Send key events for the modifiers

Before sending a key event while modifiers are active, send events for
the modifier keys.

Some applications don't look at the "metaState" flags but instead keep
track of the up and down events for the modifiers.

For example, the basic text views that are in every applications
correctly handle the "metaState" flags except for one binding:
Selecting text with the arrows while pressing shift.
This commit is contained in:
Jules Aguillon 2022-02-22 19:32:16 +01:00
parent 84bce23fec
commit 14dabb6f51
2 changed files with 39 additions and 20 deletions

View File

@ -25,11 +25,11 @@ class KeyEventHandler implements Config.IKeyEventHandler
case KeyValue.EVENT_ACTION: _recv.performAction(); return; case KeyValue.EVENT_ACTION: _recv.performAction(); return;
default: default:
if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT | KeyValue.FLAG_META)) != 0) if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT | KeyValue.FLAG_META)) != 0)
handleMetaKeyUp(key, flags); handleKeyUpWithModifier(key, flags);
else if (key.char_ != KeyValue.CHAR_NONE) else if (key.char_ != KeyValue.CHAR_NONE)
_recv.commitChar(key.char_); _recv.commitChar(key.char_);
else if (key.eventCode != KeyValue.EVENT_NONE) else if (key.eventCode != KeyValue.EVENT_NONE)
handleMetaKeyUp(key, flags); handleKeyUpWithModifier(key, flags);
else else
_recv.commitText(key.symbol); _recv.commitText(key.symbol);
} }
@ -45,22 +45,43 @@ class KeyEventHandler implements Config.IKeyEventHandler
// getCurrentInputConnection().deleteSurroundingText(before, after); // getCurrentInputConnection().deleteSurroundingText(before, after);
// } // }
private void handleMetaKeyUp(KeyValue key, int flags) private int sendMetaKey(int eventCode, int metaFlags, int metaState, boolean down)
{ {
int meta = 0; int action;
if (key.eventCode == KeyValue.EVENT_NONE) int updatedMetaState;
return ; if (down) { action = KeyEvent.ACTION_DOWN; updatedMetaState = metaState | metaFlags; }
if ((flags & KeyValue.FLAG_CTRL) != 0) else { action = KeyEvent.ACTION_UP; updatedMetaState = metaState & ~metaFlags; }
meta |= KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON; _recv.sendKeyEvent(action, eventCode, metaState);
if ((flags & KeyValue.FLAG_ALT) != 0) return updatedMetaState;
meta |= KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON;
if ((flags & KeyValue.FLAG_SHIFT) != 0)
meta |= KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON;
if ((flags & KeyValue.FLAG_META) != 0)
meta |= KeyEvent.META_META_LEFT_ON | KeyEvent.META_META_ON;
_recv.sendKeyEvent(key.eventCode, meta);
} }
/* Send key events corresponding to pressed modifier keys. */
private int sendMetaKeys(int flags, int metaState, boolean down)
{
if ((flags & KeyValue.FLAG_CTRL) != 0)
metaState = sendMetaKey(KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON, metaState, down);
if ((flags & KeyValue.FLAG_ALT) != 0)
metaState = sendMetaKey(KeyEvent.KEYCODE_ALT_LEFT, KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON, metaState, down);
if ((flags & KeyValue.FLAG_SHIFT) != 0)
metaState = sendMetaKey(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON, metaState, down);
if ((flags & KeyValue.FLAG_META) != 0)
metaState = sendMetaKey(KeyEvent.KEYCODE_META_LEFT, KeyEvent.META_META_LEFT_ON | KeyEvent.META_META_ON, metaState, down);
return metaState;
}
/*
* Don't set KeyEvent.FLAG_SOFT_KEYBOARD.
*/
private void handleKeyUpWithModifier(KeyValue key, int flags)
{
if (key.eventCode == KeyValue.EVENT_NONE)
return ;
int metaState = sendMetaKeys(flags, 0, true);
_recv.sendKeyEvent(KeyEvent.ACTION_DOWN, key.eventCode, metaState);
_recv.sendKeyEvent(KeyEvent.ACTION_UP, key.eventCode, metaState);
sendMetaKeys(flags, metaState, false);
}
public static interface IReceiver public static interface IReceiver
{ {
public void switchToNextInputMethod(); public void switchToNextInputMethod();
@ -72,7 +93,7 @@ class KeyEventHandler implements Config.IKeyEventHandler
/** 'res_id' is '-1' for the currently selected layout. */ /** 'res_id' is '-1' for the currently selected layout. */
public void setLayout(int res_id); public void setLayout(int res_id);
public void sendKeyEvent(int eventCode, int meta); public void sendKeyEvent(int eventAction, int eventCode, int meta);
public void commitText(String text); public void commitText(String text);
public void commitChar(char c); public void commitChar(char c);

View File

@ -256,14 +256,12 @@ public class Keyboard2 extends InputMethodService
_keyboardView.setKeyboard(getLayout(res_id)); _keyboardView.setKeyboard(getLayout(res_id));
} }
public void sendKeyEvent(int eventCode, int meta) public void sendKeyEvent(int eventAction, int eventCode, int meta)
{ {
InputConnection conn = getCurrentInputConnection(); InputConnection conn = getCurrentInputConnection();
if (conn == null) if (conn == null)
return; return;
KeyEvent event = new KeyEvent(1, 1, KeyEvent.ACTION_DOWN, eventCode, 0, meta); conn.sendKeyEvent(new KeyEvent(1, 1, eventAction, eventCode, 0, meta));
conn.sendKeyEvent(event);
conn.sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
} }
public void showKeyboardConfig() public void showKeyboardConfig()