Repeat key on long press

This commit is contained in:
juloo 2015-08-03 15:11:11 +02:00
parent 7dd5845883
commit e9df9bc299
2 changed files with 51 additions and 55 deletions

View File

@ -63,7 +63,7 @@ public class Keyboard2 extends InputMethodService
getCurrentInputConnection().sendKeyEvent(event); getCurrentInputConnection().sendKeyEvent(event);
getCurrentInputConnection().sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP)); getCurrentInputConnection().sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
} }
else else if (key.getChar(false) != KeyValue.CHAR_NONE)
{ {
sendKeyChar(key.getChar((flags & KeyValue.FLAG_SHIFT) != 0)); sendKeyChar(key.getChar((flags & KeyValue.FLAG_SHIFT) != 0));
} }

View File

@ -15,9 +15,15 @@ public class Keyboard2View extends View
implements View.OnTouchListener implements View.OnTouchListener
{ {
private static final float KEY_PER_ROW = 10; private static final float KEY_PER_ROW = 10;
private static final float SUB_VALUE_DIST = 6f;
private static final long VIBRATE_LONG = 25; private static final long VIBRATE_LONG = 25;
private static final long VIBRATE_MIN_INTERVAL = 100; private static final long VIBRATE_MIN_INTERVAL = 100;
private static final long LONGPRESS_TIMEOUT = 800;
private static final long LONGPRESS_INTERVAL = 90;
private Keyboard2 _ime; private Keyboard2 _ime;
private KeyboardData _keyboard; private KeyboardData _keyboard;
@ -62,7 +68,6 @@ public class Keyboard2View extends View
_keyLabelLockedPaint.setTextAlign(Paint.Align.CENTER); _keyLabelLockedPaint.setTextAlign(Paint.Align.CENTER);
_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);
setOnTouchListener(this); setOnTouchListener(this);
} }
@ -123,13 +128,39 @@ public class Keyboard2View extends View
private void onTouchMove(float moveX, float moveY, int pointerId) private void onTouchMove(float moveX, float moveY, int pointerId)
{ {
KeyDown k = getKeyDown(pointerId); KeyDown key = getKeyDown(pointerId);
KeyValue newValue;
if (k != null && k.updateDown(moveX, moveY)) if (key != null)
{ {
moveX -= key.downX;
moveY -= key.downY;
if ((Math.abs(moveX) + Math.abs(moveY)) < SUB_VALUE_DIST)
newValue = key.key.key0;
else if (moveX < 0)
newValue = (moveY < 0) ? key.key.key1 : key.key.key3;
else if (moveY < 0)
newValue = key.key.key2;
else
newValue = key.key.key4;
if (newValue != null && newValue != key.value)
{
key.setValue(newValue);
updateFlags(); updateFlags();
vibrate(); vibrate();
} }
else if (key.value != null && (key.flags & KeyValue.FLAG_NOCHAR) == 0)
{
long now = System.currentTimeMillis();
if (now >= key.nextPress)
{
key.nextPress = now + LONGPRESS_INTERVAL;
_ime.handleKeyUp(key.value, _flags);
vibrate();
}
}
}
} }
private void onTouchDown(float touchX, float touchY, int pointerId) private void onTouchDown(float touchX, float touchY, int pointerId)
@ -268,33 +299,21 @@ public class Keyboard2View extends View
(_keyHeight + _keyLabelPaint.getTextSize()) / 2f + y, (_keyHeight + _keyLabelPaint.getTextSize()) / 2f + y,
(keyDown != null && (keyDown.flags & KeyValue.FLAG_LOCKED) != 0) (keyDown != null && (keyDown.flags & KeyValue.FLAG_LOCKED) != 0)
? _keyLabelLockedPaint : _keyLabelPaint); ? _keyLabelLockedPaint : _keyLabelPaint);
float textOffsetY = -_keySubLabelPaint.ascent();
float subPadding = _keyBgPadding + _keyPadding; float subPadding = _keyBgPadding + _keyPadding;
_keySubLabelPaint.setTextAlign(Paint.Align.LEFT);
if (k.key1 != null) if (k.key1 != null)
{
_keySubLabelPaint.setTextAlign(Paint.Align.LEFT);
canvas.drawText(k.key1.getSymbol(upperCase), x + subPadding, canvas.drawText(k.key1.getSymbol(upperCase), x + subPadding,
y + subPadding + textOffsetY, _keySubLabelPaint); y + subPadding - _keySubLabelPaint.ascent(), _keySubLabelPaint);
}
if (k.key2 != null)
{
_keySubLabelPaint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText(k.key2.getSymbol(upperCase), x + keyW - subPadding,
y + subPadding + textOffsetY, _keySubLabelPaint);
}
textOffsetY = _keySubLabelPaint.descent();
if (k.key3 != null) if (k.key3 != null)
{
_keySubLabelPaint.setTextAlign(Paint.Align.LEFT);
canvas.drawText(k.key3.getSymbol(upperCase), x + subPadding, canvas.drawText(k.key3.getSymbol(upperCase), x + subPadding,
y + _keyHeight - subPadding - textOffsetY, _keySubLabelPaint); y + _keyHeight - subPadding - _keySubLabelPaint.descent(), _keySubLabelPaint);
}
if (k.key4 != null)
{
_keySubLabelPaint.setTextAlign(Paint.Align.RIGHT); _keySubLabelPaint.setTextAlign(Paint.Align.RIGHT);
if (k.key2 != null)
canvas.drawText(k.key2.getSymbol(upperCase), x + keyW - subPadding,
y + subPadding - _keySubLabelPaint.ascent(), _keySubLabelPaint);
if (k.key4 != null)
canvas.drawText(k.key4.getSymbol(upperCase), x + keyW - subPadding, canvas.drawText(k.key4.getSymbol(upperCase), x + keyW - subPadding,
y + _keyHeight - subPadding - textOffsetY, _keySubLabelPaint); y + _keyHeight - subPadding - _keySubLabelPaint.descent(), _keySubLabelPaint);
}
x += keyW; x += keyW;
} }
y += _keyHeight; y += _keyHeight;
@ -303,51 +322,28 @@ public class Keyboard2View extends View
private class KeyDown private class KeyDown
{ {
private static final float SUB_VALUE_DIST = 6f;
public int pointerId; public int pointerId;
public KeyValue value; public KeyValue value;
public KeyboardData.Key key; public KeyboardData.Key key;
public float downX; public float downX;
public float downY; public float downY;
public int flags; public int flags;
public long nextPress;
public KeyDown(int pointerId, KeyboardData.Key key, float x, float y) public KeyDown(int pointerId, KeyboardData.Key key, float x, float y)
{ {
this.pointerId = pointerId; this.pointerId = pointerId;
value = key.key0;
this.key = key; this.key = key;
downX = x; downX = x;
downY = y; downY = y;
flags = (value == null) ? 0 : value.getFlags(); setValue(key.key0);
} }
public boolean updateDown(float x, float y) public void setValue(KeyValue v)
{ {
KeyValue newValue = getDownValue(x - downX, y - downY); value = v;
flags = (value == null) ? 0 : v.getFlags();
if (newValue != null && newValue != value) nextPress = System.currentTimeMillis() + LONGPRESS_TIMEOUT;
{
value = newValue;
flags = newValue.getFlags();
return (true);
}
return (false);
}
private KeyValue getDownValue(float x, float y)
{
if ((Math.abs(x) + Math.abs(y)) < SUB_VALUE_DIST)
return (key.key0);
if (x < 0)
{
if (y < 0)
return (key.key1);
return (key.key3);
}
else if (y < 0)
return (key.key2);
return (key.key4);
} }
} }
} }