95

I am using this code:

public boolean onKey(View v, int keyCode, KeyEvent event) {
msg = (EditText)findViewById(R.id.msg);
String message = msg.getText().toString();
            if(keyCode == 66)
            {
                //It's hitting here twice.          
            }
            return false;
        };

Can anyone please tell me why it's hitting twice when I press enter?

1
  • 19
    Once down, once up? Try onKeyDown() or onKeyUp(), or check KeyEvent.getAction().
    – benvd
    Sep 27, 2010 at 10:22

3 Answers 3

233

OnKey is fired twice: the first time for key down, and the second time for key up, so you have to filter:

YOUR_VIEW.setOnKeyListener(new OnKeyListener()
        {                           
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                //This is the filter
                if (event.getAction()!=KeyEvent.ACTION_DOWN)
                    return true;

                switch (keyCode) {
                case KeyEvent.KEYCODE_1 : 
                    MakeToast(1);
                    break;
                case KeyEvent.KEYCODE_2 : 
                    MakeToast(2);
                    break;
                case KeyEvent.KEYCODE_3 : 
                    MakeToast(3);
                    break;

                }

                return true;
            }

        });
5
  • 2
    Does returning true or false have any meaning to the super classes?
    – OrhanC1
    Feb 10, 2012 at 11:54
  • 6
    You should return false on keys that are not processed?
    – yostane
    May 2, 2012 at 16:18
  • 2
    Apparently it should be return false for keys not processed. I put this code in and when the EditText has focus my app stop responding to back button...
    – luiges90
    Mar 4, 2014 at 12:40
  • Doing this will stop listening keyevent dpad_center as onClick
    – A_rmas
    Apr 7, 2016 at 10:59
  • Thanks for event.getAction()!=KeyEvent.ACTION_DOWN, but I return false in this case. If true, any other keys will be consumed. You even won't exit your activity/fragment with Back button!
    – CoolMind
    Oct 19, 2018 at 8:39
6
 itemView.setOnKeyListener(new View.OnKeyListener() {
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    int pos = getAdapterPosition();
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_MENU){
     //code here
    }
    return false;
  }
 });
-2
edittext.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.ACTION_UP) {
                switch (i) {
                    case KeyEvent.KEYCODE_ENTER:
                        if (!HelperFunction.isStringEmpty(edittext.getText().toString())) {

                            if (loadCount == 0) {

                                loadCount ++;


                            }
                        }
                    default:
                        return false;
                }
            }
            return false;
        }

this code block prevents calling the code twice. I use loadCount String value , if loadCount == 0 run my code block and i am doing loadCount ++ in my if block. So My Code block work on time.

2
  • Consider providing an explanation of what your code does and how it solves the problem.
    – Ankit
    Jul 3, 2018 at 17:23
  • This is just a hack and not proper way.
    – Amir Dora.
    Jun 4, 2021 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.