mirror of
https://github.com/Julow/Unexpected-Keyboard.git
synced 2025-06-19 17:31:30 +02:00
Create KeyboardData class and handle move on keys
This commit is contained in:
parent
702ae9d0c1
commit
45ea215bb6
@ -40,7 +40,9 @@ enum KeyValue
|
|||||||
KEY_6("6", '6'),
|
KEY_6("6", '6'),
|
||||||
KEY_7("7", '7'),
|
KEY_7("7", '7'),
|
||||||
KEY_8("8", '8'),
|
KEY_8("8", '8'),
|
||||||
KEY_9("9", '9');
|
KEY_9("9", '9'),
|
||||||
|
KEY_ENTER("enter", '\n'),
|
||||||
|
KEY_SPACE("space", ' ');
|
||||||
|
|
||||||
private String _name;
|
private String _name;
|
||||||
private char _char;
|
private char _char;
|
||||||
|
@ -2,19 +2,28 @@ package juloo.keyboard2;
|
|||||||
|
|
||||||
import android.inputmethodservice.InputMethodService;
|
import android.inputmethodservice.InputMethodService;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.KeyEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
public class Keyboard2 extends InputMethodService
|
public class Keyboard2 extends InputMethodService
|
||||||
{
|
{
|
||||||
public static final String TAG = "Keyboard_2.0";
|
public static final String TAG = "Keyboard_2.0";
|
||||||
|
|
||||||
|
private KeyboardData _keyboardData;
|
||||||
private Keyboard2View _inputView;
|
private Keyboard2View _inputView;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate()
|
||||||
|
{
|
||||||
|
super.onCreate();
|
||||||
|
_keyboardData = new KeyboardData(getResources().getXml(R.xml.azerty));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateInputView()
|
public View onCreateInputView()
|
||||||
{
|
{
|
||||||
_inputView = (Keyboard2View)getLayoutInflater().inflate(R.layout.input, null);
|
_inputView = (Keyboard2View)getLayoutInflater().inflate(R.layout.input, null);
|
||||||
_inputView.loadKeyboard(R.xml.azerty);
|
_inputView.setKeyboard(this, _keyboardData);
|
||||||
return (_inputView);
|
return (_inputView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package juloo.keyboard2;
|
package juloo.keyboard2;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.XmlResourceParser;
|
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
@ -9,7 +8,6 @@ import android.util.DisplayMetrics;
|
|||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class Keyboard2View extends View
|
public class Keyboard2View extends View
|
||||||
implements View.OnTouchListener
|
implements View.OnTouchListener
|
||||||
@ -21,7 +19,8 @@ public class Keyboard2View extends View
|
|||||||
private static final float KEY_LABEL_DPI = 16;
|
private static final float KEY_LABEL_DPI = 16;
|
||||||
private static final float KEY_SUBLABEL_DPI = 12;
|
private static final float KEY_SUBLABEL_DPI = 12;
|
||||||
|
|
||||||
private ArrayList<Row> _rows;
|
private Keyboard2 _ime;
|
||||||
|
private KeyboardData _keyboard;
|
||||||
|
|
||||||
private float _keyWidth;
|
private float _keyWidth;
|
||||||
private float _keyHeight;
|
private float _keyHeight;
|
||||||
@ -36,7 +35,6 @@ public class Keyboard2View extends View
|
|||||||
public Keyboard2View(Context context, AttributeSet attrs)
|
public Keyboard2View(Context context, AttributeSet attrs)
|
||||||
{
|
{
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
_rows = null;
|
|
||||||
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
||||||
_keyMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, KEY_MARGIN_DPI, dm);
|
_keyMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, KEY_MARGIN_DPI, dm);
|
||||||
_keyHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, KEY_HEIGHT_DPI, dm);
|
_keyHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, KEY_HEIGHT_DPI, dm);
|
||||||
@ -57,36 +55,10 @@ public class Keyboard2View extends View
|
|||||||
setOnTouchListener(this);
|
setOnTouchListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadKeyboard(int res)
|
public void setKeyboard(Keyboard2 ime, KeyboardData keyboardData)
|
||||||
{
|
{
|
||||||
XmlResourceParser parser = getContext().getResources().getXml(res);
|
_ime = ime;
|
||||||
ArrayList<Row> rows = new ArrayList<Row>();
|
_keyboard = keyboardData;
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
int status;
|
|
||||||
|
|
||||||
while (parser.next() != XmlResourceParser.START_TAG)
|
|
||||||
continue ;
|
|
||||||
if (!parser.getName().equals("keyboard"))
|
|
||||||
throw new Exception("Unknow tag: " + parser.getName());
|
|
||||||
while ((status = parser.next()) != XmlResourceParser.END_DOCUMENT)
|
|
||||||
{
|
|
||||||
if (status == XmlResourceParser.START_TAG)
|
|
||||||
{
|
|
||||||
String tag = parser.getName();
|
|
||||||
if (tag.equals("row"))
|
|
||||||
rows.add(new Row(parser, _keyWidth, _keyMargin));
|
|
||||||
else
|
|
||||||
throw new Exception("Unknow keyboard tag: " + tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_rows = rows;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -95,6 +67,7 @@ public class Keyboard2View extends View
|
|||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
float keyW;
|
float keyW;
|
||||||
|
int p;
|
||||||
|
|
||||||
switch (event.getActionMasked())
|
switch (event.getActionMasked())
|
||||||
{
|
{
|
||||||
@ -104,41 +77,64 @@ public class Keyboard2View extends View
|
|||||||
break ;
|
break ;
|
||||||
case MotionEvent.ACTION_DOWN:
|
case MotionEvent.ACTION_DOWN:
|
||||||
case MotionEvent.ACTION_POINTER_DOWN:
|
case MotionEvent.ACTION_POINTER_DOWN:
|
||||||
int p = event.getActionIndex();
|
p = event.getActionIndex();
|
||||||
onTouchDown(event.getX(p), event.getY(p), event.getPointerId(p));
|
onTouchDown(event.getX(p), event.getY(p), event.getPointerId(p));
|
||||||
break ;
|
break ;
|
||||||
|
case MotionEvent.ACTION_MOVE:
|
||||||
|
for (p = 0; p < event.getPointerCount(); p++)
|
||||||
|
onTouchMove(event.getX(p), event.getY(p), event.getPointerId(p));
|
||||||
|
break ;
|
||||||
default:
|
default:
|
||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
||||||
return (true);
|
return (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onTouchDown(float touch_x, float touch_y, int pointerId)
|
private void onTouchMove(float moveX, float moveY, int pointerId)
|
||||||
|
{
|
||||||
|
for (KeyboardData.Row row : _keyboard.getRows())
|
||||||
|
{
|
||||||
|
for (KeyboardData.Key k : row)
|
||||||
|
{
|
||||||
|
if (k.downPointer == pointerId)
|
||||||
|
{
|
||||||
|
KeyValue v = k.getDownValue(moveX, moveY);
|
||||||
|
if (v != k.downValue)
|
||||||
|
{
|
||||||
|
k.downValue = v;
|
||||||
|
Keyboard2.log("Key down " + v.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onTouchDown(float touchX, float touchY, int pointerId)
|
||||||
{
|
{
|
||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
float keyW;
|
float keyW;
|
||||||
|
|
||||||
y = -_keyHeight;
|
y = -_keyHeight;
|
||||||
for (Row row : _rows)
|
for (KeyboardData.Row row : _keyboard.getRows())
|
||||||
{
|
{
|
||||||
y += _keyHeight + _keyMargin;
|
y += _keyHeight + _keyMargin;
|
||||||
if (touch_y < y || touch_y >= (y + _keyHeight))
|
if (touchY < y || touchY >= (y + _keyHeight))
|
||||||
continue ;
|
continue ;
|
||||||
x = (KEY_PER_ROW * (_keyMargin + _keyWidth) - _keyMargin - row.getWidth()) / 2 + _keyMargin;
|
x = (KEY_PER_ROW * (_keyMargin + _keyWidth) - _keyMargin - row.getWidth(_keyWidth, _keyMargin)) / 2 + _keyMargin;
|
||||||
for (Key k : row)
|
for (KeyboardData.Key k : row)
|
||||||
{
|
{
|
||||||
keyW = _keyWidth * k.width;
|
keyW = _keyWidth * k.width;
|
||||||
if (touch_x >= x && touch_x < (x + keyW))
|
if (touchX >= x && touchX < (x + keyW) && k.downPointer == -1)
|
||||||
{
|
{
|
||||||
if (k.down_pointer == -1)
|
if (k.key0 != null)
|
||||||
{
|
Keyboard2.log("Key down " + k.key0.getName());
|
||||||
if (k.key0 != null)
|
k.downPointer = pointerId;
|
||||||
Keyboard2.log("Key down " + k.key0.getName());
|
k.downValue = k.key0;
|
||||||
k.down_pointer = pointerId;
|
k.downX = touchX;
|
||||||
invalidate();
|
k.downY = touchY;
|
||||||
return ;
|
invalidate();
|
||||||
}
|
return ;
|
||||||
}
|
}
|
||||||
x += keyW + _keyMargin;
|
x += keyW + _keyMargin;
|
||||||
}
|
}
|
||||||
@ -147,15 +143,15 @@ public class Keyboard2View extends View
|
|||||||
|
|
||||||
private void onTouchUp(int pointerId)
|
private void onTouchUp(int pointerId)
|
||||||
{
|
{
|
||||||
for (Row row : _rows)
|
for (KeyboardData.Row row : _keyboard.getRows())
|
||||||
{
|
{
|
||||||
for (Key k : row)
|
for (KeyboardData.Key k : row)
|
||||||
{
|
{
|
||||||
if (k.down_pointer == pointerId)
|
if (k.downPointer == pointerId)
|
||||||
{
|
{
|
||||||
if (k.key0 != null)
|
if (k.downValue != null)
|
||||||
Keyboard2.log("Key up " + k.key0.getName());
|
Keyboard2.log("Key up " + k.downValue.getName());
|
||||||
k.down_pointer = -1;
|
k.downPointer = -1;
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,10 +163,10 @@ public class Keyboard2View extends View
|
|||||||
{
|
{
|
||||||
int height;
|
int height;
|
||||||
|
|
||||||
if (_rows == null)
|
if (_keyboard.getRows() == null)
|
||||||
height = 0;
|
height = 0;
|
||||||
else
|
else
|
||||||
height = (int)((_keyHeight + _keyMargin) * (float)_rows.size() + _keyMargin);
|
height = (int)((_keyHeight + _keyMargin) * ((float)_keyboard.getRows().size()) + _keyMargin);
|
||||||
setMeasuredDimension(MeasureSpec.getSize(wSpec), height);
|
setMeasuredDimension(MeasureSpec.getSize(wSpec), height);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,16 +176,14 @@ public class Keyboard2View extends View
|
|||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
|
|
||||||
if (_rows == null)
|
|
||||||
return ;
|
|
||||||
y = _keyMargin;
|
y = _keyMargin;
|
||||||
for (Row row : _rows)
|
for (KeyboardData.Row row : _keyboard.getRows())
|
||||||
{
|
{
|
||||||
x = (KEY_PER_ROW * (_keyMargin + _keyWidth) - _keyMargin - row.getWidth()) / 2 + _keyMargin;
|
x = (KEY_PER_ROW * (_keyMargin + _keyWidth) - _keyMargin - row.getWidth(_keyWidth, _keyMargin)) / 2 + _keyMargin;
|
||||||
for (Key k : row)
|
for (KeyboardData.Key k : row)
|
||||||
{
|
{
|
||||||
float keyW = _keyWidth * k.width;
|
float keyW = _keyWidth * k.width;
|
||||||
if (k.down_pointer != -1)
|
if (k.downPointer != -1)
|
||||||
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyDownBgPaint);
|
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyDownBgPaint);
|
||||||
else
|
else
|
||||||
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyBgPaint);
|
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyBgPaint);
|
||||||
@ -213,72 +207,4 @@ public class Keyboard2View extends View
|
|||||||
y += _keyHeight + _keyMargin;
|
y += _keyHeight + _keyMargin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Row extends ArrayList<Key>
|
|
||||||
{
|
|
||||||
private float _width;
|
|
||||||
|
|
||||||
public Row(XmlResourceParser parser, float keyWidth, float keyMargin) throws Exception
|
|
||||||
{
|
|
||||||
super();
|
|
||||||
|
|
||||||
int status;
|
|
||||||
_width = 0;
|
|
||||||
while ((status = parser.next()) != XmlResourceParser.END_TAG)
|
|
||||||
{
|
|
||||||
if (status == XmlResourceParser.START_TAG)
|
|
||||||
{
|
|
||||||
String tag = parser.getName();
|
|
||||||
if (tag.equals("key"))
|
|
||||||
{
|
|
||||||
Key k = new Key(parser);
|
|
||||||
if (_width != 0f)
|
|
||||||
_width += keyMargin;
|
|
||||||
_width += keyWidth * k.width;
|
|
||||||
add(k);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw new Exception("Unknow row tag: " + tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getWidth()
|
|
||||||
{
|
|
||||||
return (_width);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class Key
|
|
||||||
{
|
|
||||||
public KeyValue key0;
|
|
||||||
public KeyValue key1;
|
|
||||||
public KeyValue key2;
|
|
||||||
public KeyValue key3;
|
|
||||||
public KeyValue key4;
|
|
||||||
|
|
||||||
public float width;
|
|
||||||
|
|
||||||
public int down_pointer;
|
|
||||||
|
|
||||||
public Key(XmlResourceParser parser) throws Exception
|
|
||||||
{
|
|
||||||
down_pointer = -1;
|
|
||||||
key0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
|
|
||||||
key1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
|
|
||||||
key2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
|
|
||||||
key3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
|
|
||||||
key4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
|
|
||||||
try
|
|
||||||
{
|
|
||||||
width = parser.getAttributeFloatValue(null, "width", 1f);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
width = 1f;
|
|
||||||
}
|
|
||||||
while (parser.next() != XmlResourceParser.END_TAG)
|
|
||||||
continue ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
139
srcs/juloo.keyboard2/KeyboardData.java
Normal file
139
srcs/juloo.keyboard2/KeyboardData.java
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
package juloo.keyboard2;
|
||||||
|
|
||||||
|
import android.content.res.XmlResourceParser;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
class KeyboardData
|
||||||
|
{
|
||||||
|
private ArrayList<Row> _rows;
|
||||||
|
|
||||||
|
public KeyboardData(XmlResourceParser parser)
|
||||||
|
{
|
||||||
|
ArrayList<Row> rows = new ArrayList<Row>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
|
||||||
|
while (parser.next() != XmlResourceParser.START_TAG)
|
||||||
|
continue ;
|
||||||
|
if (!parser.getName().equals("keyboard"))
|
||||||
|
throw new Exception("Unknow tag: " + parser.getName());
|
||||||
|
while ((status = parser.next()) != XmlResourceParser.END_DOCUMENT)
|
||||||
|
{
|
||||||
|
if (status == XmlResourceParser.START_TAG)
|
||||||
|
{
|
||||||
|
String tag = parser.getName();
|
||||||
|
if (tag.equals("row"))
|
||||||
|
rows.add(new Row(parser));
|
||||||
|
else
|
||||||
|
throw new Exception("Unknow keyboard tag: " + tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_rows = rows;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Row> getRows()
|
||||||
|
{
|
||||||
|
return (_rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Row extends ArrayList<Key>
|
||||||
|
{
|
||||||
|
private float _keysWidth;
|
||||||
|
|
||||||
|
public Row(XmlResourceParser parser) throws Exception
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
|
||||||
|
int status;
|
||||||
|
_keysWidth = 0;
|
||||||
|
while ((status = parser.next()) != XmlResourceParser.END_TAG)
|
||||||
|
{
|
||||||
|
if (status == XmlResourceParser.START_TAG)
|
||||||
|
{
|
||||||
|
String tag = parser.getName();
|
||||||
|
if (tag.equals("key"))
|
||||||
|
{
|
||||||
|
Key k = new Key(parser);
|
||||||
|
_keysWidth += k.width;
|
||||||
|
add(k);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw new Exception("Unknow row tag: " + tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getWidth(float keyWidth, float keyMargin)
|
||||||
|
{
|
||||||
|
return (keyWidth * _keysWidth + ((size() - 1) * keyMargin));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Key
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
** 1 2
|
||||||
|
** 0
|
||||||
|
** 3 4
|
||||||
|
*/
|
||||||
|
public KeyValue key0;
|
||||||
|
public KeyValue key1;
|
||||||
|
public KeyValue key2;
|
||||||
|
public KeyValue key3;
|
||||||
|
public KeyValue key4;
|
||||||
|
|
||||||
|
public float width;
|
||||||
|
|
||||||
|
public Key(XmlResourceParser parser) throws Exception
|
||||||
|
{
|
||||||
|
downPointer = -1;
|
||||||
|
key0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
|
||||||
|
key1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
|
||||||
|
key2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
|
||||||
|
key3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
|
||||||
|
key4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
width = parser.getAttributeFloatValue(null, "width", 1f);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
width = 1f;
|
||||||
|
}
|
||||||
|
while (parser.next() != XmlResourceParser.END_TAG)
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO move it in Keyboard2View
|
||||||
|
private static final float SUB_VALUE_DIST = 5f;
|
||||||
|
|
||||||
|
public int downPointer;
|
||||||
|
public KeyValue downValue;
|
||||||
|
public float downX;
|
||||||
|
public float downY;
|
||||||
|
|
||||||
|
public KeyValue getDownValue(float x, float y)
|
||||||
|
{
|
||||||
|
x -= downX;
|
||||||
|
y -= downY;
|
||||||
|
if ((Math.abs(x) + Math.abs(y)) < SUB_VALUE_DIST)
|
||||||
|
return (key0);
|
||||||
|
if (x < 0)
|
||||||
|
{
|
||||||
|
if (y < 0)
|
||||||
|
return ((key1 == null) ? key0 : key1);
|
||||||
|
return ((key3 == null) ? key0 : key3);
|
||||||
|
}
|
||||||
|
else if (y < 0)
|
||||||
|
return ((key2 == null) ? key0 : key2);
|
||||||
|
return ((key4 == null) ? key0 : key4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user