net.java.games.input.EventQueue Java Examples

The following examples show how to use net.java.games.input.EventQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GamepadListener.java    From haxademic with MIT License 5 votes vote down vote up
public GamepadListener() {
	new Thread(new Runnable() { public void run() {

		// look for controllers via JInput
		Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
		
		if (controllers.length == 0) {
			P.error("JInput Found no controllers.");
		} else {
			P.out("Gamepads found:");
			for (int i = 0; i < controllers.length; i++) {
				P.out("["+i+"] " + controllers[i].getName(), " | ", controllers[i].getType());
			}
		}

		while (true) {
			// poll each device
			for (int i = 0; i < controllers.length; i++) {
				// On OS X, Gamepad showed up as GAMEPAD, and on Windows, UNKNOWN
				if(controllers[i].getType() != Controller.Type.MOUSE && controllers[i].getType() != Controller.Type.KEYBOARD) {
					// get input updates
					controllers[i].poll();
					EventQueue queue = controllers[i].getEventQueue();
					Event event = new Event();
					while (queue.getNextEvent(event)) {
						Component comp = event.getComponent();
						float value = event.getValue();
						GamepadState.instance().setControlValue(comp.getName(), value);
					}
				}
			}
		}
		
	}}).start();
}
 
Example #2
Source File: ControllerImpl.java    From halfnes with GNU General Public License v3.0 4 votes vote down vote up
private Runnable eventQueueLoop() {
    return new Runnable() {
        @Override
        public void run() {
            if (gameController != null) {
                Event event = new Event();
                while (!Thread.interrupted()) {
                    gameController.poll();
                    EventQueue queue = gameController.getEventQueue();
                    while (queue.getNextEvent(event)) {
                        Component component = event.getComponent();
                        if (component.getIdentifier() == Component.Identifier.Axis.X) {
                            if (event.getValue() > threshold) {
                                gamepadbyte |= BIT7;//left on, right off
                                gamepadbyte &= ~BIT6;

                            } else if (event.getValue() < -threshold) {
                                gamepadbyte |= BIT6;
                                gamepadbyte &= ~BIT7;
                            } else {
                                gamepadbyte &= ~(BIT7 | BIT6);
                            }
                        } else if (component.getIdentifier() == Component.Identifier.Axis.Y) {
                            if (event.getValue() > threshold) {
                                gamepadbyte |= BIT5;//up on, down off
                                gamepadbyte &= ~BIT4;
                            } else if (event.getValue() < -threshold) {
                                gamepadbyte |= BIT4;//down on, up off
                                gamepadbyte &= ~BIT5;
                            } else {
                                gamepadbyte &= ~(BIT4 | BIT5);
                            }
                        } else if (component == buttons[0]) {
                            if (isPressed(event)) {
                                gamepadbyte |= BIT0;
                            } else {
                                gamepadbyte &= ~BIT0;
                            }
                        } else if (component == buttons[1]) {
                            if (isPressed(event)) {
                                gamepadbyte |= BIT1;
                            } else {
                                gamepadbyte &= ~BIT1;
                            }
                        } else if (component == buttons[2]) {
                            if (isPressed(event)) {
                                gamepadbyte |= BIT2;
                            } else {
                                gamepadbyte &= ~BIT2;
                            }
                        } else if (component == buttons[3]) {
                            if (isPressed(event)) {
                                gamepadbyte |= BIT3;
                            } else {
                                gamepadbyte &= ~BIT3;
                            }
                        }
                    }

                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        // Preserve interrupt status
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
    };
}