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



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



  • 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



  • 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



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



  • 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



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



  • 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;
            }
        }
    }
    


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



  • 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



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



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



  • @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/



  • 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



  • @pejman Missing? Which class? Which branch?



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



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



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



  • @pejman nice, btw. new API coming up is way less confusing so stick around!



  • Another question, does your driver support shock feedback?


Log in to reply