1

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"?

Buddy
  • 152
  • 4
  • 13

3 Answers3

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
  • 1
    even 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;
            }
        }
} ```
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