Improve KeyValue

This commit is contained in:
juloo 2015-08-01 16:33:30 +02:00
parent 2537a7b221
commit 0942556bad
4 changed files with 87 additions and 88 deletions

View File

@ -32,14 +32,13 @@
<key key0="v" key2="." key4=";" />
<key key0="b" key2="/" key4=":" />
<key key0="n" key2="§" key4="!" />
<key key0="n" key2="§" key4="!" />
<key width="1.6" key0="back" key2="del" />
<key key1="up" key2="right" key3="left" key4="down" />
<key width="1.6" key0="back" 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=" " />
<key key1="up" key2="right" key3="down" key4="left" />
<key width="2.0" key0="return" />
<key width="4.0" key0="space" />
<key width="3.0" key0="return" />
</row>
</keyboard>

View File

@ -5,97 +5,70 @@ import java.util.HashMap;
class KeyValue
{
// KEY_A("a", 'a'),
// KEY_A2("à", 'à'),
// KEY_B("b", 'b'),
// KEY_C("c", 'c'),
// KEY_C2("ç", 'ç'),
// KEY_D("d", 'd'),
// KEY_E("e", 'e'),
// KEY_E2("é", 'é'),
// KEY_E3("è", 'è'),
// KEY_F("f", 'f'),
// KEY_G("g", 'g'),
// KEY_H("h", 'h'),
// KEY_I("i", 'i'),
// KEY_J("j", 'j'),
// KEY_K("k", 'k'),
// KEY_L("l", 'l'),
// KEY_M("m", 'm'),
// KEY_N("n", 'n'),
// KEY_O("o", 'o'),
// KEY_P("p", 'p'),
// KEY_Q("q", 'q'),
// KEY_R("r", 'r'),
// KEY_S("s", 's'),
// KEY_T("t", 't'),
// KEY_U("u", 'u'),
// KEY_V("v", 'v'),
// KEY_W("w", 'w'),
// KEY_X("x", 'x'),
// KEY_Y("y", 'y'),
// KEY_Z("z", 'z'),
// KEY_0("0", '0'),
// KEY_1("1", '1'),
// KEY_2("2", '2'),
// KEY_3("3", '3'),
// KEY_4("4", '4'),
// KEY_5("5", '5'),
// KEY_6("6", '6'),
// KEY_7("7", '7'),
// KEY_8("8", '8'),
// KEY_9("9", '9'),
// KEY_AND("&", '&'),
// KEY_DQUOTE("\"", '"'),
// KEY_QUOTE("'", '\''),
// KEY_PARENTHESIS("(", '('),
// KEY_MINUS("-", '-'),
// KEY_UNDERSCORE("_", '_'),
// KEY_TILDE("~", '~'),
// KEY_DIESE("#", '#'),
// KEY_BLOCK("{", '{'),
// KEY_SQUARE("[", '['),
// KEY_PIPE("|", '|'),
// KEY_BACKQUOTE("`", '`'),
// KEY_BACKSLASH("\\", '\\'),
// KEY_XOR("^", '^'),
// KEY_AROBASE("@", '@'),
// KEY_ENTER("enter", '\n'),
// KEY_SPACE("space", ' '),
// KEY_DEL("del", '\u007F');
private String _name;
private String _symbol;
private char _char;
private KeyValue(String name, char c)
{
_name = name;
_char = c;
}
public String getName()
{
return (_name);
}
public String getSymbol()
{
return (_symbol);
}
public char getChar()
{
return (_char);
}
// private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>();
private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>();
// static
// {
// for (KeyValue k : KeyValue.values())
// keys.put(k.getName(), k);
// }
private KeyValue(String name, String symbol, char c)
{
_name = name;
_symbol = symbol;
_char = c;
}
public static KeyValue getKeyByName(String name)
{
// return (KeyValue.keys.get(name));
if (name == null || name.length() == 0)
return (null);
return (new KeyValue(name, name.charAt(0)));
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"
+ "àçéèêë"
+ "0123456789<>"
+ "\"'(-_)=°+"
+ "~#{[|`\\^@]}"
+ "^$ù*,;:!¨£%µ?./§";
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');
}
}

View File

@ -27,6 +27,11 @@ public class Keyboard2 extends InputMethodService
return (_inputView);
}
public void handleKey(KeyValue key)
{
Keyboard2.log("Key up " + key.getName());
}
public static void log(String str)
{
Log.d(TAG, str);

View File

@ -17,6 +17,8 @@ public class Keyboard2View extends View
private Keyboard2 _ime;
private KeyboardData _keyboard;
private KeyValue _downValue;
private float _verticalMargin;
private float _horizontalMargin;
private float _keyWidth;
@ -33,6 +35,7 @@ public class Keyboard2View extends View
{
super(context, attrs);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
_downValue = null;
_verticalMargin = getResources().getDimension(R.dimen.vertical_margin);
_horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin);
_keyHeight = getResources().getDimension(R.dimen.key_height);
@ -101,7 +104,8 @@ public class Keyboard2View extends View
if (v != k.downValue)
{
k.downValue = v;
Keyboard2.log("Key down " + v.getName());
if (v != null)
_downValue = v;
}
}
}
@ -127,7 +131,7 @@ public class Keyboard2View extends View
if (touchX >= x && touchX < (x + keyW) && k.downPointer == -1)
{
if (k.key0 != null)
Keyboard2.log("Key down " + k.key0.getName());
_downValue = k.key0;
k.downPointer = pointerId;
k.downValue = k.key0;
k.downX = touchX;
@ -149,9 +153,27 @@ public class Keyboard2View extends View
if (k.downPointer == pointerId)
{
if (k.downValue != null)
Keyboard2.log("Key up " + k.downValue.getName());
_ime.handleKey(k.downValue);
_downValue = null;
k.downPointer = -1;
nextDownValue();
invalidate();
return ;
}
}
}
}
private void nextDownValue()
{
for (keyboardData.Row row : _keyboard.getRows())
{
for (KeyboardData.Key k : row)
{
if (k.downPointer != -1)
{
_downValue = k.downValue;
return ;
}
}
}
@ -190,22 +212,22 @@ public class Keyboard2View extends View
canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding,
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyBgPaint);
if (k.key0 != null)
canvas.drawText(k.key0.getName(), keyW / 2 + x,
canvas.drawText(k.key0.getSymbol(), keyW / 2 + x,
(_keyHeight + _keyLabelPaint.getTextSize()) / 2 + y, _keyLabelPaint);
float textOffsetY = _keySubLabelPaint.getTextSize() / 2;
float subPadding = _keyPadding + _keyBgPadding;
if (k.key1 != null)
canvas.drawText(k.key1.getName(), x + subPadding,
canvas.drawText(k.key1.getSymbol(), x + subPadding,
y + subPadding + textOffsetY, _keySubLabelPaint);
if (k.key2 != null)
canvas.drawText(k.key2.getName(), x + keyW - subPadding,
canvas.drawText(k.key2.getSymbol(), x + keyW - subPadding,
y + subPadding + textOffsetY, _keySubLabelPaint);
textOffsetY /= 2; // lol
if (k.key3 != null)
canvas.drawText(k.key3.getName(), x + subPadding,
canvas.drawText(k.key3.getSymbol(), x + subPadding,
y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint);
if (k.key4 != null)
canvas.drawText(k.key4.getName(), x + keyW - subPadding,
canvas.drawText(k.key4.getSymbol(), x + keyW - subPadding,
y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint);
x += keyW;
}