The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL 2.0 Andriod KEYCODE_BACK (SDL events to handle it)
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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;
}
SDL 2.0 Andriod KEYCODE_BACK (SDL events to handle it)
gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
2013/12/27 Timodor
Quote:
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.


 
Quote:
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);





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


Gabriel.
...
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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.
.....
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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.
...
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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.
...
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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.
...
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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 Razz

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.
...
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
got it working!!!!!!!!!!!!!!!!

Just added exit() code in the main() Smile


Thanks Dude....
SDL 2.0 Andriod KEYCODE_BACK (SDL events to handle it)
Jonny D


Joined: 12 Sep 2009
Posts: 932
Were you using SDL_Quit()?  I think it does call exit() on Android and cleans up stuff besides.

Jonny D



On Sat, Dec 28, 2013 at 5:03 AM, Timodor wrote:
Quote:
got it working!!!!!!!!!!!!!!!!

Just added exit() code in the main()


Thanks Dude....


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

....
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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.
...
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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 Smile
SDL 2.0 Andriod KEYCODE_BACK (SDL events to handle it)
gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
2013/12/28 Timodor
Quote:
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 2.0 Andriod KEYCODE_BACK (SDL events to handle it)
M. Gerhardy
Guest

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":
Quote:


2013/12/28 Timodor
Quote:
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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

...
Timodor


Joined: 14 Apr 2013
Posts: 72
Location: Australia
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.