Skip to main content
gamedev.net
🔒 Locked

SDL Keyboard Input

Started by WilyCoder 7 replies 1.2k views
Original Post
WilyCoder
WilyCoder
I'm new to SDL and I have a quick question for everyone. I think its best if I just post the code :

Uint8 *Keys;
/*main loop portion*/
Keys = SDL_GetKeyState(NULL);

if(Keys[SDLK_RETURN])
 MessageBox(NULL, "Enter", "Enter", MB_OK,MB_ICONEXCLAMATION);


When I press enter, nothing happens. Any ideas?
Deranged
Deranged
Hola, please delete your other (empty) post.

Also MessageBox(); is part of the win32 library, so you CANNOT use it correctly with sdl, in my experiance.
(I may be wrong becasue i outed win32 a long time ago)
Drew_Benton
Drew_Benton
I agree that when you use SDL, you shouldn't throw in Win32..., but you can get it to work. WilyCoder, something is not right with your input system.

You will need to call:
 Keys = SDL_GetKeyState(NULL); 
Each frame in your update loop. After that, you should be able to see a messagebox when the return is pressed. In my sig, I have a link, starting with SDL. I just added:
// If we press escapeif( g_keys[ SDLK_RETURN ] ){	MessageBox( 0, "Enter", "Enter", MB_OK | MB_ICONEXCLAMATION );}
To it and I saw the messagebox just fine, so I know it works. Give that a try and see if you can get it, if not, take a look at my little tutorial or some other tutorial on the net to compare your update loops.

Here is the modded code:
// Include our SDL header file#include <SDL/SDL.h>#include <windows.h>// Prototypes for the functions we will usebool Init();void ProcessEvents( SDL_Event &event );void Update();void Draw();void DeInit();// Variable to tell when our game is doneint g_done = 0;// Variable holding all of the states of the keysUint8 *g_keys;int main( int argc, char *argv[] ){	// First thing to do is initialze SDL	if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )	{		printf("Unable to init SDL: %s\n", SDL_GetError());		return -1;	}	// Now we need to tell our program to execute SDL_Quit() at exit	atexit(SDL_Quit);	// Now we call our init function and return if it fails	if( !Init() )		return -1;	// While we want to run	while( g_done == 0 )	{		SDL_Event event;		while ( SDL_PollEvent( &event ) )		{			// Process any events			ProcessEvents( event );		}		// Update and draw 		Update();		Draw();	}	// Deinitialize the game	DeInit();	return 0;}bool Init(){	// Set the video mode, we will use a 640x480x32 screen 	// with double buffering and a request to run in hardware mode	SDL_SetVideoMode( 640,480,32, SDL_HWSURFACE|SDL_DOUBLEBUF );	// Test to see if the display was created	if ( SDL_GetVideoSurface() == NULL )	{		printf( "Unable to set 640x480 video: %s\n", SDL_GetError() );		return 0;	}	// Return success!	return 1;}void ProcessEvents( SDL_Event &event ){	// If it is a quit event	if ( event.type == SDL_QUIT )	{		g_done = 1;	}}void Update(){	// Update the state of the keys	g_keys = SDL_GetKeyState( 0 );	// If we press escape	if( g_keys[ SDLK_ESCAPE ] )	{		g_done = 1;	}	// If we press escape	if( g_keys[ SDLK_RETURN ] )	{		MessageBox( 0, "Enter", "Enter", MB_OK | MB_ICONEXCLAMATION );	}}void Draw(){}void DeInit(){}


Also note that this works only in windowed mode. If you go FS, there is no gaurntee that you will be able to see the box above your draw window, so keep that in mind

- Drew
WilyCoder
WilyCoder
^^^ Thanks, I forgot to mention that I am using SDL only for input handling. All my rendering code is straight OpenGL...

Hence, I am not calling InitSubsystem() or anything like that. Also, I am not processing any SDL events.
Deranged
Deranged
Quote:
Original post by WilyCoder
^^^ Thanks, I forgot to mention that I am using SDL only for input handling. All my rendering code is straight OpenGL...

Hence, I am not calling InitSubsystem() or anything like that. Also, I am not processing any SDL events.


Technically you are, becauseInput "is" an sdl event.
WilyCoder
WilyCoder
^^^ Correction: I am not explicitly handling any SDL events, such as:
while ( SDL_PollEvent( &event ) )		{			// Process any events			ProcessEvents( event );		}


Do you think that's the problem?
Kylotan
Kylotan
The docs say what you must do with events to use that function.

Pipo DeClown
Pipo DeClown
I fixed MessageBox by putting this after SDL_SetVideoMode():

while(SDL_PollEvent(&event)) ProcessEvents(event);

Topic Locked

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

Sign in to reply to this topic.