[Solved]How to get key pressed on html5 platform?(TextBox)

I’m trying to get keystrokes using javascript, and the InputManager class from gdjs API: http://4ian.github.io/GD-Documentation/GDJS%20Runtime%20Documentation/classes/gdjs.InputManager.html

[code]if(inputmanager.anyKeyPressed()){

var key = String.fromCharCode(inputmanager.getLastPressedKey());
var container = objects[0].getVariables();
var text = container.get("text").getAsString()+key;
objects[0].setVariableNumber(container.get("text"),text);

}[/code]

I guess I should use the name of the InputManager class in lowercase (inputmanager).

And to get the reference to object of object’s variable “text” I used container.get( “text”).

But the script does not work.

EDITED.

Hi,

You forgot the () after anyKeyPressed (it’s a method).

Indeed, I have corrected that detail but still does not work.

Even if I try this script the screen is showing 100%, and does not load the scene.

if(inputmanager.anyKeyPressed()){}

I had to incorporate a timer to 0.2 seconds, and enter the script in a sub-event.

I don’t think that the “inputmanager” variable exists. You need to get it by doing this :

runtimeScene.getGame().getInputManager()

(I’m not sure if its runtimeScene or scene, it’s probably written in the JS event editor)

Yes, it’s runtimeScene, but getInputManager method is not in RuntimeGame class: http://4ian.github.io/GD-Documentation/GDJS%20Runtime%20Documentation/classes/gdjs.RuntimeGame.html

Using getInputManager() method, we can access to InputManager runtime instance.

[code]var inputmanager = runtimeScene.getGame().getInputManager();

if(inputmanager.anyKeyPressed()){

var key = inputmanager.getLastPressedKey();
window.alert(key );

}[/code]

But, anyKeyPressed() method continue working incorrectly. If I press a key one time, the script show the key message nonstop.

Tomorrow, i will continue.

Finally, I managed to detect and store in a global variable keystrokes, to create a textbox.

Has been necessary to use two events, one to detect uppercase and lowercase letters, and another to detect special keys:


//To detect low and uppercase.
document.onkeypress=KeyPressed;
function KeyPressed(e)
{

    var evtobj=window.event? event : e; //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
    var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
    if((unicode>=65 && unicode<=90)||(unicode>=97 && unicode<=122))
	{
		var actualkey=String.fromCharCode(unicode);
		var container=runtimeScene.getGame().getVariables();
		var text = container.get("gText").getAsString()+actualkey;
		container.get("gText").setString(text);	
	}
	
}


//To detect special keys
document.addEventListener("keydown", KeyCheck);  //or however you are calling your method
function KeyCheck(e)
{
    var evtobj=window.event? event : e; //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
    var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
   switch(unicode)
   {
      case 8:
      //alert("backspace");
	  var container=runtimeScene.getGame().getVariables();
	  var text = container.get("gText").getAsString();
	  if(text.length>0)
	  {
	  text = text.substring(0,text.length-1);
	  container.get("gText").setString(text);
	  }	  
      break; 
      case 46:
      //alert("delete");
      break;
      default:
      break;
   }

}