1

I'm using what should be pretty simple code, but it just doesn't want to work. Does the OS block intercepting this?

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {


    if (keyCode == KeyEvent.KEYCODE_MENU) 
    {
    longOptionPress = true;
    openOptionsMenu();
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}
Maximus
  • 8,351
  • 3
  • 29
  • 37

2 Answers2

10

You must call startTracking() on the event from the normal onKeyPress() method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

See also a question I asked and answered some time ago.

Community
  • 1
  • 1
Thomas
  • 174,939
  • 50
  • 355
  • 478
1

If it is ListView, You need to use onCreateContextMenu.

 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
Aliostad
  • 80,612
  • 21
  • 160
  • 208