Keyboard Combinations (ctrl, shift, alt)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Pan
Posts: 24
Joined: Tue Jan 20, 2009 9:36 am
Location: Bavaria, Germany

Keyboard Combinations (ctrl, shift, alt)

Post by Pan »

Hi!

I'm having a problem with my keyboard detection and couldn't find the answer in the docu or older threads (most questions regarding this topic go way back and work with an array, what I really don't want to do, since the api seems to provide everything neccessary).

What I'm trying to do are simple combinations with the special keys ctrl, alt and shift. Now the event structure for a keyboard event already contains boolean values for Ctrl and Shift, which brings me to my first question: where is the Alt-boolean ..? (ie. how are combinations like alt-p implemented..?)

My current code then looks like this:

Code: Select all

if( event.KeyInput.Char == 'v' && event.KeyInput.PressedDown == false ){
                    ..do stuff..
}
This works exactly how I want it to. If I now go ahead and add an additional condition to check for Ctrl, it looks like this:

Code: Select all

if( event.KeyInput.Char == 'v' && event.KeyInput.PressedDown == false && event.KeyInput.Control){
                    ..do stuff..
}
When I now press Ctrl+v, nothing happens ..

So, I took the first version again without the Ctrl-check and tried Ctrl-v: nothing happened.
Just pressing v works fine, but all combinations with Ctrl, Alt or Shift did not work, even without actually checking any of the corresponding boolean values ..

What am I overlooking ..?
Wanted: Schrödingers Cat. Dead or alive.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

the problem is that you don't get a combination of the keys as an event...
you only get single key strokes...
a solution would be to define flags for the Alt, Ctrl and Shift keys, and if one of the keys is pressed set it to true (if the key is released set it to false)...
then if another key is pressed check if one of the flags is true... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Pan
Posts: 24
Joined: Tue Jan 20, 2009 9:36 am
Location: Bavaria, Germany

Post by Pan »

Hm, are you sure..?

What you are saying makes sense, but then I don't understand the API, which states:
bool Control: True if ctrl was also pressed.
For the irr::SEvent::SKeyInput member Control.

If only single events are taken into account, then this doesn't make much sense, since pressing ctrl-v would then (as you said) mean two seperate events.. and thus there'd be no need for the Control boolean at all?

Also, the seperate events solution doesn't explain why pressing ctrl-v doesn't even trigger the event which checks only the 'v' key, and ignores the state of the ctrl-key alltogether?

I don't get it :?
Wanted: Schrödingers Cat. Dead or alive.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Pan wrote:but then I don't understand the API, which states:
bool Control: True if ctrl was also pressed.
For the irr::SEvent::SKeyInput member Control.
oh, yes, you're right, it seems this flags were added to the struct now... :oops:

well, then the problem is that you check for the char...
if you (e.g.) press Shift + v, then the char is V (not v) and for STRG + v you'll get some sort of a horizontal bar...

so, instead of checking the char you should check for the key code:

Code: Select all

if( event.KeyInput.Key == KEY_KEY_V && event.KeyInput.PressedDown == false && event.KeyInput.Control){
                    ..do stuff..
}
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Pan
Posts: 24
Joined: Tue Jan 20, 2009 9:36 am
Location: Bavaria, Germany

Post by Pan »

That did it, excellent! Thanks for the help :D
Wanted: Schrödingers Cat. Dead or alive.
Post Reply