If there is some way how to manage back button in Libgdx?
for example in Andengine I have implemented this like that:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
start(MENU);
break;
...
...
}
}
}
I don't have idea how to do it here, because ApplicationListener has only create, show, render...
I tryed this:
if (Gdx.input.isButtonPressed(Keys.BACK)){
new ScreenChangeTask(MyScreen.SPLASH);
}
but it still closes my application.
FYI: I have class Controller extends Game and I use public void setScreen (Screen screen) to switch between screens.
In order to do this properly you need to tell LibGDX to catch the back key:
Gdx.input.setCatchBackKey(true);
You should do this somewhere early in the application. And set it to false when you want the user to be able to use the back key.
set
Gdx.input.setCatchBackKey(true);
then implement below code on keyUp...
#Override
public boolean keyUp(int keycode) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
game.setScreen(new MenuScreen(game)); //MenuScreen is your class Screen
break;
return false;
}
}
}
Hope this will help you
Related
I have an Android webview that I want it to go back with the Android back button. I have it working if the previous site is, for example https://www.aaa.com/index.php and https://www.aaa.com/index2.php, but canGoBack() returns false when the URLs are for example, https://www.aaa.com/index.php?page=page1 and https://www.aaa.com/index.php?page=page2. I think this is probably because the URLs are the same, it's just the variables at the end that change. This is my code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (view.canGoBack()) {
view.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
How can I make it to go back even if the variable on the URL changes?
You are probably being hit by the bug in Chrome version 63. See: https://bugs.chromium.org/p/chromium/issues/detail?id=794020
I have source code for SmartMouse android app.
I want to alter the function of volume keys with the onscreen buttons.
I have basic knowledge for C programming but don't know java.
What part should I search for in the code?
This might be a lame question but I badly need this.
You have to capture the event as mentionned here : Android - Volume Buttons used in my application
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
dispatchKeyEvent is not only called for volume keys, it will catch all the key event so you have to :
Get the event code
Check if it's what you are seeking for
Do what you want according to the event :)
The key is dispatchKeyEvent is called before any other method by the system, so you can intercept the event
I was wondering if I am able to disable the default volume up and down key press on the side of a cell phone.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.v("key pressed", String.valueOf(event.getKeyCode()));
return false;
//return super.dispatchKeyEvent(event);
}
I am using dispatchKeyEvent to find when the user presses the volume up or down button but I am not able to stop the phone from actually turning up the volume or turning it down. I want to enable the user to fire methods in my application if they decide to use the volume up or down button. Am I able to stop the phone from performing the volume control when my application is focused?
I used this in a previous project...perhaps it is what you're looking for:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
Android - Volume Buttons used in my application
I need to lock the home button in an app, because it will be used by older people and they will not know how to get back if they accidently touch on the home button. I already have that code below but that is not working on android 4.
What I really want is when somebody touches the home button, it does not do anything. Do you have any idea that can help me?
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME)
&& event.getRepeatCount() == 0)
{
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
// Do nothing
return;
}
The only way to do this is to make your app the launcher app on that device, which may not be desirable to the vast majority of Android users, regardless of their age.
I'm using a rooted tabled with an USB port in host mode to read the value of an xbox-controller-joystick (with onJoystickMotion).
Now, I also want to track the buttons keycode_button_a, keycode_button_b, keycode_button_x, keycode_button_y and keycode_button_select. The tracking is working, but pressing these buttons will hide the activity.
Is there any way to disable android hiding an activity on this keycode_button_... events?
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
InputDeviceState state = getInputDeviceState(event);
if (state != null) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
if (state.onKeyDown(event)) {
//Do my thing
}
break;
case KeyEvent.ACTION_UP:
if (state.onKeyUp(event)) {
//Do my thing
}
break;
}
return true;
}
return super.dispatchKeyEvent(event);
}
Yeah :D