Last active
October 24, 2020 19:33
-
-
Save matthewmorrone/243fe3aab445565bdd0171a138ec71b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float x1, x2, y1, y2, dx, dy; | |
String direction; | |
boolean isDown = false; | |
Keyboard.Key activeKey; | |
public String getDirection(MotionEvent me) { | |
switch(me.getAction()) { | |
case MotionEvent.ACTION_DOWN: { | |
isDown = true; | |
x1 = me.getX(); | |
y1 = me.getY(); | |
} | |
case MotionEvent.ACTION_UP: { | |
isDown = false; | |
x2 = me.getX(); | |
y2 = me.getY(); | |
dx = x2-x1; | |
dy = y2-y1; | |
if (dx < 2 && dy < 2) { | |
direction = "none"; | |
} | |
else if (Math.abs(dx) > Math.abs(dy)) { | |
if (dx > 0) direction = "right"; | |
else direction = "left"; | |
} | |
else { | |
if (dy > 0) direction = "down"; | |
else direction = "up"; | |
} | |
} | |
} | |
return direction; | |
} | |
public Keyboard.Key getKeyFromCoordinates(int x, int y) { | |
List<Key> keys = getKeyboard().getKeys(); | |
for (Key key : keys) { | |
if (key.isInside(x, y)) { | |
return key; | |
} | |
} | |
return null; | |
} | |
@RequiresApi(api = Build.VERSION_CODES.Q) | |
@Override | |
public boolean onTouchEvent(MotionEvent me) { | |
int action = me.getAction(); | |
int x = (int)me.getX(); | |
int y = (int)me.getY(); | |
getDirection(me); | |
if (me.getAction() == MotionEvent.ACTION_DOWN) { | |
activeKey = getKeyFromCoordinates(x, y); | |
} | |
if (me.getAction() != MotionEvent.ACTION_MOVE) { | |
System.out.println(activeKey.label+" "+direction+" "+x+" "+y+" "+me.getAction()); | |
} | |
switch(direction) { | |
case("up"): { | |
if (activeKey.popupCharacters.length() > 0) { | |
getOnKeyboardActionListener().onKey(activeKey.popupCharacters.charAt(0), null); | |
} | |
} | |
case("down"): { | |
if (activeKey.popupCharacters.length() > 1) { | |
getOnKeyboardActionListener().onKey(activeKey.popupCharacters.charAt(1), null); | |
} | |
} | |
case("left"): { | |
if (activeKey.popupCharacters.length() > 2) { | |
getOnKeyboardActionListener().onKey(activeKey.popupCharacters.charAt(2), null); | |
} | |
} | |
case("right"): { | |
if (activeKey.popupCharacters.length() > 3) { | |
getOnKeyboardActionListener().onKey(activeKey.popupCharacters.charAt(3), null); | |
} | |
} | |
} | |
getOnKeyboardActionListener().onKey(activeKey.codes[0], null); | |
// ACTION_DOWN = 0 | |
// ACTION_UP = 1 | |
// ACTION_MOVE = 2 | |
// return isDown ? super.onTouchEvent(me) : false; | |
return super.onTouchEvent(me); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment