1

I want to implement my own method when the user longpresses the backspace key (KEYCODE_DEL) in softkeypad in android.

So far I have done the following, But it's not working.

public class CustomEditText extends EditText{

private Random r = new Random();
private CustomEditText e = null;

    public CustomEditText(Context context, AttributeSet attrs, int defStyle){
         super(context, attrs, defStyle);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context) {
        super(context);
    }

    public void setEditText()
    {
        e = (CustomEditText)findViewById(R.id.edit_phone_number);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return (InputConnection) new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
            true);
    }

    private class ZanyInputConnection extends InputConnectionWrapper {

        public ZanyInputConnection(InputConnection target, boolean mutable) {
            super(target, mutable);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                // This is never called . So i do not know when the user pressed 
                // and unpressed the backspace key.
                // return false;
            }
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean deleteSurroundingText(int beforeLength, int afterLength)
        {
            //This is getting called when user press and unpress the backspace 
            if (beforeLength >= 1 && afterLength == 0) {
            return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
                    && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
        }
            return super.deleteSurroundingText(beforeLength, afterLength);
        }
    }
}

Here when I press the backspace on softKeypad sendKeyEvent is not called but deleteSurroundingText gets called.

For longpress detection I wanted to get the KeyEvent.ACTION_DOWN event on backspace and KeyEvent.ACTION_UP event on backspace and if the time difference between these two events is more than 1/2 seconds I will assume it's a longpress. Since both KeyEvent.ACTION_DOWN and KeyEvent.ACTION_UP are coming in sendKeyEvent method . But sendKeyEvent is never called. So I do not know how do I do it.

Please help if you have any other approach to do it.

editPhoneNumberText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // You can identify which key pressed buy checking keyCode value
                // with KeyEvent.KEYCODE_
                if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                    // this is for backspace
                    Logger.log("Thhis is what i need");
                }
                return false;
            }
        });
Pardeep Kr
  • 479
  • 2
  • 6
  • 25
  • can you please add a comment if its unclear and whats unclear ,instead of downvoting. – Pardeep Kr Feb 15 '16 at 12:47
  • Its a simple question , we have a soft keypad in android . Pressing backspace on it deletes characters from EditTexts . If user press and hold the backspace it continuously deletes the characters until the EditText is empty. I want to detect the backspace press and hold (Or longpress) and override it . – Pardeep Kr Feb 15 '16 at 12:50
  • Whatever the code i have given above is what i have tried so far . Please anybody has any hint give me some clue . Because i am stuck at it for long time – Pardeep Kr Feb 15 '16 at 12:51

1 Answers1

3

I faced the similar issue. Just change the following function and the rest is fine :-

@Override
        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
            if(android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
                if (beforeLength >= 1 && afterLength == 0) {
                    return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
                            && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
                }
            }
            else if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
                if (beforeLength == 1 && afterLength == 0) {
                    return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
                            && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
                }
            }
            return super.deleteSurroundingText(beforeLength, afterLength);
        }

Hope it helps.

Frosty
  • 500
  • 4
  • 14
  • deleteSurroundingText is called when backspace is pressed and unpressed. But when user does press and hold on backspace its not called. So your method is also not helping – Pardeep Kr Feb 15 '16 at 13:36
  • Now after this you can add OnKeyListener to your editText and Check for Keycode == KeyEvent.KEYCODE_DEL and if the event.getAction() for keyDown is called more than once before the keyUp then you can say its a longPress. – Frosty Feb 15 '16 at 14:00
  • But i read that OnKeyListener is called for HW keys not software keys – Pardeep Kr Feb 15 '16 at 14:02
  • That's why we used CustomEditText and used sendKeyEvent. By detecting the backspace key we are sending the sendKeyEvent in deleteSurroundingText explicitly and thus the OnKeylistener will be called. – Frosty Feb 15 '16 at 14:10
  • i did add setOnKeyListener .But event.getAction() is coming only once for ACTION_DOWN in case of longpress also. – Pardeep Kr Feb 15 '16 at 14:29
  • Could you paste your customEditText and OnKeyListener as of now. Because i am doing the same thing and i am getting repititive ACTION_DOWN events on longpress. – Frosty Feb 15 '16 at 16:29
  • i have edited the question with my OnKeyListener . The CustomEditText same as i have already pasted above . The only difference is that i now have added sendKeyEvent in deleteSurroundingText as suggested by you – Pardeep Kr Feb 16 '16 at 07:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103572/discussion-between-frosty-and-pardeep-kr). – Frosty Feb 16 '16 at 07:18
  • One more issue i am facing , when i press my on backspace i get the call of deleteSurroundingText.When i unpress backspace then also i get the call for deleteSurroundingText. Inside this method i send both ACION_DOWN and ACTION_UP events . But onPress i get only ACTION_DOWN and on unpress also i get only ACTION_DOWN. ACTION_UP is not coming at all in OnkeyListner's onKey method. – Pardeep Kr Feb 16 '16 at 09:13
  • Is ACTION_UP coming for you – Pardeep Kr Feb 16 '16 at 10:10