• 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.

P3D v3 SimConnect_MapInputEventToClientEvent in C# (Resolved)

Messages
1,243
Country
canada
I am trying to make the Throttle Control C++ example into a managed example in C#.

Almost got it except for:

Code:
simconnect.MapInputEventToClientEvent(INPUT_ID.INPUT_KEYS, "A", EVENT_ID.EVENT_A, (uint)1, EVENT_ID.EVENT_NOEVENT, (uint)1, false);

This give me an exception =1 UnKNOWN.

From the SDK

Code:
HRESULT SimConnect_MapInputEventToClientEvent(
  HANDLE  hSimConnect,
  SIMCONNECT_INPUT_GROUP_ID  GroupID,
  const char*  pszInputDefinition,
  SIMCONNECT_CLIENT_EVENT_ID  DownEventID,
  DWORD  DownValue = 0,
  SIMCONNECT_CLIENT_EVENT_ID  UpEventID =(SIMCONNECT_CLIENT_EVENT_ID)SIMCONNECT_UNUSED,
  DWORD  UpValue = 0,
  BOOL  bMaskable = FALSE
);

Parameters
hSimConnect
[in] Handle to a SimConnect object.
GroupID
[in] Specifies the ID of the client defined input group that the input event is to be added to.
pszInputDefinition
[in] Pointer to a null-terminated string containing the definition of the input events (keyboard keys, mouse or joystick events, for example). See the Remarks and example below for a range of possibilities.
DownEventID
[in] Specifies the ID of the down, and default, event. This is the client defined event that is triggered when the input event occurs. If only an up event is required, set this to SIMCONNECT_UNUSED.

DownValue
[in, optional] Specifies an optional numeric value, which will be returned when the down event occurs. This value is only acknowledged for keyboard and button events.
UpEventID
[in, optional] Specifies the ID of the up event. This is the client defined event that is triggered when the up action occurs.
UpValue
[in, optional] Specifies an optional numeric value, which will be returned when the up event occurs. This value is only acknowledged for keyboard and button events.
bMaskable
[in, optional] If set to true, specifies that the client will mask the event, and no other lower priority clients will receive it. The default is false.

The SDK documentation says to make the DownEventID should be SIMCONNECT_UNUSED and in the non managed world it's (EVENT_ID)SimConnect.SIMCONNECT_UNUSED. This gives me a overflow error. So I set an EVENT_ID.EVENT_NOEVENT as the first enum in my events (Seen this on the google).

Does anyone know how to use SimConnect_MapInputEventToClientEvent in a non-managed program?
 

ddawson

Resource contributor
Messages
862
Country
canada
Ron,

I think you're misreading the SDK. You only set the down event to SIMCONNECT_UNUSED if you are only interested in the up event. Typically you will watch for and act on the down event and ignore the up event.
Also, you have to call MapClientEventToSimEvent and AddClientEventToNotificationGroup before you can call MapInputEventToClientEvent.
I have a working example - I'll just have to put some comments in it... :oops:
 
Messages
1,243
Country
canada
Ron,

I think you're misreading the SDK. You only set the down event to SIMCONNECT_UNUSED if you are only interested in the up event. Typically you will watch for and act on the down event and ignore the up event.
Also, you have to call MapClientEventToSimEvent and AddClientEventToNotificationGroup before you can call MapInputEventToClientEvent.
I have a working example - I'll just have to put some comments in it... :oops:
Yes you're right Doug, I meant to say I used the SIMCONNECT_UNUSED in the Up Event as my code shows with EVENT_ID.EVENT_NOEVENT.

I am trying to stop the overflow error with UNUSED, but could not, so I set a (zero) EVENT_ID.NOEVENT, but that gives me a Exception=1 UNKNON error.
Thanks
 
Messages
2,077
Country
us-ohio
Ok... the declaration from the SDK is different than how your declaring that UpEvent parameter:
Code:
SDK:
(SIMCONNECT_CLIENT_EVENT_ID)SIMCONNECT_UNUSED
Note it's being cast as a SIMCONNECT_CLIENT_EVENT_ID. You're casting it as an EVENT_ID which I suspect is the problem.
 
Messages
1,243
Country
canada
Ok... the declaration from the SDK is different than how your declaring that UpEvent parameter:
Code:
SDK:
(SIMCONNECT_CLIENT_EVENT_ID)SIMCONNECT_UNUSED
Note it's being cast as a SIMCONNECT_CLIENT_EVENT_ID. You're casting it as an EVENT_ID which I suspect is the problem.

I agree, however the function in managed code is:

public void MapInputEventToClientEvent(Enum GroupID, string szInputDefinition, Enum DownEventID, uint DownValue, Enum UpEventID, uint UpValue, bool bMaskable);

So I'm thinking SIMCONNECT_CLIENT_EVENT_ID is an type Enum in C#

I have the events set up like this:

enum EVENT_ID
{
EVENT_NOEVENT,
EVENT_SIM_START,
EVENT_A,
EVENT_Z,
FLAPS_UP,
FLAPS_DOWN,
FLAPS_INC,
FLAPS_DEC,
PITOT_TOGGLE,
};

but this gives me the exception error =1 UNKNOWN. There is no SIMCONNECT_CLIENT_EVENT_ID type in P3D Managed lib.

This compiles but gives me an overflow exception when I run the program.

4294967295 this is too large for Uint32 which I find strange. 1 less than 2^32

simconnect.MapInputEventToClientEvent(INPUT_ID.INPUT_KEYS, "A", EVENT_ID.EVENT_A, (uint)1, (EVENT_ID)SimConnect.SIMCONNECT_UNUSED, (uint)1, false);

(enum)SimConnect.SIMCONNECT_UNUSED does not compile

(Enum)SimConnect.SIMCONNECT_UNUSED does not compile.
 
Last edited:
Messages
1,243
Country
canada
So trying to figure this out, I tried using the google to see what the size of a C# enum would be. It's 4 bytes. That seems reasonable and should be fine. What I took for granted was that an enum has an underlying type. After all it looks like an integer. Only one type.

One of the google results showed that you can change the underlying type that the enum is represented by. It's the size of a typical integer in most cases, but it can be a uint, a long, ulong, short, byte etc.

By changing my event enumerations to:

enum EVENT_ID :uint
{
EVENT_NOEVENT,
EVENT_SIM_START,
EVENT_A,
EVENT_Z,
FLAPS_UP,
FLAPS_DOWN,
FLAPS_INC,
FLAPS_DEC,
PITOT_TOGGLE,
};

Don't really need the EVENT_NOEVENT, enum anymore either.

This fixed the overflow error and no more exception = 1 unknown. The throttle C++ example has been reworked as a C# managed example.

simconnect.MapInputEventToClientEvent(INPUT_ID.INPUT_KEYS, "A", EVENT_ID.EVENT_A, (uint)1, (EVENT_ID)SimConnect.SIMCONNECT_UNUSED, (uint)1, false);
simconnect.MapInputEventToClientEvent(INPUT_ID.INPUT_KEYS, "Z", EVENT_ID.EVENT_Z, (uint)1, (EVENT_ID)SimConnect.SIMCONNECT_UNUSED, (uint)1, false);

I don't really like this solution, but it works. There must be some design/code model in the simconnect library that should work around this.
 
Top