如何将PS4控制器的触发按钮导入Android?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将PS4控制器的触发按钮导入Android?相关的知识,希望对你有一定的参考价值。

我想把我的PS4控制器作为一个模型进入android应用程序。 Androids Developer Page(操纵杆和数字按钮)的示例对我来说很好。使用Log.e输出检查了这一点。我不明白如何获得R2和L2的值。在开发者页面上,它以这种方式描述:

处理肩部触发按压(但提供替代输入方法)。有些控制器有左右肩触发器。如果存在这些触发器,则Android会报告左触发按下作为AXIS_LTRIGGER事件,右触发按下作为AXIS_RTRIGGER事件。在Android 4.3(API级别18)上,生成AXIS_LTRIGGER的控制器也会为AXIS_BRAKE轴报告相同的值。 AXIS_RTRIGGER和AXIS_GAS也是如此。 Android报告所有模拟触发按下的标准化值从0.0(已发布)到1.0(完全按下)。并非所有控制器都有触发器,因此请考虑允许玩家使用其他按钮执行这些游戏操作。

添加此行:

float rTrigger = historyPos <0? event.getAxisValue(MotionEvent.AXIS_RTRIGGER):event.getHistoricalAxisValue(MotionEvent.AXIS_RTRIGGER,historyPos);

进程操纵杆输入():浮动触发始终为0.0

真的很期待把事情做好。

谢谢

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean handled = false;
        if ((event.getSource() & InputDevice.SOURCE_GAMEPAD)
                == InputDevice.SOURCE_GAMEPAD) {
            if (event.getRepeatCount() == 0) {

                if (keyCode == 96)
                    Log.e("Taste:", "Square pressed");

                if (keyCode == 97)
                    Log.e("Taste:", "Cross pressed");

                if (keyCode == 98)
                    Log.e("Taste:", "Circle pressed");
            }
            if (handled) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean handled = false;
        if ((event.getSource() & InputDevice.SOURCE_GAMEPAD)
                == InputDevice.SOURCE_GAMEPAD) {
            if (event.getRepeatCount() == 0) {

                if (keyCode == 96)
                    Log.e("Taste:", "Square released");

                if (keyCode == 97)
                    Log.e("Taste:", "X released");

                if (keyCode == 98)
                    Log.e("Taste:", "Circle released");
            }
            if (handled) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {


        // Check that the event came from a game controller
        if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) ==
                InputDevice.SOURCE_JOYSTICK &&
                event.getAction() == MotionEvent.ACTION_MOVE) {


            // Process all historical movement samples in the batch
            final int historySize = event.getHistorySize();

            // Process the movements starting from the
            // earliest historical position in the batch
            for (int i = 0; i < historySize; i++) {
                // Process the event at historical position i
                processJoystickInput(event, i);
            }

            // Process the current movement sample in the batch (position -1)
            processJoystickInput(event, -1);
            return true;
        }
        return super.onGenericMotionEvent(event);
    }


    private void processJoystickInput(MotionEvent event,
                                      int historyPos) {

        InputDevice mInputDevice = event.getDevice();

        // Calculate the horizontal distance to move by
        // using the input value from one of these physical controls:
        // the left control stick, hat axis, or the right control stick.
        float lx = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_X, historyPos);

        float rx = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_Z, historyPos);

        float ly = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_Y, historyPos);

        float ry = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_RZ, historyPos);


        Log.e("LX:", lx + "");
        Log.e("LY:", ly + "");
        Log.e("RX:", rx + "");
        Log.e("RY:", ry + "");
    }

    private static float getCenteredAxis(MotionEvent event,
                                         InputDevice device, int axis, int historyPos) {
        final InputDevice.MotionRange range =
                device.getMotionRange(axis, event.getSource());

        // A joystick at rest does not always report an absolute position of
        // (0,0). Use the getFlat() method to determine the range of values
        // bounding the joystick axis center.
        if (range != null) {
            final float flat = range.getFlat();
            final float value =
                    historyPos < 0 ? event.getAxisValue(axis) :
                            event.getHistoricalAxisValue(axis, historyPos);

            // Ignore axis values that are within the 'flat' region of the
            // joystick axis center.
            if (Math.abs(value) > flat) {
                return value;
            }
        }
        return 0;
    }
}
答案

自己解决了这个问题。触发按钮与Axis_RX和Axis_RY匹配

以上是关于如何将PS4控制器的触发按钮导入Android?的主要内容,如果未能解决你的问题,请参考以下文章

使用C ++在Linux上使用接口Playstation DualShock 4(DS4)控制器

PS4平台动作游戏新作《堕落骑士》发表!

WM_CLOSEWM_DESTROYWM_QUIT学习总结(点击关闭按钮会触发WM_CLOSE消息,DestroyWindow API会触发WM_DESTROY和WM_NCDESTROY消息,MS(代

android gridview 如何点击item中的一个组合控件的图标按钮触发整个item

如何触发 UISearchBar 中的取消按钮?

在 Xcode 中播放音效