Keyboard input

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
bigzpanda
Posts: 8
Joined: Fri Dec 21, 2007 11:24 am

Keyboard input

Post by bigzpanda »

Good morning,
I've just discovered Irrlicht and my first questions are coming. :)
I would like to get to keyboard entry and put it into a statictext.

Code: Select all

const char* input;
stringc charconvert;
class TraitementDesMessages : public IEventReceiver
{
public:

	virtual bool OnEvent(SEvent event)
	{
		if (event.EventType == EET_KEY_INPUT_EVENT  && event.KeyInput.PressedDown)
		{
  			
			stringc charconvert=key2string(event.KeyInput.Key);
			input = charconvert.c_str();
			printf("%s",input);
		return true;
	}
	}
};
(key2string is a function given in this forum)
So with this little code when the user presses on a letter, the programm writes it in the console.
But now, how can I do to write the letter in my statictext ?

Thank you for your future help !
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: Keyboard input

Post by Acki »

bigzpanda wrote:(key2string is a function given in this forum)
that's nice, but what does it do? :lol:

well, you should learn general coding first... :twisted:
you declare charconvert two times, first as a global and then as a local !!! :lol:
Then input is declared as global and even not needed at all...

you need a global string to hold the complete text...
then instead of printing the key input you add it to this string...
this string you can use for example to set the text of a static text box...

for example:

Code: Select all

stringc theString;
class TraitementDesMessages : public IEventReceiver
{
public:

	virtual bool OnEvent(SEvent event)
	{
		if (event.EventType == EET_KEY_INPUT_EVENT  && event.KeyInput.PressedDown)
		{
  			// add key to string
  			theString += key2string(event.KeyInput.Key).c_str();
  			// set string as text for staticText box
			txtBox->setText(theString.c_str());
		return true;
	}
	}
};
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
bigzpanda
Posts: 8
Joined: Fri Dec 21, 2007 11:24 am

Post by bigzpanda »

Hi Acki and thank you for the help.
Since that point, i have no problem, but what I don't understand is:

Code: Select all

debuginput->setText(theString.c_str()); 
I can't use debuginput and this function, because I declare them after, in the main function:

Code: Select all

int main(void)
{
...
IGUIStaticText *debuginput = gui->addStaticText(L">>", core::rect<s32>(5,768-85,1024,768), false, true, 0, -1, false);
...
}
My compilator says:"'debuginput' was not declared in this scope."
How can i do ?
mqrk
Posts: 16
Joined: Mon Dec 10, 2007 5:55 am

Post by mqrk »

then make debuginput global (global variables are declared outside of any function)
bigzpanda
Posts: 8
Joined: Fri Dec 21, 2007 11:24 am

Post by bigzpanda »

Good morning mqrk,
I've been trying to declare debuginput as global, but with any method, I get an error. Could you make me an example of what you mean in this snippet ?
#include "global.h"
#include "fonctions.h"

stringc theString;
class TraitementDesMessages : public IEventReceiver
{
public:

virtual bool OnEvent(SEvent event)
{
theString += key2string(event.KeyInput.Key).c_str();
debuginput->setText(theString.c_str());
return true;
}
}
};



int main(void)
{
//initialisation moteur
TraitementDesMessages traitement;
IrrlichtDevice *device = createDevice (video::EDT_OPENGL, core::dimension2d<s32>(1024,768),32,false,true,false,&traitement);
video::IVideoDriver* driver = device->getVideoDriver ();
scene::ISceneManager *scenegraph = device->getSceneManager();


IGUIStaticText *debuginput = gui->addStaticText(L">>", core::rect<s32>(5,768-85,1024,768), false, true, 0, -1, false);
//boucle de rendu
while (device->run())
{
//rendu
driver->beginScene(true, true, video::SColor(0,0,0,0));
scenegraph->drawAll ();
driver->endScene ();
device->drop ();
return 0;
}
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

If you don't know how to make a variable global, then you realy should read some C/C++ books first !!! :roll:

another question, why don't you simply use an edit box ???
#include "global.h"
#include "fonctions.h"

stringc theString;
IGUIStaticText *debuginput;
class TraitementDesMessages : public IEventReceiver
{
public:

virtual bool OnEvent(SEvent event)
{
theString += key2string(event.KeyInput.Key).c_str();
debuginput->setText(theString.c_str());
return true;
}
}
};



int main(void)
{
//initialisation moteur
TraitementDesMessages traitement;
IrrlichtDevice *device = createDevice (video::EDT_OPENGL, core::dimension2d<s32>(1024,768),32,false,true,false,&traitement);
video::IVideoDriver* driver = device->getVideoDriver ();
scene::ISceneManager *scenegraph = device->getSceneManager();


debuginput = gui->addStaticText(L">>", core::rect<s32>(5,768-85,1024,768), false, true, 0, -1, false);
//boucle de rendu
while (device->run())
{
//rendu
driver->beginScene(true, true, video::SColor(0,0,0,0));
scenegraph->drawAll ();
driver->endScene ();
device->drop ();
return 0;
}
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
bigzpanda
Posts: 8
Joined: Fri Dec 21, 2007 11:24 am

Post by bigzpanda »

I know how to declare a variable as global and I had written exactly the same code.
In fact, I've been trapped with your code:

Code: Select all

debuginput->setText(theString.c_str()); 
This doesn't work, because setText must have a wchar_t for its parameter.

The working code is

Code: Select all

			swprintf(input, 100, L"%s", key2string(event.KeyInput.Key).c_str());
			wcscat(inputtext,input);
			debuginput->setText(inputtext);  
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

ahh, yeah... ;)
you can also try to declare the string as a wide char...

Code: Select all

stringw theString;
then it should work without converting... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply