22
33import static android .view .InputDevice .KEYBOARD_TYPE_ALPHABETIC ;
44import static android .view .InputDevice .SOURCE_GAMEPAD ;
5- import static android .view .KeyEvent .KEYCODE_DPAD_CENTER ;
6- import static android .view .KeyEvent .KEYCODE_DPAD_DOWN ;
7- import static android .view .KeyEvent .KEYCODE_DPAD_LEFT ;
8- import static android .view .KeyEvent .KEYCODE_DPAD_RIGHT ;
9- import static android .view .KeyEvent .KEYCODE_DPAD_UP ;
105
11- import android .view .InputDevice ;
126import android .view .KeyEvent ;
13- import android .view .MotionEvent ;
147
158
169public class GamepadDpad {
17- private int mLastKeycode = KEYCODE_DPAD_CENTER ;
18-
19- /**
20- * Convert the event to a 2 int array: keycode and keyAction, similar to a keyEvent
21- * @param event The motion to convert
22- * @return int[0] keycode, int[1] keyAction
23- */
24- public int [] convertEvent (MotionEvent event ){
25- // Use the hat axis value to find the D-pad direction
26- float xaxis = event .getAxisValue (MotionEvent .AXIS_HAT_X );
27- float yaxis = event .getAxisValue (MotionEvent .AXIS_HAT_Y );
28- int action = KeyEvent .ACTION_DOWN ;
29-
30- // Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
31- // LEFT and RIGHT direction accordingly.
32- if (Float .compare (xaxis , -1.0f ) == 0 ) {
33- mLastKeycode = KEYCODE_DPAD_LEFT ;
34- } else if (Float .compare (xaxis , 1.0f ) == 0 ) {
35- mLastKeycode = KEYCODE_DPAD_RIGHT ;
36- }
37- // Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
38- // UP and DOWN direction accordingly.
39- else if (Float .compare (yaxis , -1.0f ) == 0 ) {
40- mLastKeycode = KEYCODE_DPAD_UP ;
41- } else if (Float .compare (yaxis , 1.0f ) == 0 ) {
42- mLastKeycode = KEYCODE_DPAD_DOWN ;
43- }else {
44- //No keycode change
45- action = KeyEvent .ACTION_UP ;
46- }
47-
48- return new int []{mLastKeycode , action };
49-
50- }
51-
52- @ SuppressWarnings ("unused" ) public static boolean isDpadEvent (MotionEvent event ) {
53- // Check that input comes from a device with directional pads.
54- // And... also the joystick since it declares sometimes as a joystick.
55- return (event .getSource () & InputDevice .SOURCE_JOYSTICK ) == InputDevice .SOURCE_JOYSTICK ;
56- }
57-
58- public static boolean isDpadEvent (KeyEvent event ){
59- //return ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) && (event.getDevice().getKeyboardType() == KEYBOARD_TYPE_NON_ALPHABETIC);
60- return event .isFromSource (SOURCE_GAMEPAD ) && event .getDevice ().getKeyboardType () != KEYBOARD_TYPE_ALPHABETIC ;
10+ public static boolean isDpadEvent (KeyEvent event ) {
11+ return event .isFromSource (SOURCE_GAMEPAD ) && (event .getDevice () == null || event .getDevice ().getKeyboardType () != KEYBOARD_TYPE_ALPHABETIC );
6112 }
6213}
0 commit comments