Smart TV - mojo.input - remote control - for input

Monkey Targets Forums/Android/Smart TV - mojo.input - remote control - for input

matty(Posted 2012) [#1]
Hi all,

I'm not sure how to go about getting this to work.

We have a smart tv at work (runs android) which I'm trying to test some android apks generated from monkey with.

All the graphics and sound play fine.

My problem is with mojo's input routine.
The smart tv can use a mouse but we would prefer to create a few demos using the smart tv's own physical keyboard on its remote control.

Currently the only key that is detected is the "back" key (KEY_ESCAPE).
The remote has a qwerty style keyboard, cursor keys, and the standard android buttons. It also has the TV specific remote buttons on the reverse side of the remote.

Is there some way I can get monkey to recognise a key press from *any* of the keys?

I tried polling KeyHit(index) with index values from 0 to 255 with no luck on any of the keys (aside from the back key).

Thanks,
Matt


therevills(Posted 2012) [#2]
I would say you would need to "hack" mojo...

Have a look at the dispatchKeyEventPreIme method:

http://developer.android.com/reference/android/view/View.html#dispatchKeyEventPreIme(android.view.KeyEvent)

And the KeyEvents class:

http://developer.android.com/reference/android/view/KeyEvent.html


Aman(Posted 2012) [#3]
I think this page could be helpful:
https://developers.google.com/tv/android/docs/gtv_displayguide#Input

Does the arrow keys work?
If not, I think this will help

Add these few lines to either:
your generated project code (You will have to do it for every app)
mojo.android.java (Once only)


public boolean onKeyDown(int keyCode, KeyEvent event)
"At the end of the switch statement"
case 21:				// KEYCODE_DPAD_LEFT
	app.input.OnKeyDown( 37 );	// KEY_LEFT
	break;
case 19:				// KEYCODE_DPAD_UP
	app.input.OnKeyDown( 38 );	// KEY_UP
	break;
case 22:				// KEYCODE_DPAD_RIGHT
	app.input.OnKeyDown( 39 );	// KEY_RIGHT
	break;
case 20:				// KEYCODE_DPAD_DOWN
	app.input.OnKeyDown( 40 );	// KEY_DOWN
	break;
default:
	app.input.OnKeyDown( keyCode );


public boolean onKeyUp(int keyCode, KeyEvent event)
"At the end of the switch statement"
case 21:				// KEYCODE_DPAD_LEFT
	app.input.OnKeyUp( 37 );	// KEY_LEFT
	break;
case 19:				// KEYCODE_DPAD_UP
	app.input.OnKeyUp( 38 );	// KEY_UP
	break;
case 22:				// KEYCODE_DPAD_RIGHT
	app.input.OnKeyUp( 39 );	// KEY_RIGHT
	break;
case 20:				// KEYCODE_DPAD_DOWN
	app.input.OnKeyUp( 40 );	// KEY_DOWN
	break;
default:
	app.input.OnKeyUp( keyCode );


I have a good feeling that this is going to work. If it does, you will need to hard core more keys and I am not sure if Media keys will be treated similarly. There are many TV Buttons in the second link posted by therevills:
http://developer.android.com/reference/android/view/KeyEvent.html

I had to do the same to support the controller buttons on the Xperia Play:
http://monkeycoder.co.nz/Community/posts.php?topic=1341


matty(Posted 2012) [#4]
Thanks both of you, I will try this out today when I get the chance.

from Matt


matty(Posted 2012) [#5]
Hi again,

To Aman -

I've had a look through my mojo.android.java source code and I cannot find any "public onKeyDown" code...the closest thing is

	void OnKeyDown( int key ){
		if( (keyStates[key]&0x100)==0 ){
			keyStates[key]|=0x100;
			++keyStates[key];
		}
	}
	
	void OnKeyUp( int key ){
		keyStates[key]&=0xff;
	}



I am using an earlier version of monkey(v42)...and would rather not have to update if that means I have to re install the android sdk.

Is there some other way I can do this...perhaps by changing these:

Const KEY_LMB=1
Const KEY_RMB=2
Const KEY_MMB=3

Const KEY_BACKSPACE=8
Const KEY_TAB=9
Const KEY_ENTER=13
Const KEY_SHIFT=16
Const KEY_CONTROL=17
Const KEY_ESCAPE=27
Const KEY_SPACE=32
Const KEY_PAGEUP=33
Const KEY_PAGEDOWN=34
Const KEY_END=35
Const KEY_HOME=36

'etc etc



Aman(Posted 2012) [#6]
That's why keyboard doesn't work. There are no key action listeners on that version for android. The only reason the back button work is because of this function:
public void onBackPressed(){
	if( app!=null ){
		app.input.OnKeyDown( 27 );
		app.input.OnKeyUp( 27 );
	}
}


You could try adding this code to the end of
public static class MonkeyView extends GLSurfaceView
public boolean dispatchKeyEventPreIme( KeyEvent event ){
	if( app==null ) return false;
	
	if( app.input.keyboardEnabled ) {
		if( event.getKeyCode()==KeyEvent.KEYCODE_BACK ){
			if( event.getAction()==KeyEvent.ACTION_DOWN ){
				app.input.PutChar( 27 );
			}
			return true;
		}
	}else{
		if( event.getKeyCode()==KeyEvent.KEYCODE_BACK ){
			if( event.getAction()==KeyEvent.ACTION_DOWN ){
				app.input.OnKeyDown( 27 );
			}else if( event.getAction()==KeyEvent.ACTION_UP ){
				app.input.OnKeyUp( 27 );
			}
			return true;
		}
	}
	return false;
}
	
public boolean onKeyDown( int key,KeyEvent event ){
	if( app==null || !app.input.keyboardEnabled ) return false;
		
	if( event.getKeyCode()==KeyEvent.KEYCODE_DEL ){
		app.input.PutChar( 8 );
	}else{
		int chr=event.getUnicodeChar();
		if( chr!=0 ){
			if( chr==10 ) chr=13;
			app.input.PutChar( chr );
		}
	}
	return true;
	}

public boolean onKeyMultiple( int keyCode,int repeatCount,KeyEvent event ){
	if( app==null || !app.input.keyboardEnabled ) return false;
	
	gxtkInput input=app.input;
			
	String str=event.getCharacters();
	for( int i=0;i<str.length();++i ){
		int chr=str.charAt( i );
		if( chr!=0 ){
			if( chr==10 ) chr=13;
			input.PutChar( chr );
		}
	}
	return true;
}


This is a part of the new mojo file. If it doesn't work, I suggest you install the new version next to the old one.


matty(Posted 2012) [#7]
Thanks Aman,

I installed a later version of monkey (45c) which implements the new keyboard method (or seems to looking at mojo) with some limited success. (Note:I don't want to upgrade to the most recent monkey just yet however as I need to upgrade the android sdk which I can't do at the moment at work).

I think it could be an issue with our smart tv (it's just a test one at the moment) - it gets the first character pressed and then ceases getting input (however the same tv also has problems with its remote on some of its own software)....