Multiple key?
Multiple key?
How can I read a combination of key being pressed? If I press two keys at the same time I have to know that!
-
- Posts: 386
- Joined: Thu Sep 25, 2003 12:43 pm
- Contact:
With your even reciever... try this. (i've never done this, its straight off the top of my head but should work)
Hey you never know. Try different combinations.
Code: Select all
if(event.KeyInput.Key == KEY_LEFT && event.KeyInput.Key == KEY_UP)
{
//CODE HERE
}
Re: Multiple key?
the best way to handle the keyboard is described in this thread http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=808Keanu 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!
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
}
Re: Sorry
post the code in your eventreciever, that might give us something to work withKeanu 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?
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]
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]
are you creating the key array like so?
read the second last posting in http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=808 and make sure you followed the directions
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;
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.
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
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.
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.
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);