diff options
| author | juloo | 2015-08-01 21:36:40 +0200 |
|---|---|---|
| committer | juloo | 2015-08-01 21:36:40 +0200 |
| commit | 349f0bee6fc203975aeb3dc1f6324cc5f671265e (patch) | |
| tree | 1a3327db80427bac9e9f596b6c14de08d91d03d7 /srcs | |
| parent | 3b7141e3a05e44a907ab403ec5de52a1ad988224 (diff) | |
| download | unexpected-keyboard-349f0bee6fc203975aeb3dc1f6324cc5f671265e.tar.gz unexpected-keyboard-349f0bee6fc203975aeb3dc1f6324cc5f671265e.zip | |
Send keys to the application
Diffstat (limited to 'srcs')
| -rw-r--r-- | srcs/juloo.keyboard2/KeyValue.java | 71 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2.java | 21 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2View.java | 30 |
3 files changed, 87 insertions, 35 deletions
diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 1b6ef23..e4beae5 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -5,9 +5,14 @@ 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; + private String _name; private String _symbol; private char _char; + private int _eventCode; public String getName() { @@ -24,13 +29,35 @@ class KeyValue return (_char); } + public int getEventCode() + { + return (_eventCode); + } + private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>(); + private KeyValue(String name) + { + this(name, name, name.charAt(0), EVENT_NONE); + } + private KeyValue(String name, String symbol, char c) { + this(name, symbol, c, EVENT_NONE); + } + + private KeyValue(String name, String symbol, int eventCode) + { + this(name, symbol, name.charAt(0), eventCode); + } + + private KeyValue(String name, String symbol, char c, int eventCode) + { _name = name; _symbol = symbol; _char = c; + _eventCode = eventCode; + KeyValue.keys.put(name, this); } public static KeyValue getKeyByName(String name) @@ -38,11 +65,6 @@ class KeyValue return (KeyValue.keys.get(name)); } - private static void add(String name, String symbol, char c) - { - keys.put(name, new KeyValue(name, symbol, c)); - } - static { String chars = "abcdefghijklmnopqrstuvwxyz" @@ -52,23 +74,26 @@ class KeyValue + "~#{[|`\\^@]}" + "^$ù*,;:!¨£%µ?./§"; for (int i = 0; i < chars.length(); i++) - add(chars.substring(i, i + 1), chars.substring(i, i + 1), chars.charAt(i)); - add("shift", "Shift", 'S'); - add("ctrl", "Ctrl", 'C'); - add("alt", "Alt", 'A'); - - add("back", "⌫", '\u007F'); - add("up", "↑", 'U'); - add("right", "→", 'R'); - add("down", "↓", 'D'); - add("left", "←", 'L'); - add("page_up", "⇞", 'U'); - add("page_down", "⇟", 'D'); - add("home", "↖", 'H'); - add("end", "↗", 'E'); - add("tab", "↹", '\t'); - add("return", "↵", '\n'); - add("space", " ", ' '); - add("delete", "⌦", 'D'); + new KeyValue(chars.substring(i, i + 1)); + + new KeyValue("shift", "Shift", EVENT_NONE); + new KeyValue("ctrl", "Ctrl", EVENT_NONE); + new KeyValue("alt", "Alt", EVENT_NONE); + + new KeyValue("backspace", "⌫", EVENT_BACKSPACE); + new KeyValue("delete", "⌦", EVENT_DELETE); + + 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("tab", "↹", '\t'); + new KeyValue("space", " ", ' '); } } diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 38a9294..ce2f011 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -27,9 +27,26 @@ public class Keyboard2 extends InputMethodService return (_inputView); } - public void handleKey(KeyValue key) + public void handleKeyUp(KeyValue key) { - Keyboard2.log("Key up " + key.getName()); + int eventCode = key.getEventCode(); + + switch (eventCode) + { + case KeyValue.EVENT_NONE: + sendKeyChar(key.getChar()); + 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 ; + } } public static void log(String str) diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 4e429b0..2154fd9 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -2,6 +2,7 @@ package juloo.keyboard2; import android.content.Context; import android.graphics.Canvas; +import android.graphics.RectF; import android.graphics.Paint; import android.util.AttributeSet; import android.util.DisplayMetrics; @@ -26,6 +27,7 @@ public class Keyboard2View extends View private float _keyHeight; private float _keyPadding; private float _keyBgPadding; + private float _keyRound; private Paint _keyBgPaint; private Paint _keyDownBgPaint; @@ -42,6 +44,7 @@ public class Keyboard2View extends View _keyHeight = getResources().getDimension(R.dimen.key_height); _keyPadding = getResources().getDimension(R.dimen.key_padding); _keyBgPadding = getResources().getDimension(R.dimen.key_bg_padding); + _keyRound = getResources().getDimension(R.dimen.key_round); _keyWidth = (dm.widthPixels - (_horizontalMargin * 2)) / KEY_PER_ROW; _keyBgPaint = new Paint(); _keyBgPaint.setColor(getResources().getColor(R.color.key_bg)); @@ -118,7 +121,9 @@ public class Keyboard2View extends View KeyDown k = getKeyDown(pointerId); if (k != null) + { k.updateDown(moveX, moveY); + } } private void onTouchDown(float touchX, float touchY, int pointerId) @@ -155,7 +160,7 @@ public class Keyboard2View extends View if (k != null) { if (k.value != null) - _ime.handleKey(k.value); + _ime.handleKeyUp(k.value); _downKeys.remove(k); invalidate(); return ; @@ -192,8 +197,8 @@ public class Keyboard2View extends View canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding, x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyDownBgPaint); else - canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding, - x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyBgPaint); + canvas.drawRoundRect(new RectF(x + _keyBgPadding, y + _keyBgPadding, + x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding), _keyRound, _keyRound, _keyBgPaint); if (k.key0 != null) canvas.drawText(k.key0.getSymbol(), keyW / 2 + x, (_keyHeight + _keyLabelPaint.getTextSize()) / 2 + y, _keyLabelPaint); @@ -231,17 +236,22 @@ public class Keyboard2View extends View public KeyDown(int pointerId, KeyboardData.Key key, float x, float y) { this.pointerId = pointerId; - this.value = key.key0; + value = key.key0; this.key = key; - this.downX = x; - this.downY = y; + downX = x; + downY = y; } - public void updateDown(float x, float y) + public boolean updateDown(float x, float y) { - value = getDownValue(x - downX, y - downY); - if (value == null) - value = key.key0; + KeyValue newValue = getDownValue(x - downX, y - downY); + + if (newValue != null && newValue != value) + { + value = newValue; + return (true); + } + return (false); } private KeyValue getDownValue(float x, float y) |
