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
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.
Related
Is it possible for an Android application to intercept external keyboard shortcuts (e.g. Alt+Tab) and action them before the OS performs the default behaviour? Using Alt+Tab as an example, I would like to be able to have my app respond to Alt+Tab within my application and not have Android switch applications to a different app.
I have tried searching, but have not been able to find an answer, I think my Google-Fu is failing me!
It maybe is not as extense or detailed as it would be needed for capturing a shortcut, but you may want to override the onKeyDown event in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "Back button pressed");
}
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Log.d(this.getClass().getName(), "Home button pressed");
}
if ((keyCode == KeyEvent.KEYCODE_MENU)) {
Log.d(this.getClass().getName(), "Menu button pressed");
}
//return true;
return super.onKeyDown(keyCode, event);
}
I'm developing a simple app for Android using ActionScript 3.0 in Flash CS5 and I'd like to know if there's a way to map the physical back button of the Android's phone to tell my animation to go to the first frame.
I've red that post: Disabling the phone's back button (AIR for Android /ActionScript 3)
so maybe it is possible ? If yes HOW.
Thank you !
//first register key down listener
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);
//then listen to the back key
private function handleKeys(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK ) {
logDebug("=>handleKeys.");
NativeApplication.nativeApplication.exit();
}
}
Have you tried overriding onKeyUp method?
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do your stuff
}
return false;
}
I have an app which requires the user to be logged in. In the app, to be logged in is to contain a specific string in the SharedPreferences file.
It is totally working except for one use case: when the user presses the phone's back button.
Is there something different that happens in loading the page when the user presses the phone's back button that I should be aware of and account for in the code?
Thanks!!
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
//do your stuff here
return true;
}
else
return super.onKeyUp(keyCode, event);
}
try the above code to handle back key event
I am sincerely trying to display alert box onClick of Home key, but I am not succeeded. So please help me to write the code otherwise, show me the code.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
return false;
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
it is not possible to intercept KEYCODE_HOME by external applications:
http://code.google.com/p/android/issues/detail?id=1979
I don't think you can intercept HOME key presses. It is reserved to ensure that you don't get locked within any application.
KEYCODE_HOME : This key is handled by the frame work and is never delivered to applications.
So you can't use it
I'm a relative beginner with Android. Does anybody have a sane explanation for how to listen for keys and soft keys in an EditText/TextView?
I'd love to see a comprehensive tutorial or set of examples.
As I understand it, I can add a KeyListener to my Activity, e.g. onKeyDown(), onKeyUp() but when I try this I can't trigger the events for normal keys only HOME and BACK for example.
I have seen mention of using a TextWatcher but that isn't the same as handling raw key events.
There seem to be a number of half-solutions here on SO. Hoping you can help clear the mists of confusion...
You have to assign a key listener not to activity but rather to EditText itself.
This is what I have to listen to BACK or MENU key events. Simply add this method, without implementing any Interface. I do this in my BaseActivity, from which every Activity inherits.
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(NAME, "Key pressed");
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Log.d(NAME, "Back pressed");
// IGNORE back key!!
return true;
/* Muestra el MenĂº de Opciones */
case KeyEvent.KEYCODE_MENU:
Intent menu = new Intent(this, Menu.class);
// start activity
startActivity(menu);
return true;
}
return super.onKeyDown(keyCode, event);
}
PS: I highly discourage ignoring the back key.
For example:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
//your code here
}
return false;
}
});
I recently found another way that be stuck using Activity onKeyDown, or event setting a key listener on view (which is not really working with key events from ADB in my case) with view.setOnKeyListener.
Since android P method addOnUnhandledKeyEventListener has been introduced. It allows you to do whatever you need to do when your view is able to catch unhandled key events.
Here is an example of how I used it :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
when (event.keyCode) {
KeyEvent.KEYCODE_UNKNOWN -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
KeyEvent.KEYCODE_SOFT_RIGHT -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
// etc...
else -> false // Specify you didn't handle the event
}
}