7 Replies - 1608 Views - Last Post: 28 February 2013 - 03:14 PM Rate Topic: -----

#1 gurdmatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-February 13

sdl_getkeystates not working correctly

Posted 27 February 2013 - 11:35 PM

#include "Object.h"
#include "SDL.h"
#include <string>
using namespace std;

SDL_Event event;
Uint8 * keystates = SDL_GetKeyState(NULL);

int main(int argc, char *args[] ){
	bool quit = false;

	Object play1;
	
	

	while( quit != true ){
		
		if ( keystates[SDLK_LEFT] == SDL_PRESSED) {
		play1.xvelocity = 5;
			}
		else if (  keystates[SDLK_LEFT] == SDL_RELEASED) {
		play1.xvelocity = 0;
			}	

		

		
		play1.draw_char();
	
	}
	return 0;
}




When i press key down the player moves for one frame and then stops, and i have to continually keep pressing the key.
I have tried many diff options including the standard SDL_Event. nothing seems to work, when i have a key up event. I can change states fine when there all just key down. PLEASE HELP

Is This A Good Question/Topic? 0
  • +

Replies To: sdl_getkeystates not working correctly

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: sdl_getkeystates not working correctly

Posted 28 February 2013 - 04:08 AM

You are not doing any polling for events, in SDL you must do this. For get SDL_GetKeyState you can use SDL_PumpEvents and you must also continuously update your keystates using SDL_GetKeyState. Do this stuff in your loop.
Was This Post Helpful? 0
  • +
  • -

#3 gurdmatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-February 13

Re: sdl_getkeystates not working correctly

Posted 28 February 2013 - 09:15 AM

#include "Object.h"
#include "SDL.h"
#include <string>
using namespace std;




SDL_Event event;
Uint8 * keystates = SDL_GetKeyState(NULL);

int main(int argc, char *args[] ){
	bool quit = false;
	bool left = false;

	Object play1;
	
	SDL_Init(SDL_INIT_EVERYTHING);
	

	while( quit != true ){
		
		while( SDL_PollEvent  (&event)){
			if ( event.type == SDL_KEYDOWN){
				switch(event.key.keysym.sym){
					case SDLK_LEFT:
						(left = true);
						break;
					default:
						break;
				}
			
			}
			if ( event.type == SDL_KEYUP){
				switch(event.key.keysym.sym){
					case SDLK_LEFT:
						(left = false);
						break;
					default:
						break;
				}
			}
			}
		
		if (left == true ){
			std::cout << "true";
		}
		if (left == false ){
			std::cout << "false";
		}
		play1.draw_char();
	
	}
	return 0;
}



I went ahead and tried this as a test loop. If i press left it becomes true for one frame and then resets back to false right away. I have got to be missing something here, because i have implemented this same input structure on previous games without any problems like this.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: sdl_getkeystates not working correctly

Posted 28 February 2013 - 09:54 AM

Where is your window? What is in your Object.h?
Was This Post Helpful? 0
  • +
  • -

#5 gurdmatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-February 13

Re: sdl_getkeystates not working correctly

Posted 28 February 2013 - 10:34 AM

shouldnt matter i am just testing the input, no where in my loop do i call on anything from my object.
well except play1.drawchar() but that does not operate on the bool left.

this is strictly a console loop.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: sdl_getkeystates not working correctly

Posted 28 February 2013 - 02:40 PM

It does matter in fact. SDL will not just listen for every keyboard event on your computer, it will need your application to be the main focus. The main way to do this is to have created an SDL window first and make that the focus. Different operating systems will have their own specific ways of handling this, for example I just ran your code on OSX with the app as the main focus and it worked fine. If you are using windows then it may not allow this for various reasons.

Essentially you should always have some kind of window so that you can make it your main focus, in fact SDL requires you to have initialised the video subsystem before it will listen for events (that should tell you something).
Was This Post Helpful? 1
  • +
  • -

#7 gurdmatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-February 13

Re: sdl_getkeystates not working correctly

Posted 28 February 2013 - 02:45 PM

you are correct. I had just figured this out by searching around on another forum. I had my screen being initialized by my play.drawchar() method, which was a function in a seperate class instance. I believe what happened is the screen kept going out of scope.

I appreciate the helpful and accurate answer. Has been a real pain for me all day. Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: sdl_getkeystates not working correctly

Posted 28 February 2013 - 03:14 PM

No problem :) Good luck with your project!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1