bool keys[]

A forum to store posts deemed exceptionally wise and useful
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

bool keys[]

Post by keless »

**NOTE: to see the finalized good code, read the last post by me


Hey all,

I am trying to implement the traditional bool keys[] array for proper key handling. Unfortunately, my attempt gives mixed results.

My current code:

Code: Select all

//definition
bool keys[sizeof(irr::EKEY_CODE)] ;
//initialization
for(int x=0; x< sizeof(irr::EKEY_CODE); x++) keys[x] = false;
//onEvent
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
   keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
   return true;
}
//use
 if(keys[irr::KEY_LEFT]) pos->X -= 1;
 if(keys[irr::KEY_RIGHT]) pos->X += 1;
 if(keys[irr::KEY_DOWN]) m_currBlob.Fall(10);

pressing left seems to make my block go to the right (and it doesnt stop when i let go of the key).
pressing right also makes it go to the right (but it does stop).
it also seems kind of 'sticky'
Last edited by keless on Fri Jan 02, 2004 4:36 pm, edited 1 time in total.
a screen cap is worth 0x100000 DWORDS
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

*bump*

Post by keless »

anyone? little help?
a screen cap is worth 0x100000 DWORDS
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Re: *bump*

Post by rt »

keless wrote:anyone? little help?
instead of
bool keys[sizeof(irr::EKEY_CODE)] ;
//initialization
for(int x=0; x< sizeof(irr::EKEY_CODE); x++) keys[x] = false;
how about
bool keys[irr::KEY_KEY_CODES_COUNT] ;
//initialization
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

Thats definately wanted for the size of the array (realized sizeof() was wrong last night)..

so, it works just fine now. Which is wierd, because I would have thought any problems from not having the correct size would have meant I was accessing outside of the array.. and accessing outside of a static array should give me a crash, right? Wierd..

anyhow, thanks!
a screen cap is worth 0x100000 DWORDS
Boogle
Posts: 162
Joined: Fri Nov 21, 2003 3:16 pm
Location: Toronto, Canada

Post by Boogle »

Hehe, not in C++! It happily references as far outside of your array as you want.
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

since this thread has been linked to as an example of proper key input handling, I will recap with the finalized code:

1) define a boolean keys array:
bool keys[irr::KEY_KEY_CODES_COUNT] ;

2) initialize it all to false:
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;

3) in your event handler, set the incoming key to true or false in the array:
(dont do any acting on input here, except things you want to happen immediately such as quit on ESC)

if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}

4) in your update function, act on the values in the array:
if(keys[irr::KEY_DOWN]) m_currBlob.Fall(10);


*NOTE: some people new to the engine have been sending their events to the Camera and returning that result. Then they complain that their key-handling code is never run. THATS BECAUSE THE CAMERA IS RETURNING WHETHER ITS HANDLING THE INPUT OR NOT. You have two options: put your key handling functions above camera, and if you dont handle the key, send the event to camera, or send the event to camera first, and if that returns false, then run your key handling code.
a screen cap is worth 0x100000 DWORDS
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

Moved to the Howto forum as it is good information.

Left pointer to it in Beginner forum.
Crud, how do I do this again?
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

I've always used the bool key[] method in all my Win32 but for some reason with Dev-C++ it crashes (even in Debug) with the for loop. I've never seen any other method of doing keypress (that is smooth) but does anyone know of one? With using the tutorial method movement is too... jittery.
Programmer of the Irrlicht Scene Editor:
Homepage - Forum Thread
Version 2 on its way!!
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post by thesmileman »

DarkWhoppy I used to use this method in Dev-C++ and it seemed to work just as well as in Visual C++. I would give you the code but I trashed it after switching everything over to VS 2003
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

Thats alright. I fixed my prob, I cannot use the for-loop but by setting only the keys I use to false it works fine.
Programmer of the Irrlicht Scene Editor:
Homepage - Forum Thread
Version 2 on its way!!
Mazer
Posts: 16
Joined: Sat Jun 19, 2004 12:21 pm

Post by Mazer »

This is fantastic... just what I needed. Thanks alot, keless!
Noob

Cant get it to work

Post by Noob »

I use the code above but I can't push two keys at the same time...what have I done wrong? :cry:
lifetree

to use multi-key and mouse-button

Post by lifetree »

class CIrrLichtEvent : public IEventReceiver
{
public:
bool m_key_buf[256];
bool m_key_buf_old[256];
bool m_mouse_button_buf[3];
bool m_mouse_button_buf_old[3];
public:
CIrrLichtEvent();
virtual ~CIrrLichtEvent();

void Init()
{
memset(m_key_buf, 0, 256);
memset(m_key_buf_old, 0, 256);
memset(m_mouse_button_buf, 0, 3);
memset(m_mouse_button_buf_old, 0, 3);
}


virtual bool OnEvent(SEvent event)
{
switch(event.EventType)
{
case EET_KEY_INPUT_EVENT:
m_key_buf_old[event.KeyInput.Key] = m_key_buf[event.KeyInput.Key];
m_key_buf[event.KeyInput.Key] = event.KeyInput.PressedDown;
break;
case EET_MOUSE_INPUT_EVENT:
if (event.MouseInput.Event < EMIE_MOUSE_MOVED)
{
m_mouse_button_buf_old[event.MouseInput.Event%3] = m_mouse_button_buf[event.MouseInput.Event%3];
m_mouse_button_buf[event.MouseInput.Event%3] = ((event.MouseInput.Event/3)?false:true);
}
break;
}

return true;
}

bool IsKeyDown(int index)
{
return m_key_buf[index];
}

bool IsKeyUpDown(int index)
{
return (m_key_buf[index] && !m_key_buf_old[index]);
}

bool IsKeyDownUp(int index)
{
return (!m_key_buf[index] && m_key_buf_old[index]);
}

bool IsMouseButtonDown(int index)
{
return m_mouse_button_buf[index];
}

bool IsMouseButtonUpDown(int index)
{
return (m_mouse_button_buf[index] && !m_mouse_button_buf_old[index]);
}

bool IsMouseButtonDownUp(int index)
{
return (!m_mouse_button_buf[index] && m_mouse_button_buf_old[index]);
}
};


* virtual IEventReceiver::OnEvent() function is like to window-message procedure.

GOOD LUCK!
chris

Post by chris »

Does this mean that each call to OnEvent registers only one key press or mouse action?
Is it possible to have an EventReceiver register multiple keypresses and mouse actions (simultaneously) in the one call, or would that require multiple EventReceivers - one for each potntial event type?
Or is there some way of using multiple instances of SEvent within the one EventReceiver?
SARIN
Posts: 139
Joined: Fri Oct 29, 2004 3:53 am

Re: to use multi-key and mouse-button

Post by SARIN »

woops, sry i forgot. no questions in this forum
sry
Post Reply