1
DotNet / Can't used a joystick that is connected in the middle of program execution.
« on: February 15, 2023, 03:21:34 pm »
I encountered a problem when trying to use a gamepad or joystick that is connected in the middle of program execution.
I have this minimum code:
If I have a joystick connected and I run the application, it is detected and can be used without problem.
If in the middle of the execution I disconnect the joystick, the disconnection is detected, the number of connected joystick is updated.
then i reconnect the joystick, the connection is detected, the number of connected joystick is back to 1 and i can use the joystick again, so far everything works as expected.
The console output:
***************************************
The problem occurs when I launch the app without the joystick connected.
If, during the execution of the program, I connect the joystick, you can see that it actually triggers the "JoystickConnected" event. But I don't have access, since all the joysticks of "Joystick.IsConnected" mark false.
I have tried to force read with
"if (Joystick.IsButtonPressed(0,0)) { Console.WriteLine("Joystick 0, button 0 "); }"
But it doesn't work, it's like SFML detects that the joystick was connected, but doesn't give access to it.
Is it necessary to do something to be able to access the Joystick or is it a bug?
Spec
Windows 10.
C#
I have this minimum code:
Code: [Select]
// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");
using System;
using SFML.Audio;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
namespace sfml
{
class Program
{
static void Main(string[] args)
{
string TITULO = "Test Input";
string VERSION = "0.0.1";
//Selección estilo de ventana
Styles auxStyle = Styles.Default;
//Creamos el objeto windows
RenderWindow window = new RenderWindow(new VideoMode(800, 600), TITULO + " v " + VERSION, auxStyle);
window.Closed += (_, __) => window.Close();
window.JoystickConnected += (o, e) => gamePad_connect(e);
window.JoystickDisconnected += (o, e) => gamePad_disconnect(e);
//Variable propias de este contexto, como color de renderwindow o auxiliares para shader
Color backColor = new Color(0,0,255); //Color de fondo
Joystick.Update();
int Joystickcount = 0;
for(uint i=0;i<8;i++){
if (Joystick.IsConnected(i)) { Joystickcount++; }
}
Console.WriteLine("Total de Joystick: " + Joystickcount.ToString());
//Ciclo principal
while (window.IsOpen)
{
//***** Rutinas especiales como actualizacíon de gamepad *****
Joystick.Update();
int newJoystickcount = 0;
for(uint i=0;i<8;i++){
if (Joystick.IsConnected(i)) { newJoystickcount++; }
}
if (newJoystickcount != Joystickcount) {
Joystickcount = newJoystickcount;
Console.WriteLine("Total de Joystick: " + Joystickcount.ToString());
}
if (Joystick.IsButtonPressed(0,0)) { Console.WriteLine("Joystick 0, button 0 "); }
window.DispatchEvents(); //Control de eventos
window.Clear(backColor); //Limpiamos la pantalla con el color de fondo seleccionado
window.Display();
}
}
static void gamePad_connect(JoystickConnectEventArgs e){
Console.WriteLine("Joystick " + e.JoystickId.ToString() + "conectado");
}
static void gamePad_disconnect(JoystickConnectEventArgs e){
Console.WriteLine("Joystick " + e.JoystickId.ToString() + "desconectado");
}
}
}
If I have a joystick connected and I run the application, it is detected and can be used without problem.
If in the middle of the execution I disconnect the joystick, the disconnection is detected, the number of connected joystick is updated.
then i reconnect the joystick, the connection is detected, the number of connected joystick is back to 1 and i can use the joystick again, so far everything works as expected.
The console output:
Quote
Total de Joystick: 1
1661 Joystick 0, botón 0
Total de Joystick: 0
Joystick 0 desconectado
21 Failed to create DirectInput device: -2147467259
578 Failed to create DirectInput device: -2147221164
Total de Joystick: 1
Joystick 0 conectado
554 Joystick 0, botón 0
The program '[10288] test_input.dll' has exited with code 0 (0x0).
***************************************
The problem occurs when I launch the app without the joystick connected.
If, during the execution of the program, I connect the joystick, you can see that it actually triggers the "JoystickConnected" event. But I don't have access, since all the joysticks of "Joystick.IsConnected" mark false.
I have tried to force read with
"if (Joystick.IsButtonPressed(0,0)) { Console.WriteLine("Joystick 0, button 0 "); }"
But it doesn't work, it's like SFML detects that the joystick was connected, but doesn't give access to it.
Quote
Total de Joystick: 0
Joystick 0 conectado
Joystick 0 desconectado
14
Failed to create DirectInput device: -2147467259
Joystick 0 conectado
The program '[4728] test_input.dll' has exited with code 0 (0x0).
Is it necessary to do something to be able to access the Joystick or is it a bug?
Spec
Windows 10.
C#
Code: [Select]
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SFML.Net" Version="2.5.0" />
</ItemGroup>
</Project>