How to detect swipe action on a key in android soft keyboard? For a example lets say a user "swipe up" on letter "j" in the keyboard. How to detect it as a "swipe up" event which triggered on letter "j"?
Asked
Active
Viewed 968 times
1
-
Whats your aim behind doing this ?? – Chintan Soni Jul 07 '13 at 13:38
-
I am developing av app to extend the messaging functionalities based on user events on keyboard and for that as one requirement I need to detect above event – Buddy Jul 07 '13 at 13:56
-
https://stackoverflow.com/a/39200117/1293492 – Tamás Bolvári Jul 09 '19 at 10:42
3 Answers
1
How to detect swipe action on a key in android soft keyboard?
Write your own soft keyboard.
Input method editors (a.k.a., soft keyboards) are implemented by other apps. Not every soft keyboard will even have keys. Hence there is no API whereby soft keyboards could report swipe events on keys, let alone rules forcing soft keyboard implementers to report on swipe events.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
1even though I am writing own keyboard, how to address above problem? How to identify such events? – Buddy Jul 07 '13 at 14:07
-
@Buddy: I have no implemented an `InputMethodService` and kin. I would presume that you can respond to touch events there the same way you would in a regular activity. – CommonsWare Jul 07 '13 at 14:10
1
If anyone still struggling to achieve this here is my code:
public class KeyboardInput extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
private KeyboardView keyboardView;
private Keyboard keyboard;
private GestureDetector gestureDetector;
Boolean swipeUp = false;
Boolean swipeDown = false;
@SuppressLint({"InflateParams", "ClickableViewAccessibility"})
@Override
public View onCreateInputView() {
keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard,null);
View inputView = getLayoutInflater().inflate(R.layout.keyboard, null);
keyboardView = inputView.findViewById(R.id.keyboard_view); // Make sure to replace 'keyboardView' with the actual ID of your KeyboardView in the XML layout
keyboard = new Keyboard(this, R.xml.qwerty);
keyboardView.setKeyboard(keyboard);
keyboardView.setOnKeyboardActionListener(this);
gestureDetector = new GestureDetector(this, new GestureListener());
keyboardView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return false; // Return false to allow the touch event to propagate
}
});
return inputView;
}
public void onPress(int primaryCode) {
//here check which key is touched first or where swipe is started
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceY) > 100 && Math.abs(velocityY) > 100) {
if (distanceY < 0) {
swipeUp = true;
}
else if (distanceY > 0){
swipeDown = true;
}
}
return true;
}
}
} ```

Cristiano
- 11
- 2
0
Are you writing own keyboard? If not, your app will not be informed on how user behaved on IME area.

Marcin Orlowski
- 72,056
- 11
- 123
- 141