From 85eb9daa46f169375d553c8b6f8f75b7568c17f6 Mon Sep 17 00:00:00 2001 From: juloo Date: Sun, 2 Aug 2015 21:32:11 +0200 Subject: [PATCH] Send Ctrl/Alt(/Shift) to the application --- srcs/juloo.keyboard2/KeyValue.java | 111 +++++++++++++++++----------- srcs/juloo.keyboard2/Keyboard2.java | 50 +++++++++---- 2 files changed, 100 insertions(+), 61 deletions(-) diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 9c25673..b7331df 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -5,17 +5,16 @@ import java.util.HashMap; class KeyValue { - public static final int EVENT_NONE = -1; - public static final int EVENT_BACKSPACE = -2; - public static final int EVENT_DELETE = -3; + public static final int EVENT_NONE = -1; + public static final char CHAR_NONE = '\0'; - public static final int FLAG_KEEP_ON = 1; - public static final int FLAG_LOCK = (1 << 1); - public static final int FLAG_CTRL = (1 << 2); - public static final int FLAG_SHIFT = (1 << 3); - public static final int FLAG_ALT = (1 << 4); - public static final int FLAG_NOCHAR = (1 << 5); - public static final int FLAG_LOCKED = (1 << 8); + public static final int FLAG_KEEP_ON = 1; + public static final int FLAG_LOCK = (1 << 1); + public static final int FLAG_CTRL = (1 << 2); + public static final int FLAG_SHIFT = (1 << 3); + public static final int FLAG_ALT = (1 << 4); + public static final int FLAG_NOCHAR = (1 << 5); + public static final int FLAG_LOCKED = (1 << 8); private String _name; private String _symbol; @@ -54,21 +53,6 @@ class KeyValue private static HashMap keys = new HashMap(); - private KeyValue(String name) - { - this(name, name, name.charAt(0), EVENT_NONE, 0); - } - - private KeyValue(String name, String symbol, char c) - { - this(name, symbol, c, EVENT_NONE, 0); - } - - private KeyValue(String name, String symbol, int eventCode) - { - this(name, symbol, '\0', eventCode, 0); - } - private KeyValue(String name, String symbol, char c, int eventCode, int flags) { _name = name; @@ -86,33 +70,70 @@ class KeyValue static { - String chars = "abcdefghijklmnopqrstuvwxyz" - + "àçéèêë" - + "0123456789<>" + String chars = "àçéèêë<>" + "&é\"'(-_)=°+" + "~#{[|`\\^@]}" + "^$ù*,;:!¨£%µ?./§"; for (int i = 0; i < chars.length(); i++) - new KeyValue(chars.substring(i, i + 1)); + { + String key = chars.substring(i, i + 1); + new KeyValue(key, key, key.charAt(0), EVENT_NONE, 0); + } - new KeyValue("shift", "⇧", 'S', EVENT_NONE, FLAG_KEEP_ON | FLAG_NOCHAR | FLAG_LOCK | FLAG_SHIFT); - new KeyValue("ctrl", "Ctrl", 'C', EVENT_NONE, FLAG_KEEP_ON | FLAG_NOCHAR | FLAG_CTRL); - new KeyValue("alt", "Alt", 'A', EVENT_NONE, FLAG_KEEP_ON | FLAG_NOCHAR | FLAG_ALT); + new KeyValue("shift", "⇧", CHAR_NONE, EVENT_NONE, FLAG_KEEP_ON | FLAG_NOCHAR | FLAG_LOCK | FLAG_SHIFT); + new KeyValue("ctrl", "Ctrl", CHAR_NONE, EVENT_NONE, FLAG_KEEP_ON | FLAG_NOCHAR | FLAG_CTRL); + new KeyValue("alt", "Alt", CHAR_NONE, EVENT_NONE, FLAG_KEEP_ON | FLAG_NOCHAR | FLAG_ALT); - new KeyValue("backspace", "⌫", EVENT_BACKSPACE); - new KeyValue("delete", "⌦", EVENT_DELETE); + new KeyValue("a", "a", 'a', KeyEvent.KEYCODE_A, 0); + new KeyValue("b", "b", 'b', KeyEvent.KEYCODE_B, 0); + new KeyValue("c", "c", 'c', KeyEvent.KEYCODE_C, 0); + new KeyValue("d", "d", 'd', KeyEvent.KEYCODE_D, 0); + new KeyValue("e", "e", 'e', KeyEvent.KEYCODE_E, 0); + new KeyValue("f", "f", 'f', KeyEvent.KEYCODE_F, 0); + new KeyValue("g", "g", 'g', KeyEvent.KEYCODE_G, 0); + new KeyValue("h", "h", 'h', KeyEvent.KEYCODE_H, 0); + new KeyValue("i", "i", 'i', KeyEvent.KEYCODE_I, 0); + new KeyValue("j", "j", 'j', KeyEvent.KEYCODE_J, 0); + new KeyValue("k", "k", 'k', KeyEvent.KEYCODE_K, 0); + new KeyValue("l", "l", 'l', KeyEvent.KEYCODE_L, 0); + new KeyValue("m", "m", 'm', KeyEvent.KEYCODE_M, 0); + new KeyValue("n", "n", 'n', KeyEvent.KEYCODE_N, 0); + new KeyValue("o", "o", 'o', KeyEvent.KEYCODE_O, 0); + new KeyValue("p", "p", 'p', KeyEvent.KEYCODE_P, 0); + new KeyValue("q", "q", 'q', KeyEvent.KEYCODE_Q, 0); + new KeyValue("r", "r", 'r', KeyEvent.KEYCODE_R, 0); + new KeyValue("s", "s", 's', KeyEvent.KEYCODE_S, 0); + new KeyValue("t", "t", 't', KeyEvent.KEYCODE_T, 0); + new KeyValue("u", "u", 'u', KeyEvent.KEYCODE_U, 0); + new KeyValue("v", "v", 'v', KeyEvent.KEYCODE_V, 0); + new KeyValue("w", "w", 'w', KeyEvent.KEYCODE_W, 0); + new KeyValue("x", "x", 'x', KeyEvent.KEYCODE_X, 0); + new KeyValue("y", "y", 'y', KeyEvent.KEYCODE_Y, 0); + new KeyValue("z", "z", 'z', KeyEvent.KEYCODE_Z, 0); + new KeyValue("0", "0", '0', KeyEvent.KEYCODE_0, 0); + new KeyValue("1", "1", '1', KeyEvent.KEYCODE_1, 0); + new KeyValue("2", "2", '2', KeyEvent.KEYCODE_2, 0); + new KeyValue("3", "3", '3', KeyEvent.KEYCODE_3, 0); + new KeyValue("4", "4", '4', KeyEvent.KEYCODE_4, 0); + new KeyValue("5", "5", '5', KeyEvent.KEYCODE_5, 0); + new KeyValue("6", "6", '6', KeyEvent.KEYCODE_6, 0); + new KeyValue("7", "7", '7', KeyEvent.KEYCODE_7, 0); + new KeyValue("8", "8", '8', KeyEvent.KEYCODE_8, 0); + new KeyValue("9", "9", '9', KeyEvent.KEYCODE_9, 0); - new KeyValue("enter", "↵", KeyEvent.KEYCODE_ENTER); - new KeyValue("up", "↑", KeyEvent.KEYCODE_DPAD_UP); - new KeyValue("right", "→", KeyEvent.KEYCODE_DPAD_RIGHT); - new KeyValue("down", "↓", KeyEvent.KEYCODE_DPAD_DOWN); - new KeyValue("left", "←", KeyEvent.KEYCODE_DPAD_LEFT); - new KeyValue("page_up", "⇞", KeyEvent.KEYCODE_PAGE_DOWN); - new KeyValue("page_down", "⇟", KeyEvent.KEYCODE_PAGE_UP); - new KeyValue("home", "↖", KeyEvent.KEYCODE_HOME); - new KeyValue("end", "↗", KeyEvent.KEYCODE_MOVE_END); + new KeyValue("enter", "↵", CHAR_NONE, KeyEvent.KEYCODE_ENTER, 0); + new KeyValue("up", "↑", CHAR_NONE, KeyEvent.KEYCODE_DPAD_UP, 0); + new KeyValue("right", "→", CHAR_NONE, KeyEvent.KEYCODE_DPAD_RIGHT, 0); + new KeyValue("down", "↓", CHAR_NONE, KeyEvent.KEYCODE_DPAD_DOWN, 0); + new KeyValue("left", "←", CHAR_NONE, KeyEvent.KEYCODE_DPAD_LEFT, 0); + new KeyValue("page_up", "⇞", CHAR_NONE, KeyEvent.KEYCODE_PAGE_DOWN, 0); + new KeyValue("page_down", "⇟", CHAR_NONE, KeyEvent.KEYCODE_PAGE_UP, 0); + new KeyValue("home", "↖", CHAR_NONE, KeyEvent.KEYCODE_HOME, 0); + new KeyValue("end", "↗", CHAR_NONE, KeyEvent.KEYCODE_MOVE_END, 0); + new KeyValue("backspace", "⌫", CHAR_NONE, KeyEvent.KEYCODE_DEL, 0); + new KeyValue("delete", "⌦", CHAR_NONE, KeyEvent.KEYCODE_FORWARD_DEL, 0); - new KeyValue("tab", "↹", '\t'); - new KeyValue("space", " ", ' '); + new KeyValue("tab", "↹", '\t', EVENT_NONE, 0); + new KeyValue("space", " ", ' ', EVENT_NONE, 0); } } diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 03f71bf..fd86ccc 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -29,25 +29,43 @@ public class Keyboard2 extends InputMethodService public void handleKeyUp(KeyValue key, int flags) { - int eventCode = key.getEventCode(); - if (getCurrentInputConnection() == null) - return ; // TODO wait a little before give up - switch (eventCode) + return ; + if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT)) != 0) + { + int metaState = 0; + KeyEvent event; + + if (key.getEventCode() == KeyValue.EVENT_NONE) + return ; + if ((flags & KeyValue.FLAG_CTRL) != 0) + metaState |= KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON; + if ((flags & KeyValue.FLAG_ALT) != 0) + metaState |= KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON; + if ((flags & KeyValue.FLAG_SHIFT) != 0) + metaState |= KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON; + event = new KeyEvent(1, 1, KeyEvent.ACTION_DOWN, key.getEventCode(), 1, metaState); + getCurrentInputConnection().sendKeyEvent(event); + getCurrentInputConnection().sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP)); + } + else if (key.getEventCode() == KeyEvent.KEYCODE_DEL) { - case KeyValue.EVENT_NONE: - sendKeyChar(key.getChar((flags & KeyValue.FLAG_SHIFT) != 0)); - break ; - case KeyValue.EVENT_DELETE: - getCurrentInputConnection().deleteSurroundingText(0, 1); - break ; - case KeyValue.EVENT_BACKSPACE: getCurrentInputConnection().deleteSurroundingText(1, 0); - break ; - default: - getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, eventCode)); - getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, eventCode)); - break ; + } + else if (key.getEventCode() == KeyEvent.KEYCODE_FORWARD_DEL) + { + getCurrentInputConnection().deleteSurroundingText(0, 1); + } + else if (key.getChar(false) == KeyValue.CHAR_NONE && key.getEventCode() != KeyValue.EVENT_NONE) + { + KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, key.getEventCode()); + + getCurrentInputConnection().sendKeyEvent(event); + getCurrentInputConnection().sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP)); + } + else + { + sendKeyChar(key.getChar((flags & KeyValue.FLAG_SHIFT) != 0)); } }