API Question Check if key is pressed

PeriapsisPrograde

Wannabe addon dev
Joined
Mar 29, 2011
Messages
406
Reaction score
0
Points
16
Location
In orbit
My searches have yielded nothing on this topic;

Is there any inline function that returns wether a key is pressed? In a vessel I am working on, it would be nice to be able to do
Code:
MyVessel::clbkPostStep () {
//
if (KeyPressed ('a') {functions}
else ;
//
}
or similar? Looking through the API doc in the SDK doesn't appear to have what I want.

Thanks! :tiphat:
 

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
Not sure what that accomplishes that clbkConsumeBufferedKey() can't.
Code:
clbkConsumeBufferedKey(...)
{
if(!down)
 return 0;
switch(key)
{
  case OAPI_KEY_A:
    functions;
    return 1;
    break;
    ...
}
}
Perhaps if you told us a bit more about why you want it in poststep, we could provide a bit more direction.
 

PeriapsisPrograde

Wannabe addon dev
Joined
Mar 29, 2011
Messages
406
Reaction score
0
Points
16
Location
In orbit
Before I do this wrong...

Code:
int UGH::clbkConsumeBufferedKey (DWORD a, bool down, char kstate) {
	if (!down)
		return 0;
	switch (a) {
		case OAPI_KEY_A:
			armed=!armed;
			if (fired==true) fired = false;
			break;
		case OAPI_KEY_F:
			fired=!fired;
			break;
		default:
			return 0;
	}
}

This code would:
- If no keys are pressed, return 0;
- If A is pressed, switch bool armed and fired = false;
- If F is pressed, switch bool fired;
- Otherwise return 0;

Am I right?
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,290
Reaction score
3,258
Points
203
Location
Toulouse
I usually do it that way. Not tested this one sample, though.

Code:
int UGH::clbkConsumeBufferedKey (DWORD a, bool down, char kstate) 
{

	if (!down)
		return 0;

	switch (a) 
        {
		case OAPI_KEY_A:
			armed=!armed;
			if (fired==true) fired = false; // beware there, use { }
			return 1;

		case OAPI_KEY_F:
			fired=!fired;
			return 1;

        }
	return 0;
}
 

PeriapsisPrograde

Wannabe addon dev
Joined
Mar 29, 2011
Messages
406
Reaction score
0
Points
16
Location
In orbit
Thanks guys! :tiphat:

Alright... so...
Code:
//
//
void UGH::clbkPostStep (double simt, double simdt) {
	int clbkConsumeBufferedKey (DWORD a, bool down, char kstate);
	// Move hook if necessary -- to do list
	// Grapple				  -- to do list
	// Move UGH				  -- to do list
	InsertMesh ("UGH_hook", 3, &hookpos);
}
//
//
int UGH::clbkConsumeBufferedKey (DWORD a, bool down, char kstate) 
{
	if (!down)
		return 0;
	switch (a) 
        {
		case OAPI_KEY_A:
			armed=!armed;
			if (fired==true) {fired = false;}			return 1;
		case OAPI_KEY_F:
			fired=!fired;
	return 1;
		case OAPI_KEY_G:
	return 2;
        }
	return 0;
}
would cause it to check key presses and switch armed and fired statuses every time step, correct?
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,290
Reaction score
3,258
Points
203
Location
Toulouse
I'm not sure about the way you use the "return" instruction, but I'm no expert.

Yes, it updates every frame, else yes, it should serve your purpose. Don't be afraid to try ;)
 

Glider

Addon Developer
Addon Developer
Joined
Apr 12, 2008
Messages
226
Reaction score
0
Points
16
Location
Saint-Petersburg
Thanks guys! :tiphat:

Alright... so...

would cause it to check key presses and switch armed and fired statuses every time step, correct?
Not every time step (clbkConsumeDirectKey( char *kstate ) does it every time step), clbkConsumeBufferedKey will be called only after user pressed a key or released it. It should return 0 or 1. 1 means that Orbiter will skip default processing of that key, 0 will allow it. Function prototype shouldn't be in clbkPostStep, it should be in class header. You shouldn't use F key. Orbiter already uses it for FPS counter.

Also, are you sure that you want to place InsertMesh() in clbkPostStep() ? It will cause insertion of a mesh at every frame...

Corrected code:
Code:
//UGH.h:
class UGH {
public:
....
int clbkConsumeBufferedKey (DWORD a, bool down, char *kstate);
....
};

//UGH.cpp
int UGH::clbkConsumeBufferedKey( DWORD a, bool down, char *kstate ) 
{
	if (!down)
		return 0;
	switch (a) 
        {
		case OAPI_KEY_A:
			armed = !armed;
			if (fired==true) {
				fired = false;
			}
			return 1;
		case OAPI_KEY_F:
			fired=!fired;
			return 1;
		case OAPI_KEY_G:
			return 1;
        }
	return 0;
}
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
You shouldn't use F key. Orbiter already uses it for FPS counter.
Just a side note, if it's released for the next version of Orbiter. Keys "I" and "F" are no longer used starting from Orbiter 110822.
 
Top