LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2008, 04:00 PM   #1
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Rep: Reputation: 15
Duplicate SDL event issue


Hi folks,
I have an issue with a program I'm writing. I'm using SDL to capture keyboard events. When I press an arrow key (Left, Right, UP or Down), the program works just as it should. The problem arises when I press a second button. The first key pressed seems to remain in the queue and executes again and is immediately followed by the second command.

For example:
If I press the UP arrow, it moves an object up...then if I press the left arrow, it moves the object up and then to the left.

Does anyone know what might cause this?

- Andrew
 
Old 01-02-2008, 06:26 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Hmm is one of them a keydown followed by a keyup, or maybe its a key repeat as the button is still pressed. Would you post your event loop?
 
Old 01-02-2008, 08:14 PM   #3
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
Main Loop Event (Derivative)

I thought of that...so I made only the SDL_KEYUP have any action taken.

Code:
/*
#include //  blah....blah....blah
*/

int main()
{	// declarations, video, SDL_INIT //
	// blah...blah...blah...         //


	// MAIN LOOP EVENT //
	while(SDL_PollEvent(&event))
	{	switch(event.type)
		{	case SDL_KEYDOWN:
				break;
			case SDL_KEYUP:
				DoSomething(&event.key);
				break;
			case SDL_QUIT:
				break;
		}
	}

	// blah....blah....blah...       //
	return(0);
}


void DoSomething(SDL_KeyboardEvent *key)
{
	switch(key->keysym.sym)
	{       case SDLK_LEFT:
			printf("LEFT   KEY PRESSED.\n", stdout);
			fflush(stdout);
			break;
		case SDLK_RIGHT:
			printf("RIGHT  KEY PRESSED.\n", stdout);
			fflush(stdout);
			break;
		case SDLK_UP:
			printf("UP     KEY PRESSED.\n", stdout);
			fflush(stdout);
			break;
		case SDLK_DOWN:				   
			printf("DOWN   KEY PRESSED.\n", stdout);      
			fflush(stdout);
			break;
		case SDLK_ESCAPE:
			printf("ESCAPE KEY PRESSED.\n", stdout);
			fflush(stdout);
			break;
		default:
			break;
	}
}
 
Old 01-03-2008, 04:46 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I did suspect that you were falling through the case statement yet this seems not to be the case(is this the actual code? ) and I am unsure of why this is happening; but I have to say I think you are handling key presses wrongly. Only detecting a key release is a strange method to use.
is this for a game?
which version of SDL are you using
which operating system?
How old is the keyboard you are using?
 
Old 01-03-2008, 11:25 AM   #5
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
Yeah. This is for a game. It does the same thing under Fedora Core 6 and 7 using SDL-1.2.11-1.fc6 and SDL-1.2.12-1.fc7 respectively. My keyboard is a standard keyboard.

You mentioned that only using the key release is strange...but what is the standard method. I did this to try and resolve the double action issue, but it didn't resolve it.

Thanks,
Andrew
 
Old 01-04-2008, 02:15 PM   #6
Fidori
LQ Newbie
 
Registered: Oct 2007
Location: Finland
Distribution: Slackware
Posts: 27

Rep: Reputation: 17
Is the while loop inside another loop? This is working okay for me on Ubuntu 7.10:

Code:
int main(int argc, char *argv[])
{
        SDL_Event event;

        if (SDL_Init(SDL_INIT_VIDEO) != 0
            || SDL_SetVideoMode(500, 500, 0, SDL_ANYFORMAT) == NULL) {
                fprintf(stderr, "error: %s\n", SDL_GetError());
                return EXIT_FAILURE;
        }

        for (;;) {
                while (SDL_PollEvent(&event)) {
                        switch (event.type) {
                        case SDL_KEYUP:
                                printf("key up: %d\n", event.key.keysym.sym);
                                break;
                        case SDL_QUIT:
                                SDL_Quit();
                                return EXIT_SUCCESS;
                        default:
                                break;
                        }
                }
        }
}
 
Old 01-04-2008, 02:35 PM   #7
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
Hmmm..... Actually it is inside another loop. I have a while inside a while. That was done to solve another issue with closing the program without error. I'll see what happens... Thanks. -Andrew
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fire Event from other event in Visual C++ Express Edition chrisliando Programming 1 11-08-2007 05:12 PM
NFS: duplicate lisintg issue Fedora007 Linux - Software 3 09-26-2005 08:11 PM
$PATH duplicate set issue apocolpse Linux - Newbie 4 06-11-2005 05:35 AM
duplicate gpg key issue Blackrose1 Linux - General 2 03-22-2004 09:33 AM
Duplicate on Laptop Forum SuSE Network Issue Kevin Edmonds Linux - Distributions 0 07-19-2003 10:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration