Click here to Skip to main content
15,891,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want use SDL input without create window, ( I want to run my program in the background )

for example at first I want close my program. but if it doesn't have the window, it can't read the keyboard

C++
#include <Windows.h>
#include <stdlib.h>
#include "Input.h"
#ifndef __SDL_H_I__
	#define __SDL_H_I__
	#include "SDL.h"
#endif
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
	SDL_Init(SDL_INIT_EVERYTHING);
	Input::Init();
	while(1)
	{
		Input::Update();
		if(Input::GetKey(SDLKey::SDLK_u))
		{
			SDL_Quit();
			return 0;
		}
	}
	return 0;
}


C++
#ifndef __INPUT_H__
#define __INPUT_H__
#ifndef __SDL_H_I__
	#define __SDL_H_I__
	#include "SDL.h"
#endif

class Input
{
public:
	static void Init();
	static void Update();
	static bool GetKey(SDLKey key);
	static bool GetKeyDown(SDLKey key);
	static bool GetKeyUp(SDLKey key);
	static bool GetButton(int btn);
	static bool GetButtonDown(int btn);
	static bool GetButtonUp(int btn);
private:
	static bool _curKey[322];
	static bool _preKey[322];
	static bool _curBtn[3];
	static bool _preBtn[3];
	static SDL_Event _event;
};

#endif


C++
#include "Input.h"

bool Input::_curKey[322];
bool Input::_preKey[322];
bool Input::_curBtn[3];
bool Input::_preBtn[3];
SDL_Event Input::_event;

void Input::Init()
{
    memset(_curKey,0,sizeof(bool)*322);
    memset(_preKey,0,sizeof(bool)*322);
    memset(_curBtn,0,sizeof(bool)*322);
    memset(_preBtn,0,sizeof(bool)*322);
}

void Input::Update()
{
    memcpy(_preKey,_curKey,sizeof(bool)*322);
    memcpy(_preBtn,_curBtn,sizeof(bool)*3);

    while(SDL_PollEvent(&_event))
    {
        switch(_event.type)
        {
        case SDL_KEYDOWN:
            {
                _curKey[_event.key.keysym.sym] = true;
                break;
            }
        case SDL_KEYUP:
            {
                _curKey[_event.key.keysym.sym] = false;
                break;
            }
        case SDL_MOUSEBUTTONDOWN:
            {
                _curBtn[_event.button.button]=true;
                break;
            }
        case SDL_MOUSEBUTTONUP:
            {
                _curBtn[_event.button.button]=false;
                break;
            }
        }
    }
}

bool Input::GetKey(SDLKey key)
{
    return _curKey[key];
}
bool Input::GetKeyDown(SDLKey key)
{
    return _curKey[key] && !_preKey[key];
}
bool Input::GetKeyUp(SDLKey key)
{
    return !_curKey[key] && _preKey[key];
}

bool Input::GetButton( int btn )
{
    return _curBtn[btn];
}

bool Input::GetButtonDown( int btn )
{
    return _curBtn[btn]==true && _curBtn[btn]==false;
}

bool Input::GetButtonUp( int btn )
{
    return _curBtn[btn]==false && _curBtn[btn]==true;
}
Posted

1 solution

You could consider a compromise, have a Window if SDL needs one but make it invisible. Every Windows Window has a style setting which determines if it's visible e.g. WS_VISIBLE in MFC. SDL will have the equivalent.
However if you need to be getting keyboard input then your Window should not be invisible or running in the background because it is interacting with the user. These two requirments would seem to be in contradiction.
 
Share this answer
 
Comments
mohammadali1375 6-Jun-13 14:39pm    
Thanks but in my try I see you should select the sdl window to read Inputs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900