This is the code i wrote, and almost works, but ill be honest, ive put a lot of random things on there bcuz i was tired hahah, if you touch the screen at the same spot, or alternating distances it detects the keys0 and 1, and you can hold one or another and then release it and works, but it has a strange bug that i dont know how to explain, but i think its bcuz of the conditions
public KeypadData HandleMotionEvent(MotionEvent event) {
int action = event.getActionMasked();
int pointerIndex = event.getActionIndex();
float x = event.getX(pointerIndex);
switch(action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
if (lastKey == -1) {
lastX = x;
} else {
if (Math.abs(x - lastX) > 400) {
lastX = x;
if (lastKey == 61) {
lastKey = 60;
} else {
lastKey = 61;
}
}
}
if (lastKey == 61) {
key0Pressed = true;
key1Pressed = false;
} else {
key0Pressed = false;
key1Pressed = true;
}
return new KeypadData(true, true, lastKey);
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
if (key0Pressed) {
lastKey = 61;
key0Pressed = false;
key1Pressed = true;
} else if (key1Pressed) {
lastKey = 60;
key0Pressed = true;
key1Pressed = false;
}
return new KeypadData(true, false, lastKey);
default:
break;
}
return new KeypadData(false, false, 0);
}
I want it to behave like this:
When the distance condition is true, it will alternate the pressed key. For example, you touch the display, and then you touch it in the same place, then it will output the same key. But if you touch it with a distance > 200, then it will output the other key, and it will only change it back into the first key when the condition is met again (That’s why it’s a dynamic keypad; it doesn’t behave like a typical width()/2
with one output on each side).
You can press the display and it will output a key.
You can maintain pressed for as long as you want, and when you release it, it will change to a state of not pressed.
You can press the keys at the same time if the condition of distance is true (For example: Two fingers touching the screen with a separation of 4cm between).
Basically, I want it to behave like a normal 2-button keypad, but with the twist of being dynamic with the distance thing.
For example, this code works like a charm but is a static keypad:
public KeypadData HandleMotionEvent(MotionEvent event) {
int code = 60;
if (event.getX(event.getActionIndex()) > (view.getWidth() / 2)) {
code = 61;
}
int a = event.getActionMasked();
if (a == MotionEvent.ACTION_DOWN || a == MotionEvent.ACTION_POINTER_DOWN) {
return new KeypadData(true, true, code);
} else if (a == MotionEvent.ACTION_UP || a == MotionEvent.ACTION_POINTER_UP) {
return new KeypadData(true, false, code);
} else {
return new KeypadData(false, false, 0);
}
}
Aiag is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.