Sign in to follow this  
Dragon_Strike

proper winapi input

Recommended Posts

Dragon_Strike    264
im trying to write a console for my 3d engine... however im havin some trouble gettin proper input from the winapi... first of all the characters are in high case and shift+ doesnt work etc... i was wondering how id get it working properly like the windows console.... and also as it is now if i write fast.. some input is ignored... here is how i handle keyboard input
struct KeyboardEventArgs : public base::EventArgs
{
    UINT nChar;
    bool bKeyDown; 
    bool bAltDown;

	bool bKeys[256];
   
    bool KeyPressed(unsigned char key) { return bKeys[key]; }
};
typedef boost::shared_ptr<KeyboardEventArgs> KeyboardEventArgsPtr;


class KeyboardEventHandler : public base::IEventHandler
{  
	typedef boost::function<void (Window*, KeyboardEventArgs*)> FunctionType;
public:
	template <class Object>
	KeyboardEventHandler(void (Object::*m)(Window*, KeyboardEventArgs*), Object* o): func_(boost::bind(m, o, _1, _2)){}

	virtual void operator()(const boost::any& sender, base::EventArgs* e)
	{
		EventArgs* wine =  static_cast<EventArgs*>(e);		

		keyboard_.bKeyDown = (wine->message == WM_KEYDOWN || wine->message == WM_SYSKEYDOWN);
		DWORD dwMask = (1 << 29);
		keyboard_.bAltDown = ( (wine->lParam & dwMask) != 0 );

		keyboard_.bKeys[ (BYTE) (wine->wParam & 0xFF) ] = keyboard_.bKeyDown;  

		keyboard_.nChar = (BYTE) (wine->wParam & 0xFF);


		func_(boost::any_cast<Window*>(sender), &keyboard_);
	}
private:
	FunctionType func_;
	KeyboardEventArgs keyboard_;
};



console class so far..
class Console
{

public:
    Console()
	{
		old_buffer = std::wcout.rdbuf(output.rdbuf());

	}

	virtual ~Console()
	{
		std::wcout.rdbuf(old_buffer);
	}

	std::wstring GetOutput()
	{
		return output.str();
	}


	void Proc(win32::Window* sender, win32::KeyboardEventArgs* e)
	{
		static bool down = true;
		char key = (char)e->nChar;

		if(down)
		{
			down = false;
			 std::wcout << key;
		}	 
		else
			down = !e->bKeyDown;
	}

protected:

	std::wstreambuf* old_buffer;
	std::wstringstream  output;
};

typedef boost::shared_ptr<Console> ConsolePtr;


the keyboardevent handler calls the console::Proc on WM_KEYDOWN, WM_KEYU, WM_SYSKEYDOWN, WM_SYSKEYUP

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by Dragon_Strike
i was wondering how id get it working properly like the windows console.... and also as it is now if i write fast.. some input is ignored...
As SiCrane said, just use WM_CHAR - that's exactly what it's for.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this