Skip to main content
GameDev.net gamedev.net
🔒 Locked

Key combination events in SDL

Started by Nahrix Mar 5, 2005 at 12:02 PM 4 replies 5.7k views
Original Post
Nahrix
Nahrix
I'm planning on implementing undo and redo functions in my program, and I need to use the key combinations ctrl+z and ctrl+y, respectively. How do I check events for key combinations in SDL? Edit: To clarify, I'm using SDL_PollEvent to capture an event, and using a switch stucture to implement each event type. In this case, I'm capturing SDL_KEYDOWN, and using if (e_Event.key.keysym.sym == keyname) [Edited by - Nahrix on March 5, 2005 12:20:46 PM]
Thujalvi
Thujalvi
Maybe you could use logic operator && ? I'm not sure tho.
v0dKA
v0dKA
Sorry, I can't help you too much here since I never touched SDL. However, if my assumptions are right, you shouldn't be checking keypresses using SDL but with regular Windows API.

You could use the GetAsyncKeyState() function. Its advantage over WM_KEYPRESS is that you could use it anywhere; you're not restricted to the message loop. You could use the function in a if statement, also using the logical and, &&.

There probably is a more "standard" way to do this, so I'm sorry if this doesn't help.
.:-v0d[KA]-:. <<>>
thec
thec
For my recent project I wrapped SDL's event class to my own, and since you get keydowns&ups for ctrl/alt etc I just stored those seperatly and added to the regular events, so I can detect ctrl+mouse_left_click for instance...

I haven't tried any other way, but consult the manual if you want to know more...

Albert "thec" Sandberg
-------------------------------------------http://www.thec.org
Nahrix
Nahrix
Quote:
Original post by Thujalvi
Maybe you could use logic operator && ? I'm not sure tho.


I think SDL_PollEvent only generates one event at a time, though.
Drew_Benton
Drew_Benton
Going on with my older post here on a better input for SDL (IMO), I made a few changes to show you how this can be done to give you some ideas. Feel free to use this though if you don't wanna make mods [wink]

#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>#pragma comment (lib, "SDL.lib")#pragma comment (lib, "SDLmain.lib")#include <vector>using namespace std;vector <Uint8> keydown_list;vector <Uint8> donotrelease_list;int main( int argc, char *argv[] ){	keydown_list.clear();	donotrelease_list.clear();	keydown_list.resize(0xFFFF);	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )	{		printf("Unable to init SDL: %s\n", SDL_GetError());		exit(1);	}	atexit(SDL_Quit);	SDL_Surface *screen;	SDL_EnableUNICODE(1);		screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);		if ( screen == NULL )	{		printf("Unable to set 640x480 video: %s\n", SDL_GetError());		exit(1);	}	int done=0;	while(done == 0)	{		SDL_Event event;		while ( SDL_PollEvent(&event) )		{			if ( event.type == SDL_QUIT )  			{  				done = 1;  			}			if ( event.type == SDL_KEYDOWN ) // keydown			{				keydown_list[ event.key.keysym.unicode ] = 1;				donotrelease_list.push_back(event.key.keysym.unicode);                // If you want to use ctrl,alt,shift				if( !event.key.keysym.unicode )				{					//printf( "UNI: %i\n", event.key.keysym.unicode );					//printf( "MOD: %i\n\n", event.key.keysym.mod );					//printf( "SYM: %i\n", event.key.keysym.sym );					/*if( event.key.keysym.sym == 306 )					{						printf("CTRL pressed");					}*/				}			}			int ind = 0;			int current = 0;			if( donotrelease_list.size() > 0 )			{				current = donotrelease_list.at(ind);				for(int x=0;x<0xFFFF;x++)				{					while(x < current)					{						keydown_list[x] = 0;						x++;					}					ind++;					if(ind >= donotrelease_list.size())						current = 0xFFFF;				}				donotrelease_list.clear();			}			else			{				for(int x=0;x<0xFFFF;x++)				{					keydown_list[x] = 0;				}			}		}// ctrl + any char gives some number in the range of 1-26, 'A' being 1, 26 being 'Z'. That is the premise of this		for( int x=1;x<27;x++)		{			if(keydown_list[x])			{				printf("ctrl + %c\n",x+64);				// added this so ctrl+ char can only be pressed once				keydown_list[x] = 0;			}		}		SDL_Flip(screen);	}	return 0;}


I'll let you have a look for yourself. If you have any questions about the code posted feel free to ask, I'll try to get back as soon as possible. I did make a full explanation at the bottom of this post as well if you need some additional commentary. When you run it, hit ctrl + a character to see the output.

- Drew

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.