2021-12-28 17:47:18 +01:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
2024-02-10 18:03:09 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2022-11-13 16:18:08 +01:00
|
|
|
import android.os.Looper;
|
2024-02-04 00:27:02 +01:00
|
|
|
import android.text.InputType;
|
2021-12-28 17:47:18 +01:00
|
|
|
import android.view.KeyEvent;
|
2022-11-13 16:18:08 +01:00
|
|
|
import android.view.inputmethod.EditorInfo;
|
2023-08-17 12:47:44 +02:00
|
|
|
import android.view.inputmethod.ExtractedText;
|
|
|
|
import android.view.inputmethod.ExtractedTextRequest;
|
2022-11-13 16:18:08 +01:00
|
|
|
import android.view.inputmethod.InputConnection;
|
2024-01-26 00:17:51 +01:00
|
|
|
import java.util.Iterator;
|
2021-12-28 17:47:18 +01:00
|
|
|
|
2024-01-13 20:59:05 +01:00
|
|
|
public final class KeyEventHandler implements Config.IKeyEventHandler
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
2022-11-13 16:18:08 +01:00
|
|
|
IReceiver _recv;
|
|
|
|
Autocapitalisation _autocap;
|
2024-01-26 00:17:51 +01:00
|
|
|
/** State of the system modifiers. It is updated whether a modifier is down
|
|
|
|
or up and a corresponding key event is sent. */
|
|
|
|
Pointers.Modifiers _mods;
|
|
|
|
/** Consistent with [_mods]. This is a mutable state rather than computed
|
|
|
|
from [_mods] to ensure that the meta state is correct while up and down
|
|
|
|
events are sent for the modifier keys. */
|
2024-02-04 00:27:02 +01:00
|
|
|
int _meta_state = 0;
|
|
|
|
/** Whether to force sending arrow keys to move the cursor when
|
|
|
|
[setSelection] could be used instead. */
|
|
|
|
boolean _move_cursor_force_fallback = false;
|
2021-12-28 17:47:18 +01:00
|
|
|
|
2022-11-13 16:18:08 +01:00
|
|
|
public KeyEventHandler(Looper looper, IReceiver recv)
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
|
|
|
_recv = recv;
|
2022-11-13 16:18:08 +01:00
|
|
|
_autocap = new Autocapitalisation(looper,
|
|
|
|
this.new Autocapitalisation_callback());
|
2024-01-26 00:17:51 +01:00
|
|
|
_mods = Pointers.Modifiers.EMPTY;
|
2022-11-13 16:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Editing just started. */
|
|
|
|
public void started(EditorInfo info)
|
|
|
|
{
|
|
|
|
_autocap.started(info, _recv.getCurrentInputConnection());
|
2024-02-04 00:27:02 +01:00
|
|
|
// Workaround a bug in Acode, which answers to [getExtractedText] but do
|
|
|
|
// not react to [setSelection] while returning [true].
|
|
|
|
// Note: Using & to workaround a bug in Acode, which sets several
|
|
|
|
// variations at once.
|
|
|
|
_move_cursor_force_fallback = (info.inputType & InputType.TYPE_MASK_VARIATION &
|
|
|
|
InputType.TYPE_TEXT_VARIATION_PASSWORD) != 0;
|
2022-11-13 16:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Selection has been updated. */
|
|
|
|
public void selection_updated(int oldSelStart, int newSelStart)
|
|
|
|
{
|
|
|
|
_autocap.selection_updated(oldSelStart, newSelStart);
|
2021-12-28 17:47:18 +01:00
|
|
|
}
|
|
|
|
|
2023-08-26 23:37:22 +02:00
|
|
|
/** A key is being pressed. There will not necessarily be a corresponding
|
|
|
|
[key_up] event. */
|
2024-01-26 00:17:51 +01:00
|
|
|
@Override
|
2023-08-26 23:37:22 +02:00
|
|
|
public void key_down(KeyValue key, boolean isSwipe)
|
|
|
|
{
|
|
|
|
if (key == null)
|
|
|
|
return;
|
|
|
|
switch (key.getKind())
|
|
|
|
{
|
|
|
|
case Modifier:
|
|
|
|
// Stop auto capitalisation when activating a system modifier
|
|
|
|
switch (key.getModifier())
|
|
|
|
{
|
|
|
|
case CTRL:
|
|
|
|
case ALT:
|
|
|
|
case META:
|
|
|
|
_autocap.stop();
|
|
|
|
break;
|
2024-02-11 20:46:36 +01:00
|
|
|
case COMPOSE_PENDING:
|
|
|
|
KeyModifier.set_compose_pending(0);
|
|
|
|
break;
|
2023-08-26 23:37:22 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:18:08 +01:00
|
|
|
/** A key has been released. */
|
2024-01-26 00:17:51 +01:00
|
|
|
@Override
|
2022-11-13 16:18:08 +01:00
|
|
|
public void key_up(KeyValue key, Pointers.Modifiers mods)
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
2022-06-05 17:26:34 +02:00
|
|
|
if (key == null)
|
2022-03-13 00:35:15 +01:00
|
|
|
return;
|
2024-01-26 00:17:51 +01:00
|
|
|
Pointers.Modifiers old_mods = _mods;
|
|
|
|
update_meta_state(mods);
|
2022-06-05 17:26:34 +02:00
|
|
|
switch (key.getKind())
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
2022-11-13 16:18:08 +01:00
|
|
|
case Char: send_text(String.valueOf(key.getChar())); break;
|
|
|
|
case String: send_text(key.getString()); break;
|
2023-06-03 20:06:44 +02:00
|
|
|
case Event: _recv.handle_event_key(key.getEvent()); break;
|
2024-01-26 00:17:51 +01:00
|
|
|
case Keyevent: send_key_down_up(key.getKeyevent()); break;
|
2023-08-17 12:47:44 +02:00
|
|
|
case Modifier: break;
|
2024-01-26 00:17:51 +01:00
|
|
|
case Editing: handle_editing_key(key.getEditing()); break;
|
2024-02-11 20:46:36 +01:00
|
|
|
case Compose_pending:
|
|
|
|
KeyModifier.set_compose_pending(key.getPendingCompose());
|
|
|
|
_recv.set_compose_pending(true);
|
|
|
|
break;
|
2022-12-30 15:15:58 +01:00
|
|
|
}
|
2024-01-26 00:17:51 +01:00
|
|
|
update_meta_state(old_mods);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mods_changed(Pointers.Modifiers mods)
|
|
|
|
{
|
|
|
|
update_meta_state(mods);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Update [_mods] to be consistent with the [mods], sending key events if
|
|
|
|
needed. */
|
|
|
|
void update_meta_state(Pointers.Modifiers mods)
|
|
|
|
{
|
|
|
|
// Released modifiers
|
|
|
|
Iterator<KeyValue.Modifier> it = _mods.diff(mods);
|
|
|
|
while (it.hasNext())
|
|
|
|
sendMetaKeyForModifier(it.next(), false);
|
|
|
|
// Activated modifiers
|
|
|
|
it = mods.diff(_mods);
|
|
|
|
while (it.hasNext())
|
|
|
|
sendMetaKeyForModifier(it.next(), true);
|
|
|
|
_mods = mods;
|
2022-12-30 15:15:58 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 17:47:18 +01:00
|
|
|
// private void handleDelKey(int before, int after)
|
|
|
|
// {
|
|
|
|
// CharSequence selection = getCurrentInputConnection().getSelectedText(0);
|
|
|
|
|
|
|
|
// if (selection != null && selection.length() > 0)
|
|
|
|
// getCurrentInputConnection().commitText("", 1);
|
|
|
|
// else
|
|
|
|
// getCurrentInputConnection().deleteSurroundingText(before, after);
|
|
|
|
// }
|
|
|
|
|
2024-01-26 00:17:51 +01:00
|
|
|
void sendMetaKey(int eventCode, int meta_flags, boolean down)
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
2024-01-26 00:17:51 +01:00
|
|
|
if (down)
|
|
|
|
{
|
|
|
|
_meta_state = _meta_state | meta_flags;
|
|
|
|
send_keyevent(KeyEvent.ACTION_DOWN, eventCode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send_keyevent(KeyEvent.ACTION_UP, eventCode);
|
|
|
|
_meta_state = _meta_state & ~meta_flags;
|
|
|
|
}
|
2022-02-22 19:32:16 +01:00
|
|
|
}
|
|
|
|
|
2024-01-26 00:17:51 +01:00
|
|
|
void sendMetaKeyForModifier(KeyValue.Modifier mod, boolean down)
|
2022-02-22 19:32:16 +01:00
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
switch (mod)
|
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
case CTRL:
|
2024-01-26 00:17:51 +01:00
|
|
|
sendMetaKey(KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON, down);
|
|
|
|
break;
|
2022-06-05 19:30:53 +02:00
|
|
|
case ALT:
|
2024-01-26 00:17:51 +01:00
|
|
|
sendMetaKey(KeyEvent.KEYCODE_ALT_LEFT, KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON, down);
|
|
|
|
break;
|
2022-06-05 19:30:53 +02:00
|
|
|
case SHIFT:
|
2024-01-26 00:17:51 +01:00
|
|
|
sendMetaKey(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON, down);
|
|
|
|
break;
|
2022-06-05 19:30:53 +02:00
|
|
|
case META:
|
2024-01-26 00:17:51 +01:00
|
|
|
sendMetaKey(KeyEvent.KEYCODE_META_LEFT, KeyEvent.META_META_LEFT_ON | KeyEvent.META_META_ON, down);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-06-05 01:38:42 +02:00
|
|
|
}
|
2021-12-28 17:47:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 19:32:16 +01:00
|
|
|
/*
|
|
|
|
* Don't set KeyEvent.FLAG_SOFT_KEYBOARD.
|
|
|
|
*/
|
2024-01-26 00:17:51 +01:00
|
|
|
void send_key_down_up(int keyCode)
|
2022-11-13 16:18:08 +01:00
|
|
|
{
|
2024-01-26 00:17:51 +01:00
|
|
|
send_keyevent(KeyEvent.ACTION_DOWN, keyCode);
|
|
|
|
send_keyevent(KeyEvent.ACTION_UP, keyCode);
|
2022-11-13 16:18:08 +01:00
|
|
|
}
|
|
|
|
|
2024-01-26 00:17:51 +01:00
|
|
|
void send_keyevent(int eventAction, int eventCode)
|
2022-11-13 16:18:08 +01:00
|
|
|
{
|
|
|
|
InputConnection conn = _recv.getCurrentInputConnection();
|
|
|
|
if (conn == null)
|
|
|
|
return;
|
2024-01-26 00:17:51 +01:00
|
|
|
conn.sendKeyEvent(new KeyEvent(1, 1, eventAction, eventCode, 0, _meta_state));
|
2022-11-13 16:18:08 +01:00
|
|
|
if (eventAction == KeyEvent.ACTION_UP)
|
2024-01-26 00:17:51 +01:00
|
|
|
_autocap.event_sent(eventCode, _meta_state);
|
2022-11-13 16:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void send_text(CharSequence text)
|
|
|
|
{
|
|
|
|
InputConnection conn = _recv.getCurrentInputConnection();
|
|
|
|
if (conn == null)
|
|
|
|
return;
|
|
|
|
conn.commitText(text, 1);
|
|
|
|
_autocap.typed(text);
|
|
|
|
}
|
2022-02-22 19:32:16 +01:00
|
|
|
|
2022-11-13 16:45:57 +01:00
|
|
|
/** See {!InputConnection.performContextMenuAction}. */
|
|
|
|
void send_context_menu_action(int id)
|
|
|
|
{
|
|
|
|
InputConnection conn = _recv.getCurrentInputConnection();
|
|
|
|
if (conn == null)
|
|
|
|
return;
|
|
|
|
conn.performContextMenuAction(id);
|
|
|
|
}
|
|
|
|
|
2024-02-10 18:03:09 +01:00
|
|
|
@SuppressLint("InlinedApi")
|
2024-01-26 00:17:51 +01:00
|
|
|
void handle_editing_key(KeyValue.Editing ev)
|
2023-08-17 12:47:44 +02:00
|
|
|
{
|
|
|
|
switch (ev)
|
|
|
|
{
|
|
|
|
case COPY: send_context_menu_action(android.R.id.copy); break;
|
|
|
|
case PASTE: send_context_menu_action(android.R.id.paste); break;
|
|
|
|
case CUT: send_context_menu_action(android.R.id.cut); break;
|
|
|
|
case SELECT_ALL: send_context_menu_action(android.R.id.selectAll); break;
|
|
|
|
case SHARE: send_context_menu_action(android.R.id.shareText); break;
|
|
|
|
case PASTE_PLAIN: send_context_menu_action(android.R.id.pasteAsPlainText); break;
|
|
|
|
case UNDO: send_context_menu_action(android.R.id.undo); break;
|
|
|
|
case REDO: send_context_menu_action(android.R.id.redo); break;
|
|
|
|
case REPLACE: send_context_menu_action(android.R.id.replaceText); break;
|
|
|
|
case ASSIST: send_context_menu_action(android.R.id.textAssist); break;
|
|
|
|
case AUTOFILL: send_context_menu_action(android.R.id.autofill); break;
|
2024-01-26 00:17:51 +01:00
|
|
|
case CURSOR_LEFT: move_cursor(-1); break;
|
|
|
|
case CURSOR_RIGHT: move_cursor(1); break;
|
2023-08-17 12:47:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExtractedTextRequest _move_cursor_req = null;
|
|
|
|
|
|
|
|
/** Query the cursor position. The extracted text is empty. Returns [null] if
|
|
|
|
the editor doesn't support this operation. */
|
|
|
|
ExtractedText get_cursor_pos(InputConnection conn)
|
|
|
|
{
|
|
|
|
if (_move_cursor_req == null)
|
|
|
|
{
|
|
|
|
_move_cursor_req = new ExtractedTextRequest();
|
|
|
|
_move_cursor_req.hintMaxChars = 0;
|
|
|
|
}
|
|
|
|
return conn.getExtractedText(_move_cursor_req, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Move the cursor right or left, if possible without sending key events.
|
2023-08-26 23:44:41 +02:00
|
|
|
Unlike arrow keys, the selection is not removed even if shift is not on.
|
|
|
|
Falls back to sending arrow keys events if the editor do not support
|
|
|
|
moving the cursor or a modifier other than shift is pressed. */
|
2024-01-26 00:17:51 +01:00
|
|
|
void move_cursor(int d)
|
2023-08-17 12:47:44 +02:00
|
|
|
{
|
|
|
|
InputConnection conn = _recv.getCurrentInputConnection();
|
|
|
|
if (conn == null)
|
|
|
|
return;
|
|
|
|
ExtractedText et = get_cursor_pos(conn);
|
2023-08-26 23:44:41 +02:00
|
|
|
// Fallback to sending key events
|
2024-02-04 00:27:02 +01:00
|
|
|
if (_move_cursor_force_fallback
|
|
|
|
|| et == null
|
2024-01-26 00:17:51 +01:00
|
|
|
|| _mods.has(KeyValue.Modifier.CTRL)
|
|
|
|
|| _mods.has(KeyValue.Modifier.ALT)
|
|
|
|
|| _mods.has(KeyValue.Modifier.META))
|
2023-08-17 12:47:44 +02:00
|
|
|
{
|
2024-01-26 00:17:51 +01:00
|
|
|
move_cursor_fallback(d);
|
2023-08-17 12:47:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int sel_start = et.selectionStart;
|
|
|
|
int sel_end = et.selectionEnd;
|
|
|
|
// Continue expanding the selection even if shift is not pressed
|
|
|
|
if (sel_end != sel_start)
|
|
|
|
{
|
|
|
|
sel_end += d;
|
|
|
|
if (sel_end == sel_start) // Avoid making the selection empty
|
|
|
|
sel_end += d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sel_end += d;
|
|
|
|
// Leave 'sel_start' where it is if shift is pressed
|
2024-01-26 00:17:51 +01:00
|
|
|
if (!_mods.has(KeyValue.Modifier.SHIFT))
|
2023-08-17 12:47:44 +02:00
|
|
|
sel_start = sel_end;
|
|
|
|
}
|
2024-02-04 00:27:02 +01:00
|
|
|
if (!conn.setSelection(sel_start, sel_end))
|
|
|
|
move_cursor_fallback(d);
|
2023-08-17 12:47:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Send arrow keys as a fallback for editors that do not support
|
|
|
|
[getExtractedText] like Termux. */
|
2024-01-26 00:17:51 +01:00
|
|
|
void move_cursor_fallback(int d)
|
2023-08-17 12:47:44 +02:00
|
|
|
{
|
|
|
|
while (d < 0)
|
|
|
|
{
|
2024-01-26 00:17:51 +01:00
|
|
|
send_key_down_up(KeyEvent.KEYCODE_DPAD_LEFT);
|
2023-08-17 12:47:44 +02:00
|
|
|
d++;
|
|
|
|
}
|
|
|
|
while (d > 0)
|
|
|
|
{
|
2024-01-26 00:17:51 +01:00
|
|
|
send_key_down_up(KeyEvent.KEYCODE_DPAD_RIGHT);
|
2023-08-17 12:47:44 +02:00
|
|
|
d--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 17:47:18 +01:00
|
|
|
public static interface IReceiver
|
|
|
|
{
|
2023-06-03 20:06:44 +02:00
|
|
|
public void handle_event_key(KeyValue.Event ev);
|
2022-11-13 16:18:08 +01:00
|
|
|
public void set_shift_state(boolean state, boolean lock);
|
2024-02-11 20:46:36 +01:00
|
|
|
public void set_compose_pending(boolean pending);
|
2022-11-13 16:18:08 +01:00
|
|
|
public InputConnection getCurrentInputConnection();
|
|
|
|
}
|
2021-12-28 17:47:18 +01:00
|
|
|
|
2022-11-13 16:18:08 +01:00
|
|
|
class Autocapitalisation_callback implements Autocapitalisation.Callback
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void update_shift_state(boolean should_enable, boolean should_disable)
|
|
|
|
{
|
|
|
|
if (should_enable)
|
|
|
|
_recv.set_shift_state(true, false);
|
|
|
|
else if (should_disable)
|
|
|
|
_recv.set_shift_state(false, false);
|
|
|
|
}
|
2021-12-28 17:47:18 +01:00
|
|
|
}
|
|
|
|
}
|