Android Development: How To Use onKeyUp?

Viewed 47321

I'm new to Android development and I can't seem to find a good guide on how to use an onKeyUp listener.

In my app, I have a big EditText, when someone presses and releases a key in that EditText I want to call a function that will perform regular expressions in that EditText.

I don't know how I'd use the onKeyUp. Could someone please show me how?

7 Answers

If you use device with no touch but with hard keypad buttons (keyboard) use this code to control the events of moving left right up down and ok. use onkeyDown and not onKeyUp because the onKeyup will return the next button events:

        Button myButton = (Button) findViewById(R.id.my_btn);
        myButton .setKeyListener(new KeyListener() {
        @Override
        public int getInputType() {
            return 0;
        }

        @Override
        public boolean onKeyDown(View view, Editable editable, int keyCode, KeyEvent keyEvent) {
            Log.d("myTag", "onKeyDown code: " +keyCode);
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    // USER_MOVE_RIGHT();
                    return true;
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    //USER_MOVE_LEFT());
                    return true;
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    //USER_MOVE_DOWN());
                    return true;
                case KeyEvent.KEYCODE_DPAD_UP:
                    //USER_MOVE_UP();
                    return true;
                case KeyEvent.KEYCODE_DPAD_CENTER:
                    //USER_Press_OK()
                    return true;
                               }
            return false;
        }
    ‏       public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        ‏               case KeyEvent.KEYCODE_BACK:
        ‏                   actionLabel.setText("KEYCODE_BACK key pressed");
        ‏                   Toast.makeText(this, "Press again back for exit", Toast.LENGTH_SHORT).show();
        ‏                   counter++;
        ‏                   if (counter > 1) {
        ‏                       super.onBackPressed();
        ‏                   }
        ‏                   return true;
        ‏               case KeyEvent.KEYCODE_VOLUME_UP:
        ‏                   actionLabel.setText("VOLUME_UP key pressed");
        ‏                   return true;
        ‏               case KeyEvent.KEYCODE_VOLUME_DOWN:
        ‏                   actionLabel.setText("VOLUME_DOWN key pressed");
        ‏                   return true;
        ‏           }
        return super.onKeyDown(keyCode, event);
        ‏       }
    // catches the onKeyUp button event
    ‏       @Override
    ‏       public boolean onKeyUp(int keyCode, KeyEvent event) {
    ‏           switch (keyCode) {
    ‏               case KeyEvent.KEYCODE_BACK:
    ‏                   actionLabel.setText("KEYCODE_BACK key released");
    ‏                   return true;
    ‏               case KeyEvent.KEYCODE_VOLUME_UP:
    ‏                   actionLabel.setText("VOLUME_UP key released");
    ‏                   return true;
    ‏               case KeyEvent.KEYCODE_VOLUME_DOWN:
    ‏                   actionLabel.setText("VOLUME_DOWN key released");
    ‏                   return true;
    ‏           }
    ‏           return super.onKeyDown(keyCode, event);
    ‏       }
    // works for API level 5 and lower
    ‏       @Override
    ‏       public void onBackPressed() {
    ‏           actionLabel.setText("BACK key pressed");
    ‏           super.onBackPressed();
    ‏       }
    // catches the long press button event (longer than 2 seconds)
    ‏       @Override
    ‏       public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    ‏           Toast.makeText(this, "Pressed for a long time", Toast.LENGTH_SHORT).show();
    ‏           return true;
    ‏       }
    // catches the on touch event on screen and shows the specific pixels
    ‏       // touched
    ‏       @Override
    ‏       public boolean onTouchEvent(MotionEvent event) {
    ‏           float x = event.getX();
    ‏           float y = event.getY();
    ‏           actionLabel.setText("Touch press on x: " + x + " y: " + y);
    ‏           return true;
    ‏       }}
Related