SDL 2.0 Andriod KEYCODE_BACK (SDL events to handle it)

Hello Dudes,

I’ve had this issue for a while now, when I press back button on any android device, I have to exit. It’s seems like this doing it in Java Layer.

I want SDL 2.0 Events to handle this, this way if someone press’s back, they can get back to Menu Screen and then Exit out of the Game.

What I would like to know, is how do i do this.

  1. Get SDL 2.0 events to control the back_button
  2. Then figure out a way for SDL events terminate the Java application ( like tell the Java application to terminate) something like this I have in my code. System.exit(0);

I’m suspecting I have to return false in the KeyEvent.KEYCODE_BACK code.
Then in SDL figure out when the back button is being pressed?

This basically just like exiting a game, but going to menu screen first.

Thanks Dudes.

// JAVA CODE
public boolean onKey(View v, int keyCode, KeyEvent event) {

	//VOLUME ADDED BY TIMMY
	if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
		keyCode == KeyEvent.KEYCODE_VOLUME_UP) 
	{
		return false;
	}
	
	if (keyCode == KeyEvent.KEYCODE_BACK) 
	{
		//return false;
		//Log.v("SDL", "BACK key pressed");
		System.exit(0);
		//SDLActivity.nativeQuit();
	}
	
	if (keyCode == KeyEvent.KEYCODE_MENU) 
	{
		return false;
		
		//Log.v("SDL", "Settings key pressed");
		//System.exit(0);
		
	}
	
	if (keyCode == KeyEvent.KEYCODE_POWER) 
	{
		return false;
		//Log.v("SDL", "Settings key pressed");
		//System.exit(0);
	}
	
	if (event.getAction() == KeyEvent.ACTION_DOWN) {
        //Log.v("SDL", "key down: " + keyCode);
        SDLActivity.onNativeKeyDown(keyCode);
        return true;
    }
    else if (event.getAction() == KeyEvent.ACTION_UP) {
        //Log.v("SDL", "key up: " + keyCode);
        SDLActivity.onNativeKeyUp(keyCode);
        return true;
    }
    
    return false;
}

2013/12/27 Timodor

Hello Dudes,

I’ve had this issue for a while now, when I press back button on any
android device, I have to exit. It’s seems like this doing it in Java
Layer.

I want SDL 2.0 Events to handle this, this way if someone press’s back,
they can get back to Menu Screen and then Exit out of the Game.

What I would like to know, is how do i do this.

  1. Get SDL 2.0 events to control the back_button

Look in SDL source code, in test/controllermap.c, search for
"SDLK_AC_BACK", you’ll see an example of how to get the back button key
presses.

  1. Then figure out a way for SDL events terminate the Java application (
    like tell the Java application to terminate) something like this I have in
    my code. System.exit(0);

If you quit your app from the C side (return from main ), the Java side
will exit as well.

Gabriel.

I had to update to last snapshot of the SDL 2.0 code also changed to a new Java file.

The events are workings now, but it seems like the Java Back and SDL back are working at the same time. As it now going back to the menu screen for a split second.
If I disable it to ignore the back button, SDL not longer receives it either.

I guess what i want is for SDL to receive but for Java to ignore.

Also

if (keyCode == KeyEvent.KEYCODE_POWER)
{
//return true;
Log.v(“SDL”, “POWER key pressed”);
System.exit(0);
}

With this new code and new Java file it no longer pick’s KEYCODE _POWER event.
Before if anyone pressed the power, button it will be Automatic Exit.

Now it longer pick it’s up, goes on pause and loss’s the GL Context. Which makes it impossible to quit.

I ended up going back to the old SDL source since the new Java file broke the layout of the screen also needed a new SDL source for it work.

The events work with both my applications, same SDL source and same Java file.
But one application works perfectly fine when hitting the back button, the other works for 1 second then exits.
I’m so baffled since the code is the same.

Finally fixed that issue by commenting out a few event checks.

The issue is now exiting out. since I’m using an older version of SDL 2.0 older Java file, I don’t think exit feature works. If i implement the new SDL source.

Means my screen-layout wouldn’t function. (Which means ADMOB won’t work)

" // Add to layout
LinearLayout root = (LinearLayout) findViewById(R.id.root);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
root.addView(mSurface, lp);
"

Can’t believe something so simple on SDL PC computer, is thehardest thing on the Android.

Ok, I downloaded latest version of SDL via Planet Mercury!

Used the latest Java file, fixed up all the Layout and Ad’s
It’s working, the events, the back :stuck_out_tongue:

Only one last problem, it’s exiting out of the application find. But the process seems to be still active, when you try loading it again. a It’s black screen.

I’ve returned Returned 0; in my main function.

No idea.

got it working!!!

Just added exit() code in the main() :slight_smile:

Thanks Dude…

Were you using SDL_Quit()? I think it does call exit() on Android and
cleans up stuff besides.

Jonny DOn Sat, Dec 28, 2013 at 5:03 AM, Timodor wrote:

got it working!!!

Just added exit() code in the main() [image: Smile]

Thanks Dude…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

int main(int argc, char *argv[])
{

exit(0);

}

I didn’t call SDL exit

I never tried exit(o); maybe it would of worked in the old SDL source file?

Even will all this updated, there seem to be on-pause issues. Doesn’t seem to be working 100%, but 97%. One phone has 50% major slow down in game, after return from onpause. The other 2 devices worked fine.

seems like exit(0); is working fine with the old Java file and the Older SDL 2.0 source.

I think I might have updated all this for no reason.

TO USE BACK BUTTON ON THE ANDROID DEVICE
if (event.type == SDL_KEYDOWN)
if (event.key.keysym.sym == SDLK_AC_BACK )
// exit loop

then exit(0); in the main.cpp or main.c make sure you remove SDL_Quit(); …I think seems to hang.

There you go :slight_smile:

2013/12/28 Timodor

int main(int argc, char *argv[])
{

exit(0);
}

I didn’t call SDL exit

I never tried exit(o); maybe it would of worked in the old SDL source file?

Even will all this updated, there seem to be on-pause issues. Doesn’t seem
to be working 100%, but 97%. One phone has 50% major slow down in game,
after return from onpause. The other 2 devices worked fine.

Do not issue the exit command, that terminates all threads. Simply exit
from the main loop and the Java thread will detect that and terminate in
turn.–
Gabriel.

Btw. I’ve seen the audio thread of sdl mixer (at least the music) still
running even after the mainloop was left. Can anyone confirm?Am 31.12.2013 01:25 schrieb “Gabriel Jacobo” :

2013/12/28 Timodor

int main(int argc, char *argv[])
{

exit(0);
}

I didn’t call SDL exit

I never tried exit(o); maybe it would of worked in the old SDL source
file?

Even will all this updated, there seem to be on-pause issues. Doesn’t
seem to be working 100%, but 97%. One phone has 50% major slow down in
game, after return from onpause. The other 2 devices worked fine.

Do not issue the exit command, that terminates all threads. Simply exit
from the main loop and the Java thread will detect that and terminate in
turn.


Gabriel.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

int main(int argc, char *argv[])
{
gameloop

exit(0);
}

It’s been done in the main, you mean the SDL_Exit()?

I tried Return 0, Return 1, it never worked, but exit(0); in the main.cpp worked.