Using Modifier keys

From C4 Engine Wiki

Jump to: navigation, search

Modifier Keys

A modifier key is one of the few keys that typically change the way a keypress behaves during normal Operating system use. These keys are generally Control (left + right), Alt (left + right), and Shift (left + right).


The Code

I wrote this with the SimpleChar project, so I will be telling you to add code in the same respect to where similar code is located, however you do not need to follow this scheme. First things first, change your Action enumeration to look similar to the following:


        enum
	{
		kActionForward			= 'frwd',
		kActionBackward			= 'bkwd',
		kActionLeft			= 'left',
		kActionRight			= 'rght',
		kActionUp			= 'jump',
		kActionDown			= 'down',
                //---------------
                //Start -- Neonic
                //---------------
		kActionShift			= 'lsft',
		kActionRShift			= 'rsft',
		kActionControl			= 'lctl',
		kActionRControl			= 'rctl',

                //-------------
                //End -- Neonic
                //-------------
	};


Ok, so in your SimpleChar.h file add the following code after the MovementAction:


        //---------------
	//Start -- Neonic
	//---------------

	class SpecialAction : public Action
	{			
		public:
			
			SpecialAction(unsigned long type);
			~SpecialAction();
			
			void Begin(void);
			void End(void);
	};


        //--------------
	//End -- Neonic
	//--------------

Then add this to your Game.cpp file:


//----------------
//Start -- Neonic
//----------------

SpecialAction::SpecialAction(unsigned long type) : Action(type)
{
}

SpecialAction::~SpecialAction()
{
}

void SpecialAction::Begin(void)
{
	switch(GetActionType())
	{
		case kActionShift:
			TheGame->LeftShift(true);
			break;
		case kActionRShift:
			TheGame->RightShift(true);
			break;
		case kActionControl:
			TheGame->LeftControl(true);
			break;
		case kActionRControl:
			TheGame->RightControl(true);
			break;
	}
}
void SpecialAction::End(void)
{
	switch(GetActionType())
	{
		case kActionShift:
			TheGame->LeftShift(false);
			break;
		case kActionRShift:
			TheGame->RightShift(false);
			break;
		case kActionControl:
			TheGame->LeftControl(false);
			break;
		case kActionRControl:
			TheGame->RightControl(false);
			break;
	}
}

//--------------
//End -- Neonic
//--------------

Then, make sure you add the bindings to the input.cfg file, like so:


bind "Left Shift" %rsft;
bind "Right Shift" %rsft;
bind "Left Control" %lctl;
bind "Right Control" %rctl;

Add the following code to your Game Class definition:

Class Game ...
{
                        private:

                        ...


                        //---------------
			//Start -- Neonic
			//---------------

			SpecialAction					*lShiftAction;
			SpecialAction					*rShiftAction;
			SpecialAction					*lControlAction;
			SpecialAction					*rControlAction;

			bool							lShift;
			bool							rShift;
			bool							lControl;
			bool							rControl;

			//--------------
			//End -- Neonic
			//--------------

                        ...

                        public:

                        ...

                        //---------------
			//Start -- Neonic
			//---------------
			
			bool LeftShift() { return lShift; }
			bool LeftShift(bool pressed) { lShift = pressed; return lShift;}

			bool RightShift() { return rShift; }
			bool RightShift(bool pressed) { rShift = pressed; return rShift;}

			bool LeftControl() { return lControl; }
			bool LeftControl(bool pressed) { lControl = pressed; return lControl;}

			bool RightControl() { return rControl; }
			bool RightControl(bool pressed) { rControl = pressed; return rControl;}
                        //-------------
			//End -- Neonic
			//-------------
};


Then add the following to your Game class's constructor:

	//----------------
	//Start -- Neonic
	//----------------

	lShiftAction = new SpecialAction(kActionShift);
	rShiftAction = new SpecialAction(kActionRShift);
	lControlAction = new SpecialAction(kActionControl);
	rControlAction = new SpecialAction(kActionRControl);

	TheInputMgr->AddAction(lShiftAction);
	TheInputMgr->AddAction(rShiftAction);
	TheInputMgr->AddAction(lControlAction);
	TheInputMgr->AddAction(rControlAction);

        lShift = rShift = lControl = rControl = false;

	//--------------
	//End -- Neonic
	//--------------

Conclusion

That should just about do it. I haven't tested this code ONE Bit. It is still under construction, but do to the migration of my compiler from VS2005 to VS2008, I am not able/willing to test the project against C4 right now. If you would be so kind, if you do run this and notice any errors, either let me know on the forums or go ahead and fix them yourself.

The way that you would use this code is simple. In your, let's say, MovementAction::Begin code, you can call if(TheGame->LeftShift()) walkSpeed = 1.5; or something similar.

That is it for how you detect "Modifier keys."

Personal tools