About Monkey 2 › Forums › Monkey 2 Programming Help › [ MONKEYX2 ] Android Back button
This topic contains 13 replies, has 4 voices, and was last updated by
Amon
1 year, 4 months ago.
-
AuthorPosts
-
December 12, 2017 at 11:30 pm #12252
What keyboard or mouse keys are the equivalent to the back button on android?
I’m using a galaxy s4 for testing.
December 13, 2017 at 7:10 am #12255No idea sorry! Havent actually done any Android development in Monkey2 yet…
I’ll investigate, unless you or anyone else knows!
Edit: Quick Google in pure SDL2 C++:
Monkey12if (event.type == SDL_KEYDOWN)if (event.key.keysym.sym == SDLK_AC_BACK )December 13, 2017 at 7:27 am #12256In the native keyinfo.cpp within Mojo, Mark has:
Monkey1"AC Back",SDL_SCANCODE_AC_BACK,SDLK_AC_BACK,KeyInfo is used in keyboard.monkey2:
Monkey12345678910Extern PrivateStruct bbKeyInfoField name:Void PtrField scanCode:IntField keyCode:IntEndGlobal bbKeyInfos:bbKeyInfo Ptr…
Monkey123456789101112131415161718192021222324252627282930Class KeyboardDevice...Method Init()Local p:=bbKeyInfosWhile p->nameLocal name:=String.FromCString( p->name )Local scanCode:=p->scanCodeLocal keyCode:=p->keyCodeLocal key:=KeyCodeToKey( keyCode )_names[key]=name_raw2scan[key]=scanCode_scan2raw[scanCode]=key | Key.Raw#If __TARGET__="emscripten"_key2scan[key]=scanCode#Else_key2scan[key]=SDL_GetScancodeFromKey( Cast<SDL_Keycode>( keyCode ) )#Endif_scan2key[_key2scan[key]]=keyp=p+1WendEndIn SDL_keycode.h
Monkey123456enum{...SDLK_AC_BACK = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_AC_BACK),And SDL_keyboard.c
Monkey12345static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES] = {...SDLK_AC_BACKSo we may be able to use it somehow…
December 13, 2017 at 8:06 am #12257I’ve copied some of the monkey keyboard code and found the scan values for AC_BACK:
Monkey12345name = AC BackscanCode = 270keyCode = 1073742094So Amon could you please add this code your project:
Monkey1234567Method KeyCodeToKey:Key( keyCode:Int ) ' copied from keyboard.monkey2If (keyCode & $40000000) keyCode=(keyCode & ~$40000000)+$80' If keyCode<=0 Or keyCode>=Int( Key.Max ) Return Null ' removed as back key goes beyond Key.MaxReturn Cast<Key>( keyCode )EndThen when you want to hit the back key:
Monkey123456Local backKey:=KeyCodeToKey( 1073742094 )If Keyboard.KeyHit(backKey)Print "BACK!"EndI havent tested this so this may or may NOT work!
December 13, 2017 at 11:54 am #12259Monkey12345Method KeyCodeToKey:Key( keyCode:Int ) ' copied from keyboard.monkey2If (keyCode & $40000000) keyCode=(keyCode & ~$40000000)+$80' If keyCode<=0 Or keyCode>=Int( Key.Max ) Return Null ' removed as back key goes beyond Key.MaxReturn Cast<Key>( keyCode )EndMonkey1234Local backKey:=KeyCodeToKey( 1073742094 )If Keyboard.KeyHit(backKey)App.Terminate()EndI added the top one to my GameScreen and the second in the GameScreen Update method. It doesn’t work though. Forgive me if I’m placing the code incorrectly.
Thank you though for helping.
December 14, 2017 at 7:41 am #12279Nope you put in the right place…
Damn, KeyHit calls KeyPressed which calls ScanCode… and ScanCode checks Key.Max
I tried an Extension and sub classing the KeyboardDevice class both failed due to it uses a lot of private variables so it complains. I think we need Mark to make a change to get it to work – sorry.
I’ll raise a GitHub ticket and point to this thread. Could you change title of this thread please (edit your first post) as its a Monkey2 issue than a Diddy2 issue
Raised: https://github.com/blitz-research/monkey2/issues/293
December 14, 2017 at 9:11 am #12281Thanks, therevills. Your help is appreciated a lot. Also, an apology is not needed as you’ve done nothing wrong.
Thread title has been edited as requested.
Ta!
December 14, 2017 at 11:50 am #12282What about Key.Escape?
December 14, 2017 at 11:55 am #12284First thing I tried….it doesn’t work though.
December 14, 2017 at 10:53 pm #12289Some fixes for this are now up in the develop branch.
It was mostly working already except there was an issue with sdl that meant the app wasn’t initally being given focus properly so key events weren’t getting sent to it. If you hit ‘home’ and refocus app, key events started working. I’ve hacked around this a bit and it should work now.
The other issue is that the back key wasn’t even include in the Key enum. This has been fixed too, so you can now go:
If Keyboard.KeyHit( Key.ACBack) App.Terminate()
(Anyone know what the ‘AC’ in ‘ACBack’ stands for?)
Here’s a little runnable demo which is working fine on my API15 emulator and nvidia shield.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )EndMethod OnKeyEvent( event:KeyEvent ) OverrideIf event.Type=EventType.KeyDown Print "KeyDown: "+Keyboard.KeyName( event.Key )If event.Type=EventType.KeyUp Print "KeyUp: "+Keyboard.KeyName( event.Key )EndMethod OnRender( canvas:Canvas ) OverrideRequestRender()' If Keyboard.KeyHit( Key.ACBack ) App.Terminate()canvas.DrawText( "Hello World!",Width/2,Height/2,.5,.5 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndDecember 14, 2017 at 11:06 pm #12290…also, I should probably mention that I updated the default android studio project for android products to android studio 3.0.1, so android users should probably do the same. Ditto upgrade all installed tools/sdks/ndks etc to the latest versions.
December 15, 2017 at 2:52 am #12292I thought AC stood for Android Code, but it looks like it stands for Application Control.
“AC Back” (the Back key (application control keypad))
https://wiki.libsdl.org/SDL_ScancodeAndKeycode
Thanks for fixing this.
December 15, 2017 at 6:47 pm #12310Oops, broke non-android builds a bit…fixed now.
December 16, 2017 at 8:59 am #12324It’s working now. Thank you again, Mark.
-
AuthorPosts
You must be logged in to reply to this topic.