Android keyboard event for back key when soft keyboard is displayed?

I am trying to capture the back event when the soft keyboard is displayed.

I am using the SDK ver 8 and can successfully capture the back key event ONLY when the HIDDEN soft keyboard looks like this:

@Override public void onBackPressed() { // do something super.onBackPressed(); } 

The problem is that the system does not call this method while the soft keyboard is displayed. I tried to enter the KeyDown / Up () methods, as well as the method described above to find out what was going on in this scenario, to no avail. See below:

 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { //DEBUGGING Log.d(TAG + "KeyUP", String.valueOf(event)); } 

Logs are returned for volume and menu keys as expected, but the reverse key is only recorded when the keyboard and menu are hidden.

Do you have a β€œback” guru who can explain this phenomenon?

Any help is greatly appreciated.

+7
android android-softkeyboard
source share
3 answers

After further searching on this site and the Android API, I found that

 KeyEvent.KEYCODE_BACK 

caught and consumed by an IME that has a connection to an input method, and this input method is currently displayed (in other words, the soft keyboard is NOT hidden). This means that the event is expended before the system calls the Activity onKeyDown () or onKeyUp () classes.

To get around this, subclass your IME widget (TextView or its child classes, such as EditText), and add onKeyPreIme () .

The i2097i stack user has posted a good solution for implementing onKeyPreIme () in action here . Just return FALSE to your onKeyPreIme () Override if you want to keep the default behavior of Androids (i.e. Closing the keyboard).

+5
source share

Here's a way to capture a keystroke event: 1. Extend the view of editText to override onKeyPreIme

 package com.test.test; import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.widget.EditText; /** * Created by sumit.saurabh on 11/10/16. */ public class ChatEditText extends EditText { /* Must use this constructor in order for the layout files to instantiate the class properly */ public ChatEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } private KeyImeChange keyImeChangeListener; public void setKeyImeChangeListener(KeyImeChange listener) { keyImeChangeListener = listener; } public interface KeyImeChange { public void onKeyIme(int keyCode, KeyEvent event); } @Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyImeChangeListener != null) { keyImeChangeListener.onKeyIme(keyCode, event); } return false; } } 
  1. ChatEditText in xml

      <com.test.test.ChatEditText android:id = "@+id/messageEditText" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_gravity = "bottom" android:layout_marginLeft = "12dp" android:layout_marginRight = "30dp" android:background = "@null" android:hint = "Type your message" android:inputType = "textMultiLine" android:singleLine = "false" android:textColorHint = "#c4c0bd" android:textSize = "18sp"/> 
  2. Then attach the listener (setKeyImeChangeListener) to the edit text:

     private ChatEditText messageEditText; messageEditText = (ChatEditText) findViewById(R.id.messageEditText); messageEditText.setKeyImeChangeListener(new ChatEditText.KeyImeChange(){ @Override public void onKeyIme(int keyCode, KeyEvent event) { if (KeyEvent.KEYCODE_BACK == event.getKeyCode()) { // do something } }}); 
+7
source share

Usually the back button hides the keyboard (nateve behaivor), so there’s nothing to do about it

0
source share

All Articles