Java Code Examples for android.view.InputDevice#SOURCE_GAMEPAD

The following examples show how to use android.view.InputDevice#SOURCE_GAMEPAD . 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: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 6 votes vote down vote up
/**
 * Check for any game controllers that are connected already.
 */
private void checkGameControllers() {
    Log.d(TAG, "checkGameControllers");
    int[] deviceIds = mInputManager.getInputDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();

        // Verify that the device has gamepad buttons, control sticks, or
        // both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
                || ((sources & InputDevice.SOURCE_JOYSTICK)
                    == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            if (!mConnectedDevices.contains(deviceId)) {
                mConnectedDevices.add(deviceId);
                if (mCurrentDeviceId == -1) {
                    mCurrentDeviceId = deviceId;
                    mControllerView.setCurrentControllerNumber(dev.getControllerNumber());
                    mControllerView.invalidate();
                }
            }
        }
    }
}
 
Example 2
Source File: AndroidInputHandler14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
    public boolean onGenericMotion(View view, MotionEvent event) {
        if (view != getView()) {
            return false;
        }

        boolean consumed = false;

        int source = event.getSource();
//        logger.log(Level.INFO, "onGenericMotion source: {0}", source);

        boolean isJoystick =
                ((source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK);

        if (isJoystick && joyInput != null) {
//            logger.log(Level.INFO, "onGenericMotion source: {0}, isJoystick: {1}",
//                    new Object[]{source, isJoystick});
            // send the event to the touch processor
            consumed = consumed || ((AndroidJoyInput14)joyInput).onGenericMotion(event);
        }

        return consumed;
    }
 
Example 3
Source File: Gamepad.java    From BobEngine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@TargetApi(19)
public boolean onKeyDown(int keyCode, KeyEvent event) {
	int player;      // The player number (controller number) that caused this event

	if (event.getDevice() != null && (event.getSource() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
		try {
			player = event.getDevice().getControllerNumber();
		} catch (NoSuchMethodError e) {
			player = 1;
		}

		if (event.getRepeatCount() == 0 || player != lastPlayerDown) {
			newpress(player, keyCode);
			lastPlayerDown = player;
		}

		return true;
	}

	return false;
}
 
Example 4
Source File: Gamepad.java    From BobEngine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@TargetApi(19)
public boolean onKeyUp(int keyCode, KeyEvent event) {
	int player;      // The player number (controller number) that caused this event

	if (event.getDevice() != null && (event.getSource() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
		try {
			player = event.getDevice().getControllerNumber();
		} catch (NoSuchMethodError e) {
			player = 1;
		}

		if (event.getRepeatCount() == 0 || player != lastPlayerUp) {
			released(player, keyCode);
			lastPlayerUp = player;
		}

		return true;
	}

	return false;
}
 
Example 5
Source File: GameView.java    From bluetooth with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    mInputManager.onGenericMotionEvent(event);

    // Check that the event came from a joystick or gamepad since a generic
    // motion event could be almost anything. API level 18 adds the useful
    // event.isFromSource() helper function.
    int eventSource = event.getSource();
    if ((((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
            ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK))
            && event.getAction() == MotionEvent.ACTION_MOVE) {
        int id = event.getDeviceId();
        if (-1 != id) {
            Ship curShip = getShipForId(id);
            if (curShip.onGenericMotionEvent(event)) {
                return true;
            }
        }
    }
    return super.onGenericMotionEvent(event);
}
 
Example 6
Source File: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to determine if input device is a gamepad.
 * 
 * @param device
 * @return
 */
private boolean isGamepad(InputDevice device) {
    if ((device.getSources() &
            InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD
            || (device.getSources() &
            InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) {
        return true;
    }
    return false;
}
 
Example 7
Source File: AndroidInputHandler14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
    public boolean onKey(View view, int keyCode, KeyEvent event) {
        if (view != getView()) {
            return false;
        }

        boolean consumed = false;

//        logger.log(Level.INFO, "onKey keyCode: {0}, action: {1}, event: {2}",
//                new Object[]{KeyEvent.keyCodeToString(keyCode), event.getAction(), event});
        int source = event.getSource();
//        logger.log(Level.INFO, "onKey source: {0}", source);

        boolean isTouch =
                ((source & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) ||
                ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD);
        boolean isJoystick =
                ((source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK);
        boolean isUnknown =
                (source & android.view.InputDevice.SOURCE_UNKNOWN) == android.view.InputDevice.SOURCE_UNKNOWN;

        if (touchInput != null && (isTouch || (isUnknown && this.touchInput.isSimulateKeyboard()))) {
//            logger.log(Level.INFO, "onKey source: {0}, isTouch: {1}",
//                    new Object[]{source, isTouch});
            consumed = touchInput.onKey(event);
        }
        if (isJoystick && joyInput != null) {
//            logger.log(Level.INFO, "onKey source: {0}, isJoystick: {1}",
//                    new Object[]{source, isJoystick});
            // use inclusive OR to make sure the onKey method is called.
            consumed = consumed | ((AndroidJoyInput14)joyInput).onKey(event);
        }

        return consumed;

    }
 
Example 8
Source File: MainActivity.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(android.view.KeyEvent event) {
    boolean handled = false;
    if ((event.getSource() & InputDevice.SOURCE_GAMEPAD)
        == InputDevice.SOURCE_GAMEPAD) {

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_BUTTON_X:
                    last.setText("X Button");
                    handled = true;
                    break;
                case KeyEvent.KEYCODE_BUTTON_A:
                    last.setText("A Button");
                    handled = true;
                    break;
                case KeyEvent.KEYCODE_BUTTON_Y:
                    last.setText("Y Button");
                    handled = true;
                    break;
                case KeyEvent.KEYCODE_BUTTON_B:
                    last.setText("B Button");
                    handled = true;
                    break;
            }
            if (!handled)
                logger.append("code is " + event.getKeyCode() + "\n");
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            //don't care, but need to handle it.
            handled = true;
        } else {
            logger.append("unknown action " + event.getAction());
        }
        return handled;
    }

    return handled;
}
 
Example 9
Source File: MainActivity.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
public ArrayList getGameControllerIds() {
    ArrayList gameControllerDeviceIds = new ArrayList();
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();

        // Verify that the device has gamepad buttons, control sticks, or both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
            || ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            name.setText(dev.getName());
            if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
            }
            //possible both maybe true.
            if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
                isGamePad = true;
            if ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)
                isJoyStick = true;
            logger.append("GamePad: " + isGamePad + "\n");
            logger.append("JoyStick: " + isJoyStick + "\n");
        }

    }
    return gameControllerDeviceIds;
}
 
Example 10
Source File: GameView.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
void findControllersAndAttachShips() {
    int[] deviceIds = mInputManager.getInputDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = mInputManager.getInputDevice(deviceId);
        int sources = dev.getSources();
        // if the device is a gamepad/joystick, create a ship to represent it
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // if the device has a gamepad or joystick
            getShipForId(deviceId);
        }
    }
}
 
Example 11
Source File: AndroidControllers.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
private boolean isController(InputDevice device) {
	return ((device.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK)
			&& (((device.getSources() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
			|| (device.getKeyboardType() != InputDevice.KEYBOARD_TYPE_ALPHABETIC));
}
 
Example 12
Source File: AliteIntro.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
private boolean isController(int sourceId) {
	return (sourceId & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
		   (sourceId & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD ||
		   (sourceId & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
}
 
Example 13
Source File: AndroidView.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
private boolean isController(int sourceId) {
	return (sourceId & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
		   (sourceId & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD ||
		   (sourceId & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
}
 
Example 14
Source File: AndroidJoystickJoyInput14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public List<Joystick> loadJoysticks(int joyId, InputManager inputManager) {
    logger.log(Level.INFO, "loading Joystick devices");
    ArrayList<Joystick> joysticks = new ArrayList<Joystick>();
    joysticks.clear();
    joystickIndex.clear();

    ArrayList<Integer> gameControllerDeviceIds = new ArrayList<>();
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();
        logger.log(Level.FINE, "deviceId[{0}] sources: {1}", new Object[]{deviceId, sources});

        // Verify that the device has gamepad buttons, control sticks, or both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
                logger.log(Level.FINE, "Attempting to create joystick for device: {0}", dev);
                // Create an AndroidJoystick and store the InputDevice so we
                // can later correspond the input from the InputDevice to the
                // appropriate jME Joystick event
                AndroidJoystick joystick = new AndroidJoystick(inputManager,
                                                            joyInput,
                                                            dev,
                                                            joyId+joysticks.size(),
                                                            dev.getName());
                joystickIndex.put(deviceId, joystick);
                joysticks.add(joystick);

                // Each analog input is reported as a MotionRange
                // The axis number corresponds to the type of axis
                // The AndroidJoystick.addAxis(MotionRange) converts the axis
                // type reported by Android into the jME Joystick axis
                List<MotionRange> motionRanges = dev.getMotionRanges();
                for (MotionRange motionRange: motionRanges) {
                    logger.log(Level.INFO, "motion range: {0}", motionRange.toString());
                    logger.log(Level.INFO, "axis: {0}", motionRange.getAxis());
                    JoystickAxis axis = joystick.addAxis(motionRange);
                    logger.log(Level.INFO, "added axis: {0}", axis);
                }

                // InputDevice has a method for determining if a keyCode is
                // supported (InputDevice  public boolean[] hasKeys (int... keys)).
                // But this method wasn't added until rev 19 (Android 4.4)
                // Therefore, we only can query the entire device and see if
                // any InputDevice supports the keyCode.  This may result in
                // buttons being configured that don't exist on the specific
                // device, but I haven't found a better way yet.
                for (int keyCode: AndroidGamepadButtons) {
                    logger.log(Level.INFO, "button[{0}]: {1}",
                            new Object[]{keyCode, KeyCharacterMap.deviceHasKey(keyCode)});
                    if (KeyCharacterMap.deviceHasKey(keyCode)) {
                        // add button even though we aren't sure if the button
                        // actually exists on this InputDevice
                        logger.log(Level.INFO, "button[{0}] exists somewhere", keyCode);
                        JoystickButton button = joystick.addButton(keyCode);
                        logger.log(Level.INFO, "added button: {0}", button);
                    }
                }

            }
        }
    }


    loaded = true;
    return joysticks;
}