Home Menu Back Search buttons

Monkey Targets Forums/Android/Home Menu Back Search buttons

Bremer(Posted 2011) [#1]
I cannot seem to find out how to check if a user is pressing one of the fixed buttons most Android phones seems to have. I would like to be able to check if the Back button is pressed and have my game return to the main menu selection screen.

Does anyone know if this is possible and how it is done? I did search for it on the forum but did not find anything specific on this.


Hima(Posted 2011) [#2]
The back button is map to escape button, I think. As for the others, I have no idea :(


Virtech(Posted 2011) [#3]
This will allow you to exit the app when the back button is hit...

	Method OnUpdate ()
	
		' Quit App
		If KeyHit(KEY_ESCAPE)
			Error ""
		EndIf
			
	End


I dont think the other adroid buttons are functional in monkey yet.


Supertino(Posted 2011) [#4]
Would love to know what the back button is mapped to... if at all?


therevills(Posted 2011) [#5]
@Supertino - as Hima has said and Virtech has shown the back button is mapped to KEY_ESCAPE


Perturbatio(Posted 2011) [#6]
The home button should not be overridden, it should allow the user to return to the home screen, this is how they "quit" the app.

according to the android docs finish() should be invoked to end your application:


public void finish ()
Since: API Level 1

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().




Supertino(Posted 2011) [#7]
@therevills Sorry meant to say Menu button.


Xaron(Posted 2011) [#8]
I second that. Anyone knows how the Menu button is mapped?


therevills(Posted 2011) [#9]
Hmmmm reading this:

http://developer.android.com/guide/topics/ui/menus.html

http://developer.android.com/guide/topics/search/search-dialog.html

Apart from adding the code directly to the mojo android target, Im not sure if the community can do it by themselves without creating a new Android target.

I guess we need Mark to add the following commands to mojo:

public boolean onSearchRequested()

public boolean onCreateOptionsMenu(Menu menu)


And assign them to a keyhit state, like public void onBackPressed():

	public void onBackPressed(){
		if( app!=null ){
			app.input.OnKeyDown( 27 );
			app.input.OnKeyUp( 27 );
		}
	}



therevills(Posted 2011) [#10]
Just tested and it seems to work:

Monkey code which flashes the screen and quits when "Escape" is hit:


Adding the following code to mojo.android.java in the MonkeyGame class:



So when running the app on Android, if you now press either the Menu, Back or Search buttons the application will end.


Loofadawg(Posted 2011) [#11]
Nice! Thanks for doing the footwork.
Currently I don't suppose there is a way to use the Menu button so we can use it as an actual Menu within app instead of a way to exit?

Right now Virtech's method is how my app closes. I plan to write some additional code to bring up a dialog box to verify whether the user really wishes to quit and/or use the Back button as a way to access the app menu.


therevills(Posted 2011) [#12]
Currently I don't suppose there is a way to use the Menu button so we can use it as an actual Menu within app instead of a way to exit?


I was just testing Escape key with the onSearchRequested and onCreateOptionsMenu commands... to create an actual Android menu, you will have to create an XML file and alter the mojo android java file even more...

Have a read of this: http://developer.android.com/guide/topics/ui/menus.html

Of course you could alter the key which is "hit" on the buttons and create your own Monkey dialogs:

If KeyHit(KEY_ESCAPE) ' Back Button Pressed
	Error ""
End

If KeyHit(KEY_F1) ' Menu Button Pressed
	New MenuDialog()
End

If KeyHit(KEY_F2) ' Search Button Pressed
	New SearchDialog()
End


And the Android code:

	public boolean onSearchRequested(){
		if( app!=null ){
			app.input.OnKeyDown( 113 ); // F2 Key
			app.input.OnKeyUp( 113 );
		}
		return super.onSearchRequested();
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		if( app!=null ){
			app.input.OnKeyDown( 112 ); // F1 Key
			app.input.OnKeyUp( 112 );
		}
		return true;
	}



a1programmer(Posted 2011) [#13]
Here's the code for Menu -> Exit.


Java (activity)
	
@Override
public boolean onCreateOptionsMenu(Menu menu) {
	MenuInflater inflater = getMenuInflater();
	inflater.inflate(R.menu.menu, menu);
	return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	// Handle item selection
	switch (item.getItemId()) {

	case R.id.exit:
	    this.finish();
	    return true;
	default:
	   return super.onOptionsItemSelected(item);
	}

}


/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/exit" android:title="@string/exit" />
</menu>



/values/strings.xml
 <string name="exit">Exit</string>



Xaron(Posted 2011) [#14]
Thanks guys, very useful!


Bladko(Posted 2011) [#15]
for me works only first click. Nothing more is displayed and i cannot use this button any more until app is restarted.

If KeyHit(KEY_F1) 
	x = x + 1  'works only once
End


probably standard menu is created but due to missing buttons we cannot close it, that is why further pressing this key does not work


therevills(Posted 2011) [#16]
Change the java to return false instead of true - I think this will let you hit the key more than once ;)

	public boolean onCreateOptionsMenu(Menu menu) {
		if( app!=null ){
			app.input.OnKeyDown( 112 ); // F1 Key
			app.input.OnKeyUp( 112 );
		}
		return false;
	}




Bladko(Posted 2011) [#17]
thanks a lot, this is very helpful