Distinguish KeyEvent regular menu from opening IME

While listening to key events in ActionBarSherlock to show the overflow menu on devices prior to ICS, and I ran into an interesting problem. It would seem that I can not distinguish a simple keystroke compared to when a user long presses a menu key in order to display IME. Both instances of KeyEvent identical and look like this:

Is there an easy way to distinguish between these two separate events?

+6
source share
1 answer

Hmmmm ... onLongKeyPress() doesn't seem to work with KEYCODE_MENU . How annoying.

This is similar to working with Nexus S (4.0.3) and Nexus One (2.3.6):

 public class MenuKeyDetectorActivity extends Activity { boolean wasLongPress=false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { wasLongPress=wasLongPress | event.isLongPress(); } return(false); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { Log.w("MKD", String.format("wasLongPress: %b", wasLongPress)); wasLongPress=false; } return(false); } } 

Basically, note that this is a long press or not in your onKeyDown() calls, then use this information in onKeyUp() to determine the final location.

+3
source

Source: https://habr.com/ru/post/911965/


All Articles