key press

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
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

key press

Post by JBGame »

Hey,

how would i make receiver.IsKeyDown , so that it can only be pressed once? if you get me..because when i do receiver.IsKeyDown(KEY_KEY_W), pressing the W key is like pressing it 10+ times even when i just pressed it once.
I know with GetAsyncKeyState you can do:

Code: Select all

If(GetAsyncKeyState(0xWHATEVER) & 1)
{

}
Nalin
Posts: 194
Joined: Thu Mar 30, 2006 12:34 am
Location: Lacey, WA, USA
Contact:

Post by Nalin »

Keep a key state:

Code: Select all

// Create a key state that contains all the possible keys.
bool key_state[irr::KEY_KEY_CODES_COUNT];
...
// Set all the values in the key state to false.
memset((void*)key_state, 0, sizeof(bool) * irr::KEY_KEY_CODES_COUNT);
...
bool CGameEventReceiver::OnEvent(const SEvent& event)
{
	switch (event.EventType)
	{
		case irr::EET_KEY_INPUT_EVENT:
		{
			// Alter our key state.  If the key is pressed, it sets the state to true, else if it is released, it will be set to false.
			key_state[event.KeyInput.Key] = event.KeyInput.PressedDown;
		}
	}
}
...
// Check and see if the A key is pressed.
if (key_state[irr::KEY_KEY_A]) doStuff();
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

Hi,

i don't think you get what i'm trying to say(sorry can't explain it properly..)

i'm using:

Code: Select all

class MyEventReceiver: public IEventReceiver { 
public: 
   // This is the one method that we have to implement 
   virtual bool OnEvent(const SEvent& event) { 
      // Remember whether each key is down or up 
      if (event.EventType == irr::EET_KEY_INPUT_EVENT) 
         KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; 

      return false; 
   } 

   // This is used to check whether a key is being held down 
   virtual bool IsKeyDown(EKEY_CODE keyCode) const { 
      return KeyIsDown[keyCode]; 
   } 

   MyEventReceiver() { 
      for (u32 i = 0; i < KEY_KEY_CODES_COUNT; ++i) 
         KeyIsDown[i] = false; 
   } 

private: 
   // We use this array to store the current state of each key 
   bool KeyIsDown[KEY_KEY_CODES_COUNT]; 
}; 
and using:

Code: Select all

if(receiver.IsKeyDown(KEY_KEY_W))
{
print("TEST");
}
When i JUST press the W key ONCE, it will act as if i've held down the W key, and i want to know how to stop this.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Release the key faster ;)

When you just want to have one action when the key is pressed then check for the key release event and put your logic there.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
no_response
Posts: 26
Joined: Fri Aug 20, 2010 11:07 am
Location: Bulgaria

Post by no_response »

i would do

Code: Select all

bool keypressed = receiver.IsKeyDown(KEY_KEY_W);
...


while(device->run()){
...
...

if(!keypressed){
if(receiver.IsKeyDown(KEY_KEY_W)){ print("TEST");}
keypressed = 1;
}
else{
keypressed = receiver.IsKeyDown(KEY_KEY_W);
} 

...
...
}
lol...
cobra
Posts: 371
Joined: Fri Jan 23, 2009 2:56 am
Location: United States
Contact:

Post by cobra »

Code: Select all

if(event.KeyInput.Key == KEY_KEY_W && event.KeyInput.PressedDown == false)
{
       doSomething();
}
Josiah Hartzell
Image
Joshee23
Posts: 1
Joined: Fri Oct 01, 2010 2:19 am

Post by Joshee23 »

Thnak you very much guys for the information you posted..
It really helps me a lot...
Newbie here seeking for Knowledge and friends too...
Godspeed,,,how to deal with depression
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

Hi,

thanks guys, got it to work :D
Post Reply