Catching double keyboard presses exception

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();
}

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).]

hi martin,

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

can you roughly explain?? thanks a lot!

hi martin,

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

can you roughly explain?? thanks a lot!

hi martin,

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

can you roughly explain???

Thanks a lot!

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

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 =)

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.

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

hi martin,

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

can you roughly explain???

Thanks a lot!

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

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.