在java中的操纵杆事件

时间:2015-07-07 21:35:05

标签: java joystick jinput

我有一个包含一个文本字段的java应用程序,我使用操纵杆。

当按下操纵杆上的按钮时,如何在文本字段中执行某些操作?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



package op;

import net.java.games.input.Controller;

/**
 *
 * @author Ahmed
 */
public class Test extends javax.swing.JFrame {

    /**
     * Creates new form Test
     */
    public Test() {
        initComponents();
        con();
    }

    private void con(){

        JInputJoystick joystick = new JInputJoystick(Controller.Type.STICK);
        if( !joystick.isControllerConnected() ){   
            txt.setText("Not Connected");
        }
        else    
           txt.setText(joystick.getControllerType()+" "+joystick.getControllerName()+" Controller Cound!");
        if( !joystick.pollController() ) {

            txt.setText("Controller disconnected!");
        }

       // Number of buttons.

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txt = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(txt, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                .addGap(88, 88, 88))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(145, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */


    // Variables declaration - do not modify                     
    private javax.swing.JTextField txt;
    // End of variables declaration                   
}

1 个答案:

答案 0 :(得分:0)

基于这里的教程项目:https://theuzo007.wordpress.com/2013/10/26/joystick-in-java-with-jinput-v2/看起来你必须循环并只是检查一下,所以

0x2000
0x2020