One of the annoying things about IRRlicht for me was learning how to get the key inputs. After much testing and searching on the forum I've finally picked up a good way.
The only bug I have found is the mouse wheel doesn't seem to opperate correctly. Maybe someone can show me how to do that correctly.
//code
bool keys[irr::KEY_KEY_CODES_COUNT];//HOLDS THE INFO IF OUR KEYS ARE UP OR DOWN
f32 MOUSE_WHEEL;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
MOUSE_WHEEL = event.MouseInput.Wheel;//update mouse wheel as many chances as we can get
if(event.EventType == irr::EET_KEY_INPUT_EVENT)
{//if the keys gave input
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;//we set it on if it was pushed, off if it was let up
}
if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
//left mouse button
if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
{
keys[KEY_LBUTTON] = 1;//on
return true;//TRUE
}
if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
{
keys[KEY_LBUTTON] = 0;//off
return true;//TRUE
}
//right mouse button
if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
{
keys[KEY_MBUTTON] = 1;//on
return true;//TRUE
}
if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
{
keys[KEY_MBUTTON] = 0;//off
return true;//TRUE
}
//right mouse button
if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
{
keys[KEY_RBUTTON] = 1;//on
return true;//TRUE
}
if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
{
keys[KEY_RBUTTON] = 0;//off
return true;//TRUE
}
return true;//TRUE
}
return false;
}
};
I made this event receiver in it's own file so that it could be included in as many of my projects as I liked without having to do a lot of cutting and pasting. Now how to use this.
I modified the movement tutorial and moved the mode around like this
core::vector3df v = node->getPosition();
v.X += (keys[irr::KEY_KEY_W] - keys[irr::KEY_KEY_S]) * 0.1f;// : -2.0f;
v.Z += (keys[irr::KEY_KEY_A] - keys[irr::KEY_KEY_D]) * 0.1f;
v.Y += (EMIE_LMOUSE_PRESSED_DOWN - EMIE_RMOUSE_PRESSED_DOWN) * 0.1f;
node->setPosition(v);
So in that example you push the W key or the S key to move along the X axsis. The A and D keys to move along the Z axis, and the Left or right mouse to move along the Y axsis.
To see this in action check my highly modifed version of the movement tutorial at www.foxhound-production.com/irrlicht/movement.zip .
Key and mouse Input
-
- Posts: 275
- Joined: Fri May 12, 2006 6:37 pm
- Location: Germany
i had the same problem, when i returned with true, if there wasn´t any event... are you shure, you end the event with false, i see 2 trues in the end, but i was to lazy for looking exactly....
the wheel works as following:
if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
{
wheelSpeed=event.MouseInput.Wheel;
}
the you can access the wheelspeed ofer blub=myEvent->wheelSpeed;
(i used a float for wheelspeed, but it only ist 1 or -1 so i think a integer would work, too)
TGM
the wheel works as following:
if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
{
wheelSpeed=event.MouseInput.Wheel;
}
the you can access the wheelspeed ofer blub=myEvent->wheelSpeed;
(i used a float for wheelspeed, but it only ist 1 or -1 so i think a integer would work, too)
TGM
-
- Posts: 275
- Joined: Fri May 12, 2006 6:37 pm
- Location: Germany
-
- Posts: 275
- Joined: Fri May 12, 2006 6:37 pm
- Location: Germany
Code: Select all
#include "irrlicht.h"
#ifndef __KeyBoard_TYPES_H_INCLUDED__
#define __KeyBoard_TYPES_H_INCLUDED__
using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class LevelEdEvent : 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];
float wheelSpeed;
void Init();
virtual bool OnEvent(SEvent event);
bool IsKeyDown(int index);
bool IsKeyUpDown(int index);
bool IsKeyDownUp(int index);
bool IsMouseButtonDown(int index);
bool IsMouseButtonUpDown(int index);
bool IsMouseButtonDownUp(int index);
};
#endif
"float wheelSpeed;" which will hold the speed of the mousewheel
Code: Select all
#include "LevelEdEv.h"
#include "irrlicht.h"
using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
void LevelEdEvent::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);
}
bool LevelEdEvent::OnEvent(SEvent event)
{
//handel Gui events here
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
IGUIEnvironment* env = device->getGUIEnvironment();
ISceneManager* smgr = device->getSceneManager();
switch(event.GUIEvent.EventType)
{
case EGET_FILE_SELECTED:
{
//if a file was opened in a file open dialog
break;
}
case EGET_MENU_ITEM_SELECTED:
{
IGUIContextMenu* menu = (IGUIContextMenu*)event.GUIEvent.Caller;
s32 id = menu->getItemCommandId(menu->getSelectedItem());
switch(id)
{
case 99:
{
// Menue buttons clicked
}
break;
case 101:
{
}
break;
}
break;
}
case EGET_BUTTON_CLICKED:
switch(id)
{
case 306:
// Tab buttons clicked
break;
}
break;
}
}
switch(event.EventType) // from here on its exactly the Boolkey eventreciever out of the wiki except of the seting of wheelspeed
{
//Keyboard evenbt------------------------------------------------------------
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;
//Mouse event----------------------------------------------------------------
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);
}
if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
{
wheelSpeed=event.MouseInput.Wheel;
}
break;
}
return false;
}
bool LevelEdEvent::IsKeyDown(int index){ return m_key_buf[index];}
bool LevelEdEvent::IsKeyUpDown(int index){return (m_key_buf[index] && !m_key_buf_old[index]);}
bool LevelEdEvent::IsKeyDownUp(int index){return (!m_key_buf[index] && m_key_buf_old[index]);}
bool LevelEdEvent::IsMouseButtonDown(int index){return m_mouse_button_buf[index];}
bool LevelEdEvent::IsMouseButtonUpDown(int index){return (m_mouse_button_buf[index] && !m_mouse_button_buf_old[index]);}
bool LevelEdEvent::IsMouseButtonDownUp(int index){return (!m_mouse_button_buf[index] && m_mouse_button_buf_old[index]);}
TGM
yeah thanks
well another bug showed up
edit: lol fixed while copying
well another bug showed up
edit: lol fixed while copying
My Blog: http://www.freakybytes.org