Multiple key?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Keanu

Multiple key?

Post by Keanu »

How can I read a combination of key being pressed? If I press two keys at the same time I have to know that! :roll:
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

With your even reciever... try this. (i've never done this, its straight off the top of my head but should work)

Code: Select all

if(event.KeyInput.Key == KEY_LEFT && event.KeyInput.Key == KEY_UP)
{
     //CODE HERE
}
Hey you never know. Try different combinations.
Guest

Re: Multiple key?

Post by Guest »

Keanu wrote:How can I read a combination of key being pressed? If I press two keys at the same time I have to know that! :roll:
the best way to handle the keyboard is described in this thread http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=808

once you set up the array then you can do something like this

Code: Select all

// is CTRL and 'T' pressed down
if (keys[KEY_CONTROL] && keys[KEY_KEY_T]) {
   //do something
}
does this help you?
Keanu

Sorry

Post by Keanu »

It's half correct beacuse I can read multiple keys but the code isn't updated when a key is released? Do you know why?
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Re: Sorry

Post by rt »

Keanu wrote:It's half correct beacuse I can read multiple keys but the code isn't updated when a key is released? Do you know why?
post the code in your eventreciever, that might give us something to work with
Keanu

Post by Keanu »

I used the code you wrote in this thread
Keanu

Post by Keanu »

To be more clear...

class MyEventReceiver : public IEventReceiver {
public:
virtual bool OnEvent(SEvent event) {
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}
if (camera) return camera->OnEvent(event);
return false;
};

and in the main loop

if(keys[KEY_KEY_K])MessageBox(NULL,"","",MB_OK);

What's wrong? Also if I release the K button the message still appear! I
hope you can help.[/b]
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post by rt »

are you creating the key array like so?

Code: Select all

bool keys[irr::KEY_KEY_CODES_COUNT] ; 
//initialization 
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false; 
read the second last posting in http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=808 and make sure you followed the directions
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

okay, for gui stuff like that, you probably dont want to use the keys[] array.

what is currently happening in your code is that when the user presses K, it sets that true in the keys[] array. Then every update you're checking if K is pressed. If every update is only 10ns, that means you're checking this like 100x a second! and opening up that many message boxes!! Furthermore, you dont get a 'message' when the K key is released, because all that is happening is that the K element in keys[] is being set to false.

In fact, if your code is busy producing 1000s of message boxes, you might even miss the K-released message!

For stuff that should only happen once on key press, you cannot use the keys[] array this simply. In fact, the keys[] method is not good for GUI interactions, such as this or such as text input entries.

what keys[] IS good for, is letting you know at any time what the current state of the keyboard is. this is particularly useful for actions that need to poll the state of the keyboard every update, such as checking for buttons in an action game. For simple one-press, one-action evens, the keys[] array is not a good choice.
a screen cap is worth 0x100000 DWORDS
Keanu

Post by Keanu »

Maybe I wasn't so clear, probably this is caused by my bad english. Ok let's start again. First of all thank you to Keless and the others for their patience. I was checking if keys were being pressed in the event receiver with the usual switch case code. The result was my player could rotate or walk forward, but he cannot do both the things at the same time! Maybe I'm asking too much, but can I have some example source code. I'll be very grateful. :wink:
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post by rt »

instead of using a Switch statement in the event reciever, use an if statement in your main loop.
switch is maily used for picking one case from many, so using a series of if statements makes it possible for all of them to execute in your main loop.

Code: Select all

//create an array
bool keys[irr::KEY_KEY_CODES_COUNT] ; 
//initialization 
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false; 

//event stuff
class MyEventReceiver : public IEventReceiver { 
public: 
virtual bool OnEvent(SEvent event) { 
   //save key events to the array
   if(event.EventType == irr::EET_KEY_INPUT_EVENT){ 
      keys[event.KeyInput.Key] = event.KeyInput.PressedDown; 
      return true; 
   } 
if (camera) return camera->OnEvent(event); 
return false; 
}; 

//and in the main loop 
if (keys[KEY_KEY_W]) WalkForward();
if (keys[KEY_KEY_A]) RotateLeft();
if (keys[KEY_KEY_D]) RotateRight();
if (keys[KEY_ESCAPE] PostQuitMessage(0);
Post Reply