Click here to Skip to main content
15,880,405 members
Articles / Multimedia / DirectX

Using XInput to access an Xbox 360 Controller in Managed Code

Rate me:
Please Sign up or sign in to vote.
4.58/5 (9 votes)
11 Nov 2012CPOL8 min read 80.8K   5.9K   21  
Using the XInput library to interface with the Xbox 360 controller.
<Window x:Class="J2i.Net.XinputClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converters="clr-namespace:J2i.Net.XinputClient.Converters"
        Title="MainWindow" Height="400" Width="379"
         MinHeight="400" MinWidth="390"
         MaxHeight="400" MaxWidth="390"
        
        >
    <Window.Resources>
        <converters:BatteryTypeValueToBatteryTypeString x:Key="BatteryTypeValueToBatteryTypeString" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Margin="2,10,2,10">
            <TextBlock Margin="5">Controller Index:</TextBlock>
            <ComboBox Width="50" SelectionChanged="SelectedControllerChanged"  >
                <ComboBoxItem IsSelected="True">0</ComboBoxItem>
                <ComboBoxItem>1</ComboBoxItem>
                <ComboBoxItem>2</ComboBoxItem>
                <ComboBoxItem>3</ComboBoxItem>
            </ComboBox>
        </StackPanel>
    <Grid  Height="300" Width="350" Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" MinHeight="200"/>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />

            </Grid.RowDefinitions>

            <CheckBox x:Name="CheckboxAButton" Grid.Row="0" IsChecked="{Binding SelectedController.IsAPressed, Mode=OneWay}" />
            <TextBlock Text="A Button" Grid.Row="0" Grid.Column="1"  />

            <CheckBox x:Name="CheckboxBButton"  Grid.Row="1" IsChecked="{Binding SelectedController.IsBPressed, Mode=OneWay}" />
            <TextBlock Text="B Button" Grid.Row="1" Grid.Column="1" />

            <CheckBox x:Name="CheckboxXButton"  Grid.Row="2"  IsChecked="{Binding SelectedController.IsXPressed, Mode=OneWay}"  />
            <TextBlock Text="X Button" Grid.Row="2" Grid.Column="1" />

            <CheckBox x:Name="CheckboxYButton"  Grid.Row="3"  IsChecked="{Binding SelectedController.IsYPressed, Mode=OneWay}" />
            <TextBlock Text="Y Button" Grid.Row="3" Grid.Column="1" />


            <CheckBox x:Name="CheckboxLeftShoulderButton"  Grid.Row="4"  IsChecked="{Binding SelectedController.IsLeftShoulderPressed, Mode=OneWay}"  />
            <TextBlock Text="Left Shoulder Button" Grid.Row="4" Grid.Column="1" />

            <CheckBox x:Name="CheckboxRightShoulderButton"  Grid.Row="5"  IsChecked="{Binding SelectedController.IsRightShoulderPressed, Mode=OneWay}"  />
            <TextBlock Text="Right Shoulder Button" Grid.Row="5" Grid.Column="1" />

            <CheckBox x:Name="CheckboxLeftStickButton"  Grid.Row="6"  IsChecked="{Binding SelectedController.IsLeftStickPressed, Mode=OneWay}"  />
            <TextBlock Text="Left Stick Button" Grid.Row="6" Grid.Column="1" />


            <CheckBox x:Name="CheckboxRightStickButton"  Grid.Row="7"   IsChecked="{Binding SelectedController.IsRightStickPressed, Mode=OneWay}" />
            <TextBlock Text="Right Stick Button" Grid.Row="7" Grid.Column="1" />


            <Grid Width="50" Height="50" Grid.Row="8"  Grid.ColumnSpan="2">
                <CheckBox x:Name="CheckboxDPadUpButton"  HorizontalAlignment="Center" VerticalAlignment="Top"  IsChecked="{Binding SelectedController.IsDPadUpPressed, Mode=OneWay}" />
                <CheckBox x:Name="CheckboxDPadDownButton"   HorizontalAlignment="Center" VerticalAlignment="Bottom"  IsChecked="{Binding SelectedController.IsDPadDownPressed, Mode=OneWay}" />
                <CheckBox x:Name="CheckboxDPadLeftButton"  HorizontalAlignment="Left" VerticalAlignment="Center"  IsChecked="{Binding SelectedController.IsDPadLeftPressed, Mode=OneWay}" />
                <CheckBox x:Name="CheckboxDPadRightButton"  HorizontalAlignment="Right" VerticalAlignment="Center"  IsChecked="{Binding SelectedController.IsDPadRightPressed, Mode=OneWay}" />
            </Grid>

        </Grid>
        
        <Grid Grid.Column="1" MinWidth="200">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock Text="Left X Axis " Grid.Row="0" />
            <ProgressBar Grid.Row="1" Minimum="-32768" Maximum="32767"  Value="{Binding SelectedController.LeftThumbStick.X, Mode=OneWay}"  />

            <TextBlock Text="Left Y Axis" Grid.Row="2" />
            <ProgressBar Grid.Row="3"  Minimum="-32768" Maximum="32767"  Value="{Binding SelectedController.LeftThumbStick.Y, Mode=OneWay}" />

            <TextBlock Text="Right X Axis" Grid.Row="4" />
            <ProgressBar Grid.Row="5"  Minimum="-32768" Maximum="32767"  Value="{Binding SelectedController.RightThumbStick.X, Mode=OneWay}"  />
            
            <TextBlock Text="Right Y Axis" Grid.Row="6" />
            <ProgressBar Grid.Row="7" Minimum="-32768" Maximum="32767"  Value="{Binding SelectedController.RightThumbStick.Y, Mode=OneWay}"  />

            <TextBlock Text="Left Trigger" Grid.Row="8" />
            <ProgressBar Grid.Row="9"  Minimum="0" Maximum="255"   Value="{Binding SelectedController.LeftTrigger, Mode=OneWay}" />

            <TextBlock Text="Right Trigger" Grid.Row="10" />
            <ProgressBar Grid.Row="11"  Minimum="0" Maximum="255" Value="{Binding SelectedController.RightTrigger, Mode=OneWay}" />
        </Grid>


            <Grid Grid.Row="2" Grid.ColumnSpan="4">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                
                <TextBlock Grid.Row="1">Vibration Motors</TextBlock>
                <TextBlock Grid.Column="1">Left Motor</TextBlock>
                <TextBlock Grid.Column="2">Right Motor</TextBlock>
                <Button Grid.Column="3" Grid.Row="1" Content="Send" Click="SendVibration_Click" />
                <Slider Margin="2" Grid.Column="1"  Grid.Row="1" VerticalAlignment="Center" Minimum="0" Maximum="1"  x:Name="LeftMotorSpeed"/>
                <Slider Margin="2" Grid.Column="2"  Grid.Row="1" VerticalAlignment="Center" Minimum="0" Maximum="1" x:Name="RightMotorSpeed" />



            </Grid>
        
            <Grid Grid.Row="3" Grid.ColumnSpan="4">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>
                
                <TextBlock Text="Battery Info:" />
                <TextBlock  Grid.Column="1" Text="{Binding SelectedController.BatteryInformationGamepad}" />
            </Grid>
        </Grid>
        

    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions