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

How to detect joystick button press

Messages
120
Country
unitedstates
I would like to press a joystick button and have my app detect that event so it can then act accordingly. How do I set up my code to detect when I've pressed one of my joystick buttons?

Thanks...
 
Messages
1,244
Country
canada
Ralph,

You'll have to add the INPUT stuff back in. See the SDK joystick example for C++ code.

add in another INPUTGroup - leave your notification group

Code:
        enum NOTIFICATION_GROUPS
        {
            GROUP0,
        }

        enum INPUT_GROUPS
        {
            INPUT0,
        }

add the joystick button input

Code:
                simconnect.AddClientEventToNotificationGroup(INPUT_GROUPS.INPUT0, EVENTS.CAMERA_RESET, false);

                // Map the keys , and . keys to the private events
                simconnect.MapInputEventToClientEvent(INPUT_GROUPS.INPUT0, "joystick:0:button:0", EVENTS.CAMERA_RESET, (uint)0, (EVENTS)unused, (uint)0, (bool)false);

you will need the STATE.ON

Code:
                simconnect.SetInputGroupState(INPUT_GROUPS.INPUT0, (uint)SIMCONNECT_STATE.ON);

And you will need the received event trigger - I used resetcamera event in this example.

Code:
                case (uint)EVENTS.CAMERA_RESET:
                    resetview();
                    break;
 
Messages
120
Country
unitedstates
I understand this line will map joystick button #11 press to the camera reset event, correct?

simconnect.MapInputEventToClientEvent(INPUT_GROUPS.INPUT0, "joystick:0:button:11", EVENTS.CAMERA_RESET, (uint)0, (EVENTS)unused, (uint)0, (bool)false);
 
Messages
120
Country
unitedstates
I had to remm out the code in the camera_reset event because it was firing every sec or so.
I thought this event would only fire when I pressed joystick #11 button?

void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT recEvent)
{
switch (recEvent.uEventID)
{
case (uint)EVENTS.CAMERA_RESET:
//cameraDollyLeftRight = 0.0f;
//cameraDollyUpDown = 0.0f;
//cameraDollyForwardBack = 0.0f;
//cameraLensPitchUpDown = 0.0f;
//cameraLensBankLeftRight = 0.0f;
//cameraLensLeftRight = 0.0f;
//movingCameraDolly = false;
//simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.CAMERA_RESET, (uint)0, INPUT_GROUPS.INPUT0, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
break;
 
Messages
1,244
Country
canada
Not sure whey but using button:0 with my sidewinder just moved it to reset position. Are you sure you have a INPUT_GROUP0 separate from the notification group?
 

JB3DG

Resource contributor
Messages
1,325
Country
southafrica
Button indexes in the strings start with 0 so button 11 would really be button 12 where marked on your stick.
 
Messages
120
Country
unitedstates
Another question: is there a way to set one joystick button event rather than one for each joystick button?

Instead of this:
simconnect.MapInputEventToClientEvent(INPUT_GROUPS.INPUT0, "joystick:0:button:1", EVENTS.CAMERA_RESET, (uint)0, (EVENTS)unused, (uint)0, (bool)false);
simconnect.MapInputEventToClientEvent(INPUT_GROUPS.INPUT0, "joystick:0:button:2", EVENTS.CAMERA_RESET, (uint)0, (EVENTS)unused, (uint)0, (bool)false);
simconnect.MapInputEventToClientEvent(INPUT_GROUPS.INPUT0, "joystick:0:button:3", EVENTS.CAMERA_RESET, (uint)0, (EVENTS)unused, (uint)0, (bool)false);
simconnect.MapInputEventToClientEvent(INPUT_GROUPS.INPUT0, "joystick:0:button:4", EVENTS.CAMERA_RESET, (uint)0, (EVENTS)unused, (uint)0, (bool)false);
etc
etc

can I do a one liner something like this and then check which button was pressed:
simconnect.MapInputEventToClientEvent(INPUT_GROUPS.INPUT0, "joystick:0:button:??", EVENTS.CAMERA_RESET, (uint)0, (EVENTS)unused, (uint)0, (bool)false);

I guess the same question applies to mapping to a hot key...can we do this with one line vs having to map a line for each possible hot key?

Thanks...
 
Messages
120
Country
unitedstates
Wow...for detecting hotkeys, that going to be a lot of lines...I'm surprises MS was so inefficient with this part of the SDK...thanks...
 

ddawson

Resource contributor
Messages
862
Country
canada
Well, there's probably better ways than SimConnect to do that.
If I were watching for keyboard input, I would most likely hook the FS message queue and watch for WM_KEYDOWN or WM_CHAR messages.
For game controllers, you can use DirectInput to poll them - this allows you to check for the state of all the buttons on a controller with one call.

Doug
 
Messages
120
Country
unitedstates
Thanks Doug...do you have a reference where I can read up on how to hook into the FS msg queue? I googled around (including here) but so far have not found anything on that specific topic. I'm using c# if that makes any difference.
 
Top