Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Android modifier key fallback on an action?

I'm working on an input device that has a button that I'd like to act both as a home button when pressed by itself, but also work as a modifier for the other buttons on the device.

I know you can do this in the kcm file with other buttons, and have something like:

key ESCAPE {
    base:                               fallback BACK
    alt, meta:                          fallback HOME
    ctrl:                               fallback MENU
}

Is it possible to have something like:

key CTRL_LEFT {
    base:                               fallback HOME
    {SOMETHING HERE}:                   CTRL_LEFT
}

so that I can press that button with another button and have it not fallback on HOME?

Thanks in advance! :D

like image 284
Nick Badal Avatar asked Nov 13 '22 19:11

Nick Badal


1 Answers

This functionality is not possible on a system level: see http://source.android.com/tech/input/key-character-map-files.html for a list of all options possible.

However, if you would like to have it on an application level, you could program it in as follows:

import android.view.KeyEvent;

private boolean home = false;

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
   if (event.getScanCode() == 29)        //29 == CTRL_LEFT
     home = true;        //if we release this button, HOME needs to be invoked
   else home = false;    //so CTRL_LEFT was used as a modifier: no need to invoke HOME

   //allow the system to pass key handling to the next listener
   return super.onKeyUp(keyCode, event);
}

public boolean onKeyUp(int keyCode, KeyEvent event)
{
   if (event.getScanCode() == 29 && home == true)
   {
     super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME));
     return true;        //so we absorb the event
   }
   return super.onKeyUp(keyCode, event);
}

Then, as long as this application is set up to receive the keystrokes (usually a privacy issue unless you're coding it in for yourself), it can process the keystrokes and dispatch the HOME button when need be.

Should you decide to proceed thus, you'd have to remove the

base:                               fallback HOME

from your code.

like image 55
Alex Avatar answered Nov 15 '22 11:11

Alex