Send keys to the application

This commit is contained in:
juloo 2015-08-01 21:36:40 +02:00
parent 3b7141e3a0
commit 349f0bee6f
6 changed files with 98 additions and 45 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="bg" type="color">#111111</item>
<item name="key_bg" type="color">#303030</item>
<item name="key_down_bg" type="color">#FFFFFF</item>
<item name="key_label" type="color">#DFDFDF</item>
<item name="key_sub_label" type="color">#BFBFBF</item>
<item name="bg" type="color">#191919</item>
<item name="key_bg" type="color">#282828</item>
<item name="key_down_bg" type="color">#1B1B1B</item>
<item name="key_label" type="color">#DDDDDD</item>
<item name="key_sub_label" type="color">#BDBDBD</item>
</resources>

View File

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="vertical_margin">2dp</dimen>
<dimen name="vertical_margin">3dp</dimen>
<dimen name="horizontal_margin">2dp</dimen>
<dimen name="key_padding">5dp</dimen>
<dimen name="key_bg_padding">1dp</dimen>
<dimen name="key_height">44dp</dimen>
<dimen name="key_round">3dp</dimen>
<dimen name="label_text_size">16dp</dimen>
<dimen name="sublabel_text_size">10dp</dimen>
</resources>

View File

@ -33,12 +33,12 @@
<key key0="b" key2="/" key4=":" />
<key key0="n" key2="§" key4="!" />
<key key1="up" key2="right" key3="left" key4="down" />
<key width="1.6" key0="back" key2="delete" />
<key width="1.6" key0="backspace" key2="delete" />
</row>
<row>
<key width="1.7" key0="ctrl" />
<key width="1.3" key0="alt" key1="page_up" key2="end" key3="home" key4="page_down" />
<key width="4.0" key0="space" />
<key width="3.0" key0="return" />
<key width="1.6" key0="ctrl" />
<key width="1.0" key0="alt" key1="page_up" key2="end" key3="home" key4="page_down" />
<key width="4.8" key0="space" />
<key width="2.6" key0="enter" />
</row>
</keyboard>

View File

@ -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');
new KeyValue(chars.substring(i, i + 1));
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("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", " ", ' ');
}
}

View File

@ -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)

View File

@ -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)