Catching the Back key on Android with an Inputmultiplexer - android

ive got another Question for you.
So im trying to get the user input working on an options menu. For this i got:
1. The Stage and
2. An extra Inputadapter
I need the extra Inputadapter to catch the BACK key on Android. So i have used an Inputmultiflexer, which allows me to use both inputprocessors.
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiplexer);
Gdx.input.setCatchBackKey(true);
And my method to check for the BACK button looks like this:
#Override
public boolean keyUp(int keycode){
if(keycode == Input.Keys.ESCAPE || keycode == Input.Keys.BACK){
new MenuScreen(game);
return true;
}
return false;
}
The problem: Its not working at all. It does not go back, when using the Back key on Android or the Escape key on Desktop. The only thing the Console is printing out when pressing the Button is :
Load KCM of non-default device may incur unexpected result
To be honest, i have no idea what its means and Google didnt help me either with that.
So how do i get this to work?

First when debugging, you should use something like :
System.out.println("back was pressed");
If you see that output in the console then you know input is working. If that works, the only problem I see with your code is that you may have created a new screen class, you never made it switch screens. I noticed you passed in the game object, so you should probably have something like this:
game.setScreen(new MenuScreen(game));
Hope this helps.

Related

Cardboard Android

Is it possible to focus and open up the information just like we do on click using magnet in cardboard android?
Like ray gaze in unity, is there a alternative for android?
I want to do something like the one shown in chromeexperiments
Finally solved!
In treasurehunt sample, you can find isLookingAtObject() method which detects where user is looking...
And another method called onNewFrame which performs some action in each frame...
My solution for our problem is:
in onNewFrame method I've added this snippet code:
if (isLookingAtObject()) {
selecting++; // selecting is an integer defined as a field with zero value!
} else {
selecting = 0;
}
if (selecting == 100) {
startYourFunction(); // edit it on your own
selecting = 0;
}
}
So when user gazes 100 frame at object, your function calls and if user's gaze finishes before selecting reaches 100, selecting will reset to zero.
Hope that this also works for you
Hope this helps. (Did small research, (fingers crossed) whether the link shared below directly answers your question)
You could check GazeInputModule.cs from GoogleSamples for
Cardboard-Unity from Github. As the documentation of that class says:
This script provides an implemention of Unity's BaseInputModule
class, so that Canvas-based UI elements (_uGUI_) can be selected by
looking at them and pulling the trigger or touching the screen.
This uses the player's gaze and the magnet trigger as a raycast
generator.
Please check some tutorial regarding Google Cardboard Unity here
Please check Google-Samples posted in Github here

cocos2dx onKeyReleased gets called at keydown instead of keyup in android

I am recently working with cocos2dx 3.0rc2 and I want to respond the back key event in android platform. I learned that I could override the onKeyReleased method of the layer and setKeypadEnabled(true) to capture the back key.
However I got a problem, I can capture the event, but not accurately. That is, I expect this method gets called when I released my finger from the back key. However it now gets triggered as soon as I put my finger on the back key. That being said, it responds at key-down phase while I wish it could do it at key-up phase.
Can you help me with this? By the way, I tested the code and it seemed that on win32, the backspace key was not responded (But it does not matter for me as I only care about android)
Here are the code blocks:
init:
...
this->setKeypadEnabled(true);
...
onKeyReleased:
...
if(keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE) {
onBack(nullptr);
}
...
I also tried the other way to capture the event, by setting the listener instead of simply putting setKeypadEnabled(true). The result is the same.
I appreciate your help!
In our apps, we did like this:
auto obKeyBackListener = EventListenerKeyboard::create();
obKeyBackListener->onKeyReleased = [=](EventKeyboard::KeyCode keyCode, Event* event)
{
if (keyCode == EventKeyboard::KeyCode::KEY_BACK) // KEY_BACK
{
event->stopPropagation(); // stop propagation for this event
onBack(nullptr);
}
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(obKeyBackListener, this);
You have written the correct code but have used the wrong key code.
It shoud be EventKeyboard::KeyCode::KEY_ESCAPE.
Change this:
this->setKeypadEnabled(true);
with this:
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this);

How to utilize remote control buttons with set top box?

I'm developing a simple app on PhoneGap for an Android set top box.
I have an image that is usable as a link. When I connect a mouse to the set top box and click the image, the link works. But when I use the remote control and select the image (I see the border around the image so I know it is selected) and I click OK button, the link does not work.
How can I use the remote buttons in the code?
This is very tricky because Google didn't feel like mapping the keys on a remote to an actual key output.
To use the setTopBox, you're going to have to figure out what key codes your Android Set Top Box is using and modify the Activity's onKeyUp event to handle it. We currently have an example of a work-around in this bug however we don't have an agreed API for exposing these buttons to Javascript yet, which is why this bug is still open.
But in short, you'd do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
{
sendJavascript("javascript:myJsMethod('UP');");
return true;
}
return super.onKeyDown(keyCode, event);
}

Android disable button with case differentiation

So I really have no clue why this isn't wokring - basic stuff I did hundred times before:
I want to disable the button 'loadGame' by doing the following if the boolean is false:
if(saveExists == false){
loadGame.setEnabled(false);
}else{
loadGame.setEnabled(true);
}
saveExists is a boolean taken from a sharedPreferences object which is by default false (works, because I already checked saveExists in an output line).
Basically no matter what cases I create with if/elseif/else the button doesn't seem to get disabled at any point - did I really miss something big?
Okay so here's some code concerning the SharedPreference:
SharedPreferences settings;
SharedPreferences.Editor preferenceEditor;
...
In the onCreate of the class where I want to disable the button:
Resource.settings = getSharedPreferences(Resource.PREFERENCE_AUDIO, MODE_PRIVATE);
Resource.preferenceEditor = Resource.settings.edit();
saveExists = Resource.settings.getBoolean("settings", false);
Log.d(TAG, "saveExists="+saveExists);
and finally, the part to disable the Button:
loadGame = (ImageButton) findViewById(R.id.loadButton);
loadGame.setEnabled(saveExists);
loadGame.setOnClickListener(new OnClickListener() {...}
Oh btw we're talking about a ImageButton here
SOLUTION: You won't see this coming
God I hate to break it to you but that was clearly my fault. My projectpartner toggled the buttons invisible/visible as part of the AsyncTask and it's executed after I'm doing what I tried here all the time - yup shame on me I guess :x
It's very hard to determine the problem without more information. If the is/elseif/else doesn't seem to be working, you can try modifying your code to this to see what's going on:
if(saveExists == false){
Log.v("TEST", "saveExists is false");
}else{
Log.v("TEST", "saveExists is true");
}
If that is working then the problem seems to be with disabling the button. Does execution this line without the if/else work?
loadGame.setEnabled(false);
EDIT
From your update, I see you're using an ImageButton. For disabling an ImageButton, use setClickable(saveExists)
why don't use saveExist itself into the argument no need to used if/else
loadGame.setEnabled(saveExists);
if it is true then it is enabled otherwise not

Is there any way to capture Android's back button in mobile webkit browsers?

I know that in PhoneGap there's a way to do this, but can it be done for an HTML5 web app? I'd like to have Android users be able to use the back button within the webapp to provide a consistent UX, but of course the default is to go back in the browser history and leave the app...
Edit: tried, didn't do anything on any button press on a Google Nexus S:
document.onkeydown = checkKeycode;
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
alert("keycode: " + keycode);
}
Edit again: The ultimate answer seems to be to create history points at each UX interaction -- using URL hashes like #!/main/about_us in the URL. This then allows for back-button use, so long as you make sure that the UI triggers a history.back() when a UI back button is tapped.
The ultimate answer seems to be to create history points at each UX interaction -- using URL hashes like #!/main/about_us in the URL. This then allows for back-button use, so long as you make sure that the UI triggers a history.back() when a UI back button is tapped.
Override the OnKeyDown Event in your app and look for KEYCODE_BACK.
If you are handling the event then return true else false.

Categories

Resources