C

cmujica

Usuario (Chile)

Primer post: 20 may 2011Último post: 20 may 2011
1
Posts
0
Puntos totales
5
Comentarios
Leer Joystick USB con javaSE
Leer Joystick USB con javaSE
Apuntes Y MonografiasporAnónimo5/20/2011

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; } //--------------------------------------------------------------- // getAxisAlt (UP, DOWN, LEFT, RIGHT) resets the direction //--------------------------------------------------------------- public boolean getAxisAlt (int nDir) { boolean bRet = isAvailable() && this.abAxis; if (bRet) this.abAxis = false; return bRet; } //--------------------------------------------------------------- // getButton (BUTTON1..4) //--------------------------------------------------------------- public boolean getButton (int nButton) { return isAvailable() && this.abButtons; } //--------------------------------------------------------------- // getButtonAlt (BUTTON1..4) resets the button //--------------------------------------------------------------- public boolean getButtonAlt (int nButton) { boolean bRet = isAvailable() && this.abButtons; if (bRet) this.abButtons = false; return bRet; } //--------------------------------------------------------------- // Event: joystickAxisChanged //--------------------------------------------------------------- public void joystickAxisChanged(Joystick pJoy) { if (this.pJoy != pJoy) return; this.abAxis = this.pJoy.getY()==-1.0; this.abAxis = this.pJoy.getY()==1.0; this.abAxis = this.pJoy.get) == 1.0; this.abAxis = this.pJoy.get) == -1.0; } //--------------------------------------------------------------- // Event: joystickButtonChanged //--------------------------------------------------------------- public void joystickButtonChanged(Joystick pJoy) { if (this.pJoy != pJoy) return; this.abButtons = (this.pJoy.getButtons() & Joystick.BUTTON1) != 0; this.abButtons = (this.pJoy.getButtons() & Joystick.BUTTON2) != 0; this.abButtons = (this.pJoy.getButtons() & Joystick.BUTTON3) != 0; this.abButtons = (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

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