(C++) Tell if multiple keyboard buttons are pressed

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
kompadre
Posts: 8
Joined: Tue Aug 08, 2006 9:56 pm
Contact:

(C++) Tell if multiple keyboard buttons are pressed

Post by kompadre »

Hi, I needed this feature and the simplest way to do it I came up with is to something like

Code: Select all

class MyEventReceiver : public irr::IEventReceiver
{
	// To store if a key is down, I use this private array
	bool isDown[irr::KEY_KEY_CODES_COUNT];
public: 
	// To make sure the keys are initialized to false I do 
	MyEventReceiver() 
	{
		memset(isDown, 0, sizeof(isDown));
	}
	virtual bool OnEvent(SEvent event)
	{
		if (event.EventType == EET_KEY_INPUT_EVENT)
			isDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		/* the rest of input parsing ... */
		return false;	
	}
	/* One last thing is to take care of is that some key codes 
		mean multiple key codes (i think): e.g. KEY_CONTROL means 
		KEY_LCONTROL and KEY_RCONTROL (the code for this buttons is always the same for me, tho).
		This would mean that another method with some logic is needed.
		Something like : 
	*/
	bool keyIsDown(enum irr::EKEY_CODE notbb_code) {
		switch (notbb_code) {
			case KEY_CONTROL:
				return (isDown[KEY_CONTROL] || isDown[KEY_LCONTROL] || isDown[KEY_RCONTROL]);
			/* here would go shifts, alts ... */
			default: 
				return isDown[notbb_code];
		}
	}
};
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

take a look at the sevent class...it's already integrated to check if control is pressed down.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply