Android Back Button?

Ninjadoodle

  • Posts: 313
Hi Guys

I looked everywhere on the forums but can't seem to find any info on this.

Is there a way for Stencyl to recognise the "Back Button" on an Android being pressed?

Thank you in advance!
It's all fun and games until somebody get's a shuriken in their eye!


GeorgeN

  • Posts: 856
I've tried to make a native extension but with no success.

Gintonic

  • Posts: 69
Inspired by captaincomic's Keyboard Input scene behavior (http://community.stencyl.com/index.php/topic,24713.msg141968.html#msg141968), for me this works:

Create a scene behavior and add it to the first scene.

Import block
------------
import nme.events.Event;
import nme.events.KeyboardEvent;
import nme.Lib;
import nme.ui.Keyboard;

When created block
-------------------

in a code block:

var lastEvent:KeyboardEvent;


Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, function(event) {
   lastEvent = event;
   // trace("Keydown: " + lastEvent.keyCode);
   if (lastEvent.keyCode==27) {
      lastEvent.stopImmediatePropagation();
      lastEvent.stopPropagation();
      // call your function here
      sayToScene("Global Functions Scene", "ControlBackButton", [getCurrentSceneName()]);
   }
});



Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, function(event) {
   lastEvent = event;
   // trace("Keyup: " + lastEvent.keyCode);
   if (lastEvent.keyCode==27) {
      lastEvent.stopImmediatePropagation();
      lastEvent.stopPropagation();
   }
});


Jon

  • *
  • Posts: 17524
Thanks for sharing. The key to this was stopping propagation, otherwise one could just bind the Escape key to a control. That detects the back button but does not prevent the default operation from happening (sending the app to the background).

I suppose the cleanest thing to do if we had to integrate it would be to add a game setting that let you shut off the default back button behavior and then detect the back button pressing using the regular keyboard detection events.

Jon

  • *
  • Posts: 17524
I've now added said option to the Mobile Settings > Input page.

Checking it will disable the default behavior of quitting the app, letting you use a keyboard control bound to the "Esc" key.

NGS

  • Posts: 66
I was actually going to request this as a feature when I saw the code.

When I went to enable it... It does not look as described.  Also, regardless of enabled or not it seems to have broken android output.



NGS

  • Posts: 66
I responded in the fixed bugs forum -- im not sure anybody checks that... so im double posting here to ensure somebody see's it.

http://community.stencyl.com/index.php/topic,21520.0.html

also, i cannot clear logs as it says "unable to delete file"

Jon

  • *
  • Posts: 17524
The errors in your log don't have anything to do with this change.

Checked in the missing language pack string.

NGS

  • Posts: 66
I'm not sure what's changed.  I've downloaded a win 8 update, reboot.  Saw the change and downloaded it.

I don't have any additional language packs installed for win 8 or stencyl

Jon

  • *
  • Posts: 17524
My comment was actually referring to Stencyl and how I forgot to check in the language entry for the new option. That is why it's showing up as 'missing'. If you redownload, you'll see the text for it now.

FortySe7en

  • Posts: 304
Gintonic - do you only need to attach that initial scene behavior to the very first scene and then just bind Keyboard UP to whatever I want the back button to do?

Gintonic

  • Posts: 69
Yes, with sayToScene(...) you can trigger an event. But now this all is built-in. You don't need to overwrite it on our own.

FortySe7en

  • Posts: 304
So, how can I trigger the back button to do custom thing I want it to do? (like go back in menus)

Gintonic

  • Posts: 69