InicioApuntes Y MonografiasLeer Joystick USB con javaSE

Leer Joystick USB con javaSE

Estimados, hice una peguita en donde me tenia que conectar con un instrumento por el puerto USB, en un principio me volvi chango buscando "java+usb" de lo cual no encontré buena información, pero luego me di cuenta que el instrumento funcionaba como si fuera un joystick, entonces cambie la búsqueda y pude hacer el software que leía lo que entregaba el instrumento.

Acá les explico como se hace

Acá esta la clase que maneja los eventos


código

import java.io.IOException;
import com.centralnexus.input.Joystick;
import com.centralnexus.input.JoystickListener;

public class JoystickControl implements JoystickListener
{
//---------------------------------------------------------------
// MEMBERS
//---------------------------------------------------------------

// Class Constants
public final int UP = 0,
DOWN = 1,
LEFT = 2,
RIGHT = 3,
BUTTON1 = 0,
BUTTON2 = 1,
BUTTON3 = 2,
BUTTON4 = 3;

// Variable members
private Joystick pJoy;
private int nNumDevices;
private boolean abAxis[] = {false, false, false, false};
private boolean abButtons[] = {false, false, false, false};

//---------------------------------------------------------------
// CONSTRUCTOR
//---------------------------------------------------------------
public JoystickControl()
{
super();
this.pJoy = null;
this.nNumDevices = Joystick.getNumDevices();
if (this.nNumDevices > 0)
{
try
{
this.pJoy = Joystick.createInstance();
this.pJoy.addJoystickListener(this);
}
catch (IOException e)
{
e.printStackTrace();
this.pJoy = null;
}
}
}

//---------------------------------------------------------------
// Is Available
//---------------------------------------------------------------
public boolean isAvailable ()
{
return (this.pJoy != null);
}

//---------------------------------------------------------------
// getAxis (UP, DOWN, LEFT, RIGHT)
//---------------------------------------------------------------
public boolean getAxis (int nDir)
{
return isAvailable() && this.abAxis[nDir];
}

//---------------------------------------------------------------
// getAxisAlt (UP, DOWN, LEFT, RIGHT) resets the direction
//---------------------------------------------------------------
public boolean getAxisAlt (int nDir)
{
boolean bRet = isAvailable() && this.abAxis[nDir];
if (bRet)
this.abAxis[nDir] = false;
return bRet;
}


//---------------------------------------------------------------
// getButton (BUTTON1..4)
//---------------------------------------------------------------
public boolean getButton (int nButton)
{
return isAvailable() && this.abButtons[nButton];
}

//---------------------------------------------------------------
// getButtonAlt (BUTTON1..4) resets the button
//---------------------------------------------------------------
public boolean getButtonAlt (int nButton)
{
boolean bRet = isAvailable() && this.abButtons[nButton];
if (bRet)
this.abButtons[nButton] = false;
return bRet;
}

//---------------------------------------------------------------
// Event: joystickAxisChanged
//---------------------------------------------------------------
public void joystickAxisChanged(Joystick pJoy)
{
if (this.pJoy != pJoy)
return;
this.abAxis[UP] = this.pJoy.getY()==-1.0;
this.abAxis[DOWN] = this.pJoy.getY()==1.0;
this.abAxis[right] = this.pJoy.get) == 1.0;
this.abAxis[left] = this.pJoy.get) == -1.0;
}

//---------------------------------------------------------------
// Event: joystickButtonChanged
//---------------------------------------------------------------
public void joystickButtonChanged(Joystick pJoy)
{
if (this.pJoy != pJoy)
return;
this.abButtons[BUTTON1] = (this.pJoy.getButtons() & Joystick.BUTTON1) != 0;
this.abButtons[BUTTON2] = (this.pJoy.getButtons() & Joystick.BUTTON2) != 0;
this.abButtons[BUTTON3] = (this.pJoy.getButtons() & Joystick.BUTTON3) != 0;
this.abButtons[BUTTON4] = (this.pJoy.getButtons() & Joystick.BUTTON4) != 0;
}

}




Luego deben agregar el Joystick.jar que hace la interface con el joystick USB


También deben agregar jjstick.dll en system32 (Se puede para linux pero solo posteare para windows, lo unico que cambia es que se agregan archivos .so o .lib me parece no recuerdo).

DESCARGAR (aca estan los archivos que les nombré)

Y listo, luego crear una instancia de la clase mostrada primeramente y mostrar lo que uno desea, eventos de botones o flechas.

Espero les sirva, Saludos
Datos archivados del Taringa! original
0puntos
691visitas
0comentarios
Actividad nueva en Posteamelo
0puntos
3visitas
0comentarios
Dar puntos:

Dejá tu comentario

0/2000

Autor del Post

c
cmujica🇦🇷
Usuario
Puntos0
Posts1
Ver perfil →
PosteameloArchivo Histórico de Taringa! (2004-2017). Preservando la inteligencia colectiva de la internet hispanohablante.

CONTACTO

18 de Septiembre 455, Casilla 52

Chillán, Región de Ñuble, Chile

Solo correo postal

© 2026 Posteamelo.com. No afiliado con Taringa! ni sus sucesores.

Contenido preservado con fines históricos y culturales.