Implement shift key

This commit is contained in:
juloo 2015-08-01 23:54:38 +02:00
parent 43408916bb
commit 5bbdbcd522
5 changed files with 72 additions and 35 deletions

View File

@ -5,7 +5,7 @@
<dimen name="key_padding">5dp</dimen> <dimen name="key_padding">5dp</dimen>
<dimen name="key_bg_padding">1dp</dimen> <dimen name="key_bg_padding">1dp</dimen>
<dimen name="key_height">44dp</dimen> <dimen name="key_height">44dp</dimen>
<dimen name="key_round">3dp</dimen> <dimen name="key_round">4dp</dimen>
<dimen name="label_text_size">16dp</dimen> <dimen name="label_text_size">16dp</dimen>
<dimen name="sublabel_text_size">10dp</dimen> <dimen name="sublabel_text_size">10dp</dimen>
</resources> </resources>

View File

@ -17,7 +17,7 @@
<key key0="s" /> <key key0="s" />
<key key0="d" /> <key key0="d" />
<key key0="f" /> <key key0="f" />
<key key0="g" key2="°" key3=")" key4="}" /> <key key0="g" key2="°" key3=")" key4="]" />
<key key0="h" key2="+" key3="=" key4="}" /> <key key0="h" key2="+" key3="=" key4="}" />
<key key0="j" key2="¨" key3="^" /> <key key0="j" key2="¨" key3="^" />
<key key0="k" key1="ë" key2="£" key3="$" key4="ê" /> <key key0="k" key1="ë" key2="£" key3="$" key4="ê" />

View File

@ -9,23 +9,33 @@ class KeyValue
public static final int EVENT_BACKSPACE = -2; public static final int EVENT_BACKSPACE = -2;
public static final int EVENT_DELETE = -3; public static final int EVENT_DELETE = -3;
public static final int FLAG_KEEP_ON = 1;
public static final int FLAG_CTRL = (1 << 1);
public static final int FLAG_SHIFT = (1 << 2);
public static final int FLAG_ALT = (1 << 3);
private String _name; private String _name;
private String _symbol; private String _symbol;
private char _char; private char _char;
private int _eventCode; private int _eventCode;
private int _flags;
public String getName() public String getName()
{ {
return (_name); return (_name);
} }
public String getSymbol() public String getSymbol(boolean upperCase)
{ {
if (upperCase)
return (_symbol.toUpperCase());
return (_symbol); return (_symbol);
} }
public char getChar() public char getChar(boolean upperCase)
{ {
if (upperCase)
return (Character.toUpperCase(_char));
return (_char); return (_char);
} }
@ -34,29 +44,35 @@ class KeyValue
return (_eventCode); return (_eventCode);
} }
public int getFlags()
{
return (_flags);
}
private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>(); private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>();
private KeyValue(String name) private KeyValue(String name)
{ {
this(name, name, name.charAt(0), EVENT_NONE); this(name, name, name.charAt(0), EVENT_NONE, 0);
} }
private KeyValue(String name, String symbol, char c) private KeyValue(String name, String symbol, char c)
{ {
this(name, symbol, c, EVENT_NONE); this(name, symbol, c, EVENT_NONE, 0);
} }
private KeyValue(String name, String symbol, int eventCode) private KeyValue(String name, String symbol, int eventCode)
{ {
this(name, symbol, name.charAt(0), eventCode); this(name, symbol, '\0', eventCode, 0);
} }
private KeyValue(String name, String symbol, char c, int eventCode) private KeyValue(String name, String symbol, char c, int eventCode, int flags)
{ {
_name = name; _name = name;
_symbol = symbol; _symbol = symbol;
_char = c; _char = c;
_eventCode = eventCode; _eventCode = eventCode;
_flags = flags;
KeyValue.keys.put(name, this); KeyValue.keys.put(name, this);
} }
@ -76,9 +92,9 @@ class KeyValue
for (int i = 0; i < chars.length(); i++) for (int i = 0; i < chars.length(); i++)
new KeyValue(chars.substring(i, i + 1)); new KeyValue(chars.substring(i, i + 1));
new KeyValue("shift", "Shift", EVENT_NONE); new KeyValue("shift", "", '\0', EVENT_NONE, FLAG_KEEP_ON | FLAG_SHIFT);
new KeyValue("ctrl", "Ctrl", EVENT_NONE); new KeyValue("ctrl", "Ctrl", '\0', EVENT_NONE, FLAG_KEEP_ON | FLAG_CTRL);
new KeyValue("alt", "Alt", EVENT_NONE); new KeyValue("alt", "Alt", '\0', EVENT_NONE, FLAG_KEEP_ON | FLAG_ALT);
new KeyValue("backspace", "", EVENT_BACKSPACE); new KeyValue("backspace", "", EVENT_BACKSPACE);
new KeyValue("delete", "", EVENT_DELETE); new KeyValue("delete", "", EVENT_DELETE);

View File

@ -27,14 +27,14 @@ public class Keyboard2 extends InputMethodService
return (_inputView); return (_inputView);
} }
public void handleKeyUp(KeyValue key) public void handleKeyUp(KeyValue key, int flags)
{ {
int eventCode = key.getEventCode(); int eventCode = key.getEventCode();
switch (eventCode) switch (eventCode)
{ {
case KeyValue.EVENT_NONE: case KeyValue.EVENT_NONE:
sendKeyChar(key.getChar()); sendKeyChar(key.getChar((flags & KeyValue.FLAG_SHIFT) != 0));
break ; break ;
case KeyValue.EVENT_DELETE: case KeyValue.EVENT_DELETE:
getCurrentInputConnection().deleteSurroundingText(0, 1); getCurrentInputConnection().deleteSurroundingText(0, 1);

View File

@ -6,10 +6,9 @@ import android.graphics.RectF;
import android.graphics.Paint; import android.graphics.Paint;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import java.util.Vector; import java.util.LinkedList;
public class Keyboard2View extends View public class Keyboard2View extends View
implements View.OnTouchListener implements View.OnTouchListener
@ -19,7 +18,9 @@ public class Keyboard2View extends View
private Keyboard2 _ime; private Keyboard2 _ime;
private KeyboardData _keyboard; private KeyboardData _keyboard;
private Vector<KeyDown> _downKeys; private LinkedList<KeyDown> _downKeys = new LinkedList<KeyDown>();
private int _flags = 0;
private float _verticalMargin; private float _verticalMargin;
private float _horizontalMargin; private float _horizontalMargin;
@ -29,16 +30,15 @@ public class Keyboard2View extends View
private float _keyBgPadding; private float _keyBgPadding;
private float _keyRound; private float _keyRound;
private Paint _keyBgPaint; private Paint _keyBgPaint = new Paint();
private Paint _keyDownBgPaint; private Paint _keyDownBgPaint = new Paint();
private Paint _keyLabelPaint; private Paint _keyLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint _keySubLabelPaint; private Paint _keySubLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Keyboard2View(Context context, AttributeSet attrs) public Keyboard2View(Context context, AttributeSet attrs)
{ {
super(context, attrs); super(context, attrs);
DisplayMetrics dm = context.getResources().getDisplayMetrics(); DisplayMetrics dm = context.getResources().getDisplayMetrics();
_downKeys = new Vector<KeyDown>();
_verticalMargin = getResources().getDimension(R.dimen.vertical_margin); _verticalMargin = getResources().getDimension(R.dimen.vertical_margin);
_horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin); _horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin);
_keyHeight = getResources().getDimension(R.dimen.key_height); _keyHeight = getResources().getDimension(R.dimen.key_height);
@ -46,15 +46,11 @@ public class Keyboard2View extends View
_keyBgPadding = getResources().getDimension(R.dimen.key_bg_padding); _keyBgPadding = getResources().getDimension(R.dimen.key_bg_padding);
_keyRound = getResources().getDimension(R.dimen.key_round); _keyRound = getResources().getDimension(R.dimen.key_round);
_keyWidth = (dm.widthPixels - (_horizontalMargin * 2)) / KEY_PER_ROW; _keyWidth = (dm.widthPixels - (_horizontalMargin * 2)) / KEY_PER_ROW;
_keyBgPaint = new Paint();
_keyBgPaint.setColor(getResources().getColor(R.color.key_bg)); _keyBgPaint.setColor(getResources().getColor(R.color.key_bg));
_keyDownBgPaint = new Paint();
_keyDownBgPaint.setColor(getResources().getColor(R.color.key_down_bg)); _keyDownBgPaint.setColor(getResources().getColor(R.color.key_down_bg));
_keyLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
_keyLabelPaint.setColor(getResources().getColor(R.color.key_label)); _keyLabelPaint.setColor(getResources().getColor(R.color.key_label));
_keyLabelPaint.setTextSize(getResources().getDimension(R.dimen.label_text_size)); _keyLabelPaint.setTextSize(getResources().getDimension(R.dimen.label_text_size));
_keyLabelPaint.setTextAlign(Paint.Align.CENTER); _keyLabelPaint.setTextAlign(Paint.Align.CENTER);
_keySubLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
_keySubLabelPaint.setColor(getResources().getColor(R.color.key_sub_label)); _keySubLabelPaint.setColor(getResources().getColor(R.color.key_sub_label));
_keySubLabelPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size)); _keySubLabelPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size));
_keySubLabelPaint.setTextAlign(Paint.Align.CENTER); _keySubLabelPaint.setTextAlign(Paint.Align.CENTER);
@ -120,10 +116,8 @@ public class Keyboard2View extends View
{ {
KeyDown k = getKeyDown(pointerId); KeyDown k = getKeyDown(pointerId);
if (k != null) if (k != null && k.updateDown(moveX, moveY))
{ updateFlags();
k.updateDown(moveX, moveY);
}
} }
private void onTouchDown(float touchX, float touchY, int pointerId) private void onTouchDown(float touchX, float touchY, int pointerId)
@ -145,6 +139,7 @@ public class Keyboard2View extends View
if (touchX >= x && touchX < (x + keyW)) if (touchX >= x && touchX < (x + keyW))
{ {
_downKeys.add(new KeyDown(pointerId, key, touchX, touchY)); _downKeys.add(new KeyDown(pointerId, key, touchX, touchY));
updateFlags();
invalidate(); invalidate();
return ; return ;
} }
@ -159,14 +154,36 @@ public class Keyboard2View extends View
if (k != null) if (k != null)
{ {
if ((k.flags & KeyValue.FLAG_KEEP_ON) != 0)
{
k.flags ^= KeyValue.FLAG_KEEP_ON;
k.pointerId = -1;
return ;
}
for (int i = 0; i < _downKeys.size(); i++)
{
KeyDown downKey = _downKeys.get(i);
if (downKey.pointerId == -1)
_downKeys.remove(i--);
else if ((downKey.flags & KeyValue.FLAG_KEEP_ON) != 0)
downKey.flags ^= KeyValue.FLAG_KEEP_ON;
}
if (k.value != null) if (k.value != null)
_ime.handleKeyUp(k.value); _ime.handleKeyUp(k.value, _flags);
_downKeys.remove(k); _downKeys.remove(k);
updateFlags();
invalidate(); invalidate();
return ; return ;
} }
} }
private void updateFlags()
{
_flags = 0;
for (KeyDown k : _downKeys)
_flags |= k.flags;
}
@Override @Override
public void onMeasure(int wSpec, int hSpec) public void onMeasure(int wSpec, int hSpec)
{ {
@ -185,6 +202,7 @@ public class Keyboard2View extends View
{ {
float x; float x;
float y; float y;
boolean upperCase = ((_flags & KeyValue.FLAG_SHIFT) != 0);
y = _verticalMargin; y = _verticalMargin;
for (KeyboardData.Row row : _keyboard.getRows()) for (KeyboardData.Row row : _keyboard.getRows())
@ -200,22 +218,22 @@ public class Keyboard2View extends View
canvas.drawRoundRect(new RectF(x + _keyBgPadding, y + _keyBgPadding, canvas.drawRoundRect(new RectF(x + _keyBgPadding, y + _keyBgPadding,
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding), _keyRound, _keyRound, _keyBgPaint); x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding), _keyRound, _keyRound, _keyBgPaint);
if (k.key0 != null) if (k.key0 != null)
canvas.drawText(k.key0.getSymbol(), keyW / 2 + x, canvas.drawText(k.key0.getSymbol(upperCase), keyW / 2 + x,
(_keyHeight + _keyLabelPaint.getTextSize()) / 2 + y, _keyLabelPaint); (_keyHeight + _keyLabelPaint.getTextSize()) / 2 + y, _keyLabelPaint);
float textOffsetY = _keySubLabelPaint.getTextSize() / 2; float textOffsetY = _keySubLabelPaint.getTextSize() / 2;
float subPadding = _keyPadding + _keyBgPadding; float subPadding = _keyPadding + _keyBgPadding;
if (k.key1 != null) if (k.key1 != null)
canvas.drawText(k.key1.getSymbol(), x + subPadding, canvas.drawText(k.key1.getSymbol(upperCase), x + subPadding,
y + subPadding + textOffsetY, _keySubLabelPaint); y + subPadding + textOffsetY, _keySubLabelPaint);
if (k.key2 != null) if (k.key2 != null)
canvas.drawText(k.key2.getSymbol(), x + keyW - subPadding, canvas.drawText(k.key2.getSymbol(upperCase), x + keyW - subPadding,
y + subPadding + textOffsetY, _keySubLabelPaint); y + subPadding + textOffsetY, _keySubLabelPaint);
textOffsetY /= 2; // lol textOffsetY /= 2; // lol
if (k.key3 != null) if (k.key3 != null)
canvas.drawText(k.key3.getSymbol(), x + subPadding, canvas.drawText(k.key3.getSymbol(upperCase), x + subPadding,
y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint); y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint);
if (k.key4 != null) if (k.key4 != null)
canvas.drawText(k.key4.getSymbol(), x + keyW - subPadding, canvas.drawText(k.key4.getSymbol(upperCase), x + keyW - subPadding,
y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint); y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint);
x += keyW; x += keyW;
} }
@ -232,6 +250,7 @@ public class Keyboard2View extends View
public KeyboardData.Key key; public KeyboardData.Key key;
public float downX; public float downX;
public float downY; public float downY;
public int flags;
public KeyDown(int pointerId, KeyboardData.Key key, float x, float y) public KeyDown(int pointerId, KeyboardData.Key key, float x, float y)
{ {
@ -240,6 +259,7 @@ public class Keyboard2View extends View
this.key = key; this.key = key;
downX = x; downX = x;
downY = y; downY = y;
flags = value.getFlags();
} }
public boolean updateDown(float x, float y) public boolean updateDown(float x, float y)
@ -249,6 +269,7 @@ public class Keyboard2View extends View
if (newValue != null && newValue != value) if (newValue != null && newValue != value)
{ {
value = newValue; value = newValue;
flags = newValue.getFlags();
return (true); return (true);
} }
return (false); return (false);