0

I would like to do something while any hardware button is pressed (not after it is released).I know how to do it with a regular button, but how can I achieve it with a hardware button?

This is the behavior I want to simulate using a hardware button.

button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    button.setText("Button Pressed");
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    button.setText("Start"); //finger was lifted
                }
                return true;
            }
        });
Erick Pro
  • 33
  • 5

1 Answers1

0

You can override onKeyDown() and onKeyUp() method in your View or Activity.

The keyCodes are defined in KeyEvent class.

greenfeel
  • 36
  • 1