PDA

View Full Version : catching double keyboard presses exception



04-16-2002, 12:45 AM
Hi,

When I press too fast on my keyboard, my OpenGL program crashes.

I think that my program registers that there are 2 keys being inputed at one time and therefore crashed.

How do I solve this problem???

Thanks.

My keyboard codes:

KeyPressed (...)
{
switch(case):
{1: rotateY -= 10.0f;
break;
default: break;
}
glutPostRedisplay();
}

martin_marinov
04-16-2002, 02:18 AM
Hi
try this:
KeyPressed()
{
static bool sbProcessingKey = false;
if (sbProcessingKey)
{
return;
}
sbProcessingKey = true;

////write here your keys handling code
sbProcessingKey = false;
}
If this doesn't help, I suggest you to debug your program more, and to find really where it crashes...

Regards
Martin

[This message has been edited by martin_marinov (edited 04-16-2002).]

04-16-2002, 07:48 AM
hi martin,

I do not understand why your code can solve my problem,

can you roughly explain?? thanks a lot!

04-16-2002, 07:50 AM
hi martin,

I do not understand why your code can solve my problem,

can you roughly explain?? thanks a lot!

04-16-2002, 07:51 AM
hi martin,

I do not understand why your code is able to solve my problem...

can you roughly explain???

Thanks a lot!

rts
04-16-2002, 04:43 PM
Martin's code, roughly speaking, prevents the function from re-entering itself.

Anyways, I doubt it will help. You need to post a lot more info if you want help. Like, where EXACTLY is it crashing? (Hint: you need a debugger to know this). What is the code doing there? etc etc etc

04-16-2002, 05:20 PM
My keypressed function:

void keyboard ( unsigned char key, int x, int y )
{
switch ( key ) {
case 'z':
scale += 10.0f;
break; // Ready For Next Case
default: // Now Wrap It Up
break;
}
}

I couldnt use my debugger on my program. Somehow it runs through my glut codes without going into the function.

However, my program have a tendency to crash whenever I pressed on 'z' too fast. I think the system registers double clicks instead and hence is unable to function normally.

Therefore, I am hoping that someone have caught this exception before and can help me.

will try out the codes martin posted =)

rts
04-16-2002, 08:36 PM
Originally posted by muzz:

I couldnt use my debugger on my program.



Uhm... why not?

$ gdb <name of program>
(gdb) run
... press 'z' a bunch of times and note where it crashes. Type 'bt' for a stack trace. Type 'list' for a listing of code.

You know how to use gdb, right?



Somehow it runs through my glut codes without going into the function.


What does this mean, exactly? How do you know this? If it doesn't go in to your function, how can your function be to blame?




However, my program have a tendency to crash whenever I pressed on 'z' too fast.


Again, you've given way too little info for anyone to give you any meaningful help.



I think the system registers double clicks instead and hence is unable to function normally.


Or it could be phase of the moon isn't quite right... seriously, anything is possible with the very little info that you've presented.




will try out the codes martin posted =)

Good luck.

martin_marinov
04-17-2002, 12:36 PM
Hi
muzz - try really to debug your program - my code may help (I doubt it will however), but it can hide something wrong somewhere in your code ...
Learn to use a debuger - if not gdb, then the internal debuger of kdevelop. When you compile your program just use the -g switch of gcc and so on.

We here really need more info to help you...

Regards
Martin

455h*L3
04-19-2002, 06:32 PM
hi martin,

I do not understand why your code is able to solve my problem...

can you roughly explain???

Thanks a lot!

martin_marinov
04-20-2002, 03:17 AM
Hi
this code, as rts explained, prevents the function of reentering - this means that it will be not able to process another key event, while you are processing the current one. This can happen if you are using threads, or somehow you are generating another key input while processing the current one. The last should _not_ happend if you don't have more than one thread and your code is correct.
As I said in my previous post, I doubt that my code will help. So learn to use a debuger - this is the best advice which I can give you...

Regards
Martin