I played around with some code i gotten of this forum until it worked. The problem I had (and many others too) was to be able to check the same key (for updown/downup presses) any number of times the same loop. It wasnt to hard to implement, the answer was to update keystatus once per loop, not once per click. The speed of doubleclick can easily be changed with a func-call. For now only mousebutton can be doubleclicked (to save loops in the update) but if you do need it for non-mouse keys its very easy to add support for that yourself. The code below is used in this manner:
-------------------------------------------------------------------------------------
create a object of the class and send a pointer to is as the last perameter to createDevice when setting up irrlicht (just like normal).
Then put the myFunnyKeyHandler.updateKeys(); LAST in your mainloop. You gotta put it last but then you're set.
Mouse and keys are used by the same function-calls:
if(mfkh.keyDoubleclick(KEY_LBUTTON))
washSanta();
if(mfkh.keyDown(KEY_KEY_K) && mkh.keyDownUp(KEY_LBUTTON))
killSanta();
The last will kill Santa if you hold "k" down and click left mousebutton.
Hope this will help someone
Suliman
Your trustworthy trader in sweets and sandstone
------------------------------------------------------------------------------------
Code: Select all
class keyHandler : public irr::IEventReceiver{
public:
int doubleclickSpeed;
int doubleclickDelay[3];
bool key_buf[256];
bool key_buf_old[256];
bool mouse_buf[3];
bool mouse_buf_old[3];
public:
keyHandler(){Init();};
virtual ~keyHandler(){
};
void Init()
{
memset(key_buf, 0, 256);
memset(key_buf_old, 0, 256);
memset(mouse_buf, 0, 3);
memset(mouse_buf_old, 0, 3);
memset (doubleclickDelay,0,3);
doubleclickSpeed=15;
}
virtual bool OnEvent(irr::SEvent event)
{
switch(event.EventType)
{
case irr::EET_KEY_INPUT_EVENT:
key_buf[event.KeyInput.Key] = event.KeyInput.PressedDown;
break;
case irr::EET_MOUSE_INPUT_EVENT:
if (event.MouseInput.Event < irr::EMIE_MOUSE_MOVED)
{
mouse_buf[event.MouseInput.Event%3] = ((event.MouseInput.Event/3)?false:true);
}
break;
}
return true;
}
bool keyDown(int index)
{
switch (index){
case 0x01: return mouse_buf[0]; break;
case 0x02: return mouse_buf[1]; break;
case 0x04: return mouse_buf[2]; break;
default: return key_buf[index]; break;
}
}
bool keyUpDown(int index)
{
switch (index){
case 0x01: return (mouse_buf[0] && !mouse_buf_old[0]); break;
case 0x02: return (mouse_buf[1] && !mouse_buf_old[1]); break;
case 0x04: return (mouse_buf[2] && !mouse_buf_old[2]); break;
default: return (key_buf[index] && !key_buf_old[index]); break;
}
}
bool keyDownUp(int index)
{
switch (index){
case 0x01: return (!mouse_buf[0] && mouse_buf_old[0]); break;
case 0x02: return (!mouse_buf[1] && mouse_buf_old[1]); break;
case 0x04: return (!mouse_buf[2] && mouse_buf_old[2]); break;
default: return (!key_buf[index] && key_buf_old[index]); break;
}
}
bool doubleclick(int index){
switch (index){
case 0x01: return (keyUpDown(0x01) && doubleclickDelay[0]>0); break;
case 0x02: return (keyUpDown(0x02) && doubleclickDelay[1]>0); break;
case 0x04: return (keyUpDown(0x04) && doubleclickDelay[2]>0); break;
}
return false;
}
void updateKeys(){
for (int i=0;i<256;i++)
key_buf_old[i]=key_buf[i];
mouse_buf_old[0]=mouse_buf[0];
mouse_buf_old[1]=mouse_buf[1];
mouse_buf_old[2]=mouse_buf[2];
doubleclickDelay[0]--;
doubleclickDelay[1]--;
doubleclickDelay[2]--;
if(mouse_buf[0])
doubleclickDelay[0]=doubleclickSpeed;
if(mouse_buf[1])
doubleclickDelay[1]=doubleclickSpeed;
if(mouse_buf[2])
doubleclickDelay[2]=doubleclickSpeed;
}
void setDoubleclickSpeed(int setSpeed){
doubleclickSpeed=setSpeed;
}
};