Navigation

    ViGEm Forums

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Twitter
    • GitHub
    • Discord

    Use ViGEm to create XBox 360 controller in C#

    Discussion and Support
    5
    35
    4224
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      lemmingDev last edited by

      fyi - I converted the code to DualShock4Controller and the buttons are pressing fine

      any idea why the xbox 360 isn't working?

      thanks again for your help

      1 Reply Last reply Reply Quote 0
      • L
        lemmingDev last edited by nefarius

        Buttons not working:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading;
        using System.Threading.Tasks;
        using Nefarius.ViGEm.Client;
        using Nefarius.ViGEm.Client.Targets;
        using Nefarius.ViGEm.Client.Targets.Xbox360;
        using Nefarius.ViGEm.Client.Targets.DualShock4;
        
        
        namespace ConsoleApp1
        {
            class Program
            {
                static ViGEmClient myViGEmClient = new ViGEmClient();
                static Xbox360Controller myController = new Xbox360Controller(myViGEmClient);
        
                static void Main(string[] args)
                {
                    myController.Connect();
        
                    while (true)
                    {
                        Xbox360Report controller1Report = new Xbox360Report();
        
                        controller1Report.SetButtons(Xbox360Buttons.A, Xbox360Buttons.B);
                        controller1Report.SetAxis(Xbox360Axes.LeftThumbX, 128);
        
                        myController.SendReport(controller1Report);
                        Thread.Sleep(20);
                    }
                }
            }
        

        Buttons working:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading;
        using System.Threading.Tasks;
        using Nefarius.ViGEm.Client;
        using Nefarius.ViGEm.Client.Targets;
        using Nefarius.ViGEm.Client.Targets.Xbox360;
        using Nefarius.ViGEm.Client.Targets.DualShock4;
        
        
        namespace ConsoleApp1
        {
            class Program
            {
                static ViGEmClient myViGEmClient = new ViGEmClient();
                static DualShock4Controller myController = new DualShock4Controller(myViGEmClient);
        
                static void Main(string[] args)
                {
                    myController.Connect();
        
                    while (true)
                    {
                        DualShock4Report controller1Report = new DualShock4Report();
        
                        controller1Report.SetButtons(DualShock4Buttons.Circle, DualShock4Buttons.Cross);
                        controller1Report.SetAxis(DualShock4Axes.LeftThumbX, 0);
        
                        myController.SendReport(controller1Report);
                        Thread.Sleep(20);
                    }
                }
            }
        }
        
        1 Reply Last reply Reply Quote 0
        • nefarius
          nefarius last edited by

          Please format code snippets accordingly for better readability. So far I don't see an obvious error...

          L 1 Reply Last reply Reply Quote 0
          • L
            lemmingDev @nefarius last edited by lemmingDev

            @nefarius said in Use ViGEm to create XBox 360 controller in C#:

            Please format code snippets accordingly for better readability. So far I don't see an obvious error...

            Thanks for the reply. I see how you added the ``` before and after to markup the code - will do that in future.

            Also, what can I do to try and troubleshoot the xbox360 controller not detecting button presses and axis movements? Is it a bug? Do I have an out-of-date version? Seems weird that PS4 works fine and xbox360 doesn't.

            1 Reply Last reply Reply Quote 0
            • L
              lemmingDev last edited by

              Hi

              Did some further testing

              ViGEmClient.dll version 1.13.1 is working with xbox360 correctly

              ViGEmClient.dll version 1.15.16 is not working with xbox360

              1 Reply Last reply Reply Quote 0
              • L
                lemmingDev last edited by lemmingDev

                Hmmn - now the 1.13.1 version isn't working for xbox360 either.

                Was playing around with updating dependencies, but put everything back the way it was, but am now having the same problem with 1.13.1 as 1.15.16

                Will restart and see how I go...

                EDIT --> nope - same issue

                going out of my mind

                1 Reply Last reply Reply Quote 0
                • L
                  lemmingDev last edited by lemmingDev

                  Tried it on another computer with the same result. Thinking it might be a bug of some sort

                  1 Reply Last reply Reply Quote 0
                  • L
                    lemmingDev last edited by lemmingDev

                    My Virtual Gamepad Emulation Bus version is 1.14.3.0

                    I see 1.16.95.0 is up on Github

                    Could I trouble you to compile and sign it so I can give it a go? The one at https://nuget.vigem.org/ installed through Powershell and on the caveman instructions: at https://docs.vigem.org/#!vigem-bus-driver-installation-caveman-edition.md are both the 1.14.3.0 version.

                    Hoping it may get the Xbox360 gamepads working

                    Thanks

                    1 Reply Last reply Reply Quote 0
                    • nefarius
                      nefarius last edited by

                      Version 1.15 or higher isn't ready for a public release yet and not guaranteed to be compatible so I don't recommend you use any other nuget package other than the official older one.

                      1 Reply Last reply Reply Quote 0
                      • L
                        lemmingDev last edited by

                        Thanks for getting back to me. Any idea what could be stopping it from working properly?

                        Are there special timing requirements? Does it require special threading stuff?

                        Could it be something to do with me running the latest official Windows 10 update?

                        I tried SimWinGamepad at https://github.com/DavidRieman/SimWinInput or https://www.nuget.org/packages/SimWinGamePad and am having similar results.

                        The gamepad is connected, but the reports don't seem to be registering properly.

                        If I do a simple button press and release with SimGamePad.Instance.Use(GamePadControl.A); it works properly, but if I try to do other stuff and then call simPad.Update(0);, nothing at all registers.

                        This leads me to believe it's a problem with my code setup or an incompatibility with a Windows update or something similar.

                        Could you try compiling my code on your PC and checking if it works?

                        using System;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Text;
                        using System.Threading;
                        using Nefarius.ViGEm.Client;
                        using Nefarius.ViGEm.Client.Targets;
                        using Nefarius.ViGEm.Client.Targets.Xbox360;
                        using Nefarius.ViGEm.Client.Exceptions;
                        
                        namespace ControllerConverter
                        {
                            class Program
                            {
                                static ViGEmClient client = new ViGEmClient();
                                static Xbox360Controller controller = new Xbox360Controller(client);
                                static Xbox360FeedbackReceivedEventArgs previousXbox360FeedbackReceivedEventArgs;
                        
                                static void Main(string[] args)
                                {
                                    previousXbox360FeedbackReceivedEventArgs = new Xbox360FeedbackReceivedEventArgs(0, 0, 1);
                        
                                    controller.FeedbackReceived += new Xbox360FeedbackReceivedEventHandler(OnXbox360FeedbackReceived);
                                    controller.Connect();
                                    Console.WriteLine("Virtual Xbox360 gamepad connected.");
                        
                                    while (true)
                                    {
                                        Xbox360Report report = new Xbox360Report();
                                        report.SetButtons(Xbox360Buttons.A, Xbox360Buttons.B, Xbox360Buttons.Up, Xbox360Buttons.LeftShoulder);
                                        report.SetAxis(Xbox360Axes.LeftThumbX, 0);
                                        report.SetAxis(Xbox360Axes.LeftThumbY, 0);
                                        report.SetAxis(Xbox360Axes.RightThumbX, 128);
                                        report.SetAxis(Xbox360Axes.RightThumbY, 0);
                                        report.SetAxis(Xbox360Axes.LeftTrigger, 0);
                                        report.SetAxis(Xbox360Axes.RightTrigger, 0);
                                        //Xbox360ReportExtensions.SetButtons(report, Xbox360Buttons.A);
                                        //Xbox360ReportExtensions.SetButtonState(report, Xbox360Buttons.A, true);
                                        controller.SendReport(report);
                                        Thread.Sleep(1);
                                    }
                                }
                        
                                static void OnXbox360FeedbackReceived(object sender, Xbox360FeedbackReceivedEventArgs e)
                                {
                                    if (e.LargeMotor != previousXbox360FeedbackReceivedEventArgs.LargeMotor && e.SmallMotor != previousXbox360FeedbackReceivedEventArgs.SmallMotor & e.LedNumber != previousXbox360FeedbackReceivedEventArgs.LedNumber)
                                    {
                                        // Store passed in values
                                        byte largeMotorData = e.LargeMotor;
                                        byte smallMotorData = e.SmallMotor;
                                        byte ledNumberData = e.LedNumber;
                        
                                        // Deal with new values
                                        Console.WriteLine("Large Motor Data: {0}", largeMotorData);
                                        Console.WriteLine("Small Motor Data: {0}", smallMotorData);
                                        Console.WriteLine("LED Number: {0}", ledNumberData);
                                    }
                        
                                    previousXbox360FeedbackReceivedEventArgs = e;
                                }
                            }
                        }
                        
                        1 Reply Last reply Reply Quote 0
                        • L
                          lemmingDev last edited by

                          Update

                          !!!PROGRESS!!! --> with getting the Xbox 360 controller working.

                          Seems I've been able to get it working; somewhat.

                          I can set the buttons with eg:

                          report.SetButtonState(Xbox360Buttons.A, true);
                          report.SetButtonState(Xbox360Buttons.B, false);
                          

                          And I can set the triggers with eg:

                          report.SetAxis(Xbox360Axes.LeftTrigger, 0x00); 
                          report.SetAxis(Xbox360Axes.RightTrigger, 0xFF);
                          

                          However I can't get the left and right analogs to move with eg:

                          report.SetAxis(Xbox360Axes.LeftThumbX, 0x00);
                          report.SetAxis(Xbox360Axes.LeftThumbY, 0xFF);
                          report.SetAxis(Xbox360Axes.RightThumbX, 0x00);
                          report.SetAxis(Xbox360Axes.RightThumbY, 0xFF);
                          

                          Any ideas how I can set them properly?

                          1 Reply Last reply Reply Quote 0
                          • L
                            lemmingDev last edited by

                            Mystery solved

                            The thumb sticks need to be set between -32768 and 32767

                            Which means it's now fully working!

                            Bet you never thought I'd get there!

                            Thanks for your help

                            1 Reply Last reply Reply Quote 0
                            • nefarius
                              nefarius last edited by

                              Yeah that's a pitfall I'm gonna rectify in the upcoming version. Glad you got progress!

                              1 Reply Last reply Reply Quote 0
                              • C
                                craigrs84 last edited by

                                Hey all, I want to do the same sort of thing as lemmingDev.

                                I cloned the ViGEm.NET project but can't get it to properly build. I run build.cmd and get this error:

                                Could not find a part of the path 'C:\Users\myuser\Downloads\testwii\ViGEm.NET\build\bin\Release\bin\release\x64\ViGEmClient.dll'.

                                Any ideas?

                                1 Reply Last reply Reply Quote 0
                                • Daltz333
                                  Daltz333 last edited by

                                  @craigrs84 Hey there! Thanks for the interest in building the project. I encourage you to jump into the discord for further support. Forums are slow and tedious while Discord is a lot more handy to help with. https://discord.vigem.org/

                                  1 Reply Last reply Reply Quote 0
                                  • P
                                    pejman last edited by

                                    How do i add the missing xbox360 buttons? only these buttons are in the class:
                                    Up = 1,
                                    Down = 2,
                                    Left = 4,
                                    Right = 8,
                                    Start = 16,
                                    Back = 32,
                                    LeftThumb = 64,
                                    RightThumb = 128,
                                    LeftShoulder = 256,
                                    RightShoulder = 512,
                                    Guide = 1024,
                                    A = 4096,
                                    B = 8192,
                                    X = 16384,
                                    Y = 32768

                                    nefarius 1 Reply Last reply Reply Quote 0
                                    • nefarius
                                      nefarius @pejman last edited by

                                      @pejman Missing? Which class? Which branch?

                                      1 Reply Last reply Reply Quote 0
                                      • P
                                        pejman last edited by

                                        namespace Nefarius.ViGEm.Client.Targets.Xbox360
                                        {
                                        [Flags]
                                        public enum Xbox360Buttons : ushort
                                        {
                                        Up = 1,
                                        Down = 2,
                                        Left = 4,
                                        Right = 8,
                                        Start = 16,
                                        Back = 32,
                                        LeftThumb = 64,
                                        RightThumb = 128,
                                        LeftShoulder = 256,
                                        RightShoulder = 512,
                                        Guide = 1024,
                                        A = 4096,
                                        B = 8192,
                                        X = 16384,
                                        Y = 32768
                                        }
                                        }

                                        i'm talking about the buttons when you hard press the joy sticks or 'up/right', 'up/left', and the rest of it.

                                        1 Reply Last reply Reply Quote 0
                                        • nefarius
                                          nefarius last edited by nefarius

                                          You didn't mention the branch name so I'm guessing you're talking about outdated/legacy master.

                                          You mean L3 and R3? They are literally there; LeftThumb and RightThumb.

                                          6c4d9c51-5644-40ec-bd42-a54f022729db-image.png

                                          And please format code sections for better readability, thanks 😄

                                          1 Reply Last reply Reply Quote 0
                                          • P
                                            pejman last edited by

                                            That is right, LeftThumb and RightThumb was the ones i was looking for, thanks for your response, i noticed combining buttons of directional pad also creates the up/left or up/right and so on. thank you again.

                                            nefarius 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post