• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

register_key_event_handler

JB3DG

Resource contributor
Messages
1,325
Country
southafrica
Hi guys

Again using a modded ESP example....all the work being done here however is in the GaugeDeclarations.cpp.

First off, the code:

Code:
void FSAPI module_init(void);
void FSAPI module_deinit(void);
void EventHandler(ID32 event, UINT32 evdata, PVOID userdata);

eventsdata events;

void FSAPI module_init(void)
{
	register_key_event_handler((GAUGE_KEY_EVENT_HANDLER)EventHandler, NULL);
	// Initialize the Singleton class that holds the SimConnect data
	SimConnectData = CSimConnectData::Instance();
	SimConnectData->NavData = new NavData();
	SimConnectData->Populated = false;

	// Instantiate SimConnect
	hr = SimConnect_Open(&hSimConnect, ("//Not for you guys to know yet....."), NULL, 0, 0, 0);

	// Define a callback in this dll so that the simulation can be notified of SimConnect events
	if( hr == S_OK )
		hr = SimConnect_CallDispatch(hSimConnect, SampleGauge_DispatchProcDLL, NULL);
}

void FSAPI module_deinit(void)
{	
	unregister_key_event_handler((GAUGE_KEY_EVENT_HANDLER)EventHandler, NULL);
	// Dispose of SimConnect
	hr = SimConnect_Close(hSimConnect); 
};

void EventHandler(ID32 event, UINT32 evdata, PVOID userdata)
{
     switch(event)
    {
         case 0x11000:
			 if(events.batt == 1)
			 {
				events.batt --;
			 }
			 if(events.batt == 0)
			 {
				trigger_key_event(KEY_TOGGLE_MASTER_BATTERY, 0);
				events.batt += 2;
			 }
             break;
		 case 0x11001:
			 if(events.batt == 2)
			 {
				events.batt -= 2;
				trigger_key_event(KEY_TOGGLE_MASTER_BATTERY, 0);
			 }
			 if(events.batt == 0)
			 {
				 if(events.eng_master == true)
				 {
					trigger_key_event(KEY_TOGGLE_STARTER1, 0);
				 }
				 events.batt ++;
			 }
			 break;
		 case 0x11002:
			 events.eng_master = !events.eng_master;
			 trigger_key_event(KEY_TOGGLE_MASTER_IGNITION_SWITCH, 0);
			 break;
		 case 0x11003:
			 events.stop_start = !events.stop_start;
			 break;
         default:
             break;
    }
	 return;
}

Under the module_init/deinit functions are the register/unregister_key_event_handler functions. If I comment them out the gauge runs fine except the event system doesnt work so clicking on a switch does nothing. If i bring them back in however the sim crashes on load with a NULL pointer exception. So something here: ((GAUGE_KEY_EVENT_HANDLER)EventHandler

is not happening....and it just dont make sense. So...any ideas?

Another thing, I would really like to be able to solve such problems on my own but i have no idea on how to get a detailed output from the C++ debugger. All i get when the dll crashes is this:

First-chance exception at 0x00000000 in fsx.exe: 0xC0000005: Access violation.

So is there any way to output what functions/vars/objects or whatever are involved in the exception?
 
Last edited:
Ok after hunting around more i found the problem. The lack of FSAPI in front of EventHandler. So noobs out there the way around this is:

Code:
void FSAPI EventHandler(ID32 event, UINT32 evdata, PVOID userdata)

not

Code:
void EventHandler(ID32 event, UINT32 evdata, PVOID userdata)

Now a new problem. No more CTDs....but events are not triggering the L vars. I have made a few changes namely the variables being used are in a struct now instead of a class but still cant trigger the L vars that i registered in the panel system using register named variable. Code coming up shortly.
 
My struct under CSimconnectData.h

Code:
struct siEventsdata
{
	int batt;
	ID batt_id; 
	bool eng_master;
	ID eng_master_id;
	int timer;
	bool stop_start;
	ID stop_start_id;
};

In GaugeDeclarations.cpp i reference it here:

Code:
siEventsdata events;

then i use it in the event handler as can be seen....

Code:
switch(event)
    {
         case 0x11000:
			 if(events.batt == 1)
			 {
				events.batt --;
			 }
			 if(events.batt == 0)
			 {
				trigger_key_event(KEY_TOGGLE_MASTER_BATTERY, 0);
				events.batt += 2;
			 }
             break;
		 case 0x11001:
			 if(events.batt == 2)
			 {
				events.batt -= 2;
				trigger_key_event(KEY_TOGGLE_MASTER_BATTERY, 0);
			 }
			 if(events.batt == 0)
			 {
				 if(events.eng_master == true)
				 {
					trigger_key_event(KEY_TOGGLE_STARTER1, 0);
				 }
				 events.batt ++;
			 }
			 break;
		 case 0x11002:
			 events.eng_master = !events.eng_master;
			 trigger_key_event(KEY_TOGGLE_MASTER_IGNITION_SWITCH, 0);
			 break;
		 case 0x11003:
			 events.stop_start = !events.stop_start;
			 break;
         default:
             break;
    }

And then in the actual gauge.cpp i re-reference the struct

Code:
siEventsdata events_g;

and use it:

Code:
	case PANEL_SERVICE_PRE_INITIALIZE:		
	    register_named_variable ( "BATT" );
		register_named_variable ( "ENGINE_MASTER");
		register_named_variable ( "STOP_STARTER");
		break;
	case PANEL_SERVICE_PRE_UPDATE:
		// Called each time the gauge taken values are updated (18hz)	
		events_g.batt_id = check_named_variable ( "BATT" ) ;
		events_g.eng_master_id = check_named_variable ("ENGINE_MASTER");
		events_g.stop_start_id = check_named_variable ("STOP_STARTER");
	    set_named_variable_value (events_g.batt_id, (FLOAT64)events_g.batt ) ;
		set_named_variable_value (events_g.eng_master_id, (FLOAT64)events_g.eng_master);
		set_named_variable_value (events_g.stop_start_id, (FLOAT64)events_g.stop_start);
		break;

So...whats stopping them L vars from working?
 
Hi,

You declare:

siEventsdata events;

and

siEventsdata events_g;

I can't see the relationship between events and events_g. You are storing the event results in events but registering the variables in events_g, and both are different instantiations of siEventsdata.

Did you try doing:

siEventsdata events_g = events;


Tom
 
You're using two different variable names here - events in one block of code, and events_g in the other.
I suspect you want a single, global declaration,

Doug
 
"edited"

Ok...the problem is im handling this between 2 different cpp files that are not linked. if i #include the one into the other ill wreak havoc in the linker. So....how do i get this global declaration? use a class that references the struct?
 
Last edited:
Or is my understanding a little flawed and by creating the events_g sub-struct im creating a new set of vars based off the main struct?

Yes, tha't exactly what you're doing.
As Doug stated, would be preferred to have only one global declaration, for example events, and use that on both functions.

Tom
 
Love how the learning goes around here.....So....tip on how to declare it globally?
 
"edited"

Ok...the problem is im handling this between 2 different cpp files that are not linked. if i #include the one into the other ill wreak havoc in the linker. So....how do i get this global declaration? use a class that references the struct?

Well, I don't know the reasons you have for not being able to link both ccp. Anyway, if that's your desire, declare events in the "main" cpp and in the other declare

extern siEventsdata events;

now you can use events as the same struct in both functions

Tom
 
now i know what extern does THANKS A TON!! :D i swear one of these days im going to write a book on this....Dai Griffiths' book is good but a tad dated and doesnt explain the how/why to the noob....
 
Love how the learning goes around here.....So....tip on how to declare it globally?

Ok let's see a typical .cpp file

Code:
[FONT="Courier New"]// MyCPP.cpp 

#include (.h) file 
etc

#define TOKEN  
etc[/FONT]

//[B]Global vars go here example

[FONT="Courier New"]FLOAT64 fVar1;
int         fVa2 = 0;

struct siEventsdata
{
	int batt;
	ID batt_id; 
	bool eng_master;
	ID eng_master_id;
	int timer;
	bool stop_start;
	ID stop_start_id;
} events;

[/FONT]
etc;
[/B]

[FONT="Courier New"]#include (.cpp) another .cpp
etc,

//First function and rest of this .cpp[/FONT]

Hope this helps

Tom
 
Back
Top