diff options
Diffstat (limited to 'srcs/juloo.keyboard2/KeyEventHandler.java')
| -rw-r--r-- | srcs/juloo.keyboard2/KeyEventHandler.java | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index 45db719..471fad0 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -262,12 +262,15 @@ public final class KeyEventHandler return conn.getExtractedText(_move_cursor_req, 0); } + /** [repeatition] might be negative, in which case the direction is reversed. */ void handle_slider(KeyValue.Slider s, int repeatition) { switch (s) { case Cursor_left: move_cursor(-repeatition); break; case Cursor_right: move_cursor(repeatition); break; + case Cursor_up: move_cursor_vertical(-repeatition); break; + case Cursor_down: move_cursor_vertical(repeatition); break; } } @@ -283,47 +286,51 @@ public final class KeyEventHandler ExtractedText et = get_cursor_pos(conn); int system_mods = KeyEvent.META_CTRL_ON | KeyEvent.META_ALT_ON | KeyEvent.META_META_ON; - // Fallback to sending key events - if (_move_cursor_force_fallback || et == null - || (_meta_state & system_mods) != 0) + // Fallback to sending key events if system modifiers are activated or + // ExtractedText is not supported, for example on Termux. + if (!_move_cursor_force_fallback && et != null + && (_meta_state & system_mods) == 0) { - move_cursor_fallback(d); - 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 + 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 + if ((_meta_state & KeyEvent.META_SHIFT_ON) == 0) + sel_start = sel_end; + } + if (conn.setSelection(sel_start, sel_end)) + return; // [setSelection] succeeded, don't fallback to key events } + if (d < 0) + send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_LEFT, -d); else - { - sel_end += d; - // Leave 'sel_start' where it is if shift is pressed - if ((_meta_state & KeyEvent.META_SHIFT_ON) == 0) - sel_start = sel_end; - } - if (!conn.setSelection(sel_start, sel_end)) - move_cursor_fallback(d); + send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_RIGHT, d); } - /** Send arrow keys as a fallback for editors that do not support - [getExtractedText] like Termux. */ - void move_cursor_fallback(int d) + /** Move the cursor up and down. This sends UP and DOWN key events that might + make the focus exit the text box. */ + void move_cursor_vertical(int d) { - while (d < 0) - { - send_key_down_up(KeyEvent.KEYCODE_DPAD_LEFT); - d++; - } - while (d > 0) - { - send_key_down_up(KeyEvent.KEYCODE_DPAD_RIGHT); - d--; - } + if (d < 0) + send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_UP, -d); + else + send_key_down_up_repeat(KeyEvent.KEYCODE_DPAD_DOWN, d); + } + + /** Repeat calls to [send_key_down_up]. */ + void send_key_down_up_repeat(int event_code, int repeat) + { + while (repeat-- > 0) + send_key_down_up(event_code); } public static interface IReceiver |
