Fixed game controller buttons being unresponsive when the on-screen keyboard is up Also mapped controller A and B buttons to interact with messagebox dialogs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index af9265b..e44cc2d 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -1232,8 +1232,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
}
// This method is called by SDLControllerManager's API 26 Generic Motion Handler.
- public static View getContentView()
- {
+ public static View getContentView() {
return mLayout;
}
@@ -1304,6 +1303,77 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
return event.isPrintingKey() || event.getKeyCode() == KeyEvent.KEYCODE_SPACE;
}
+ public static boolean handleKeyEvent(View v, int keyCode, KeyEvent event, InputConnection ic) {
+ int deviceId = event.getDeviceId();
+ int source = event.getSource();
+
+ if (source == InputDevice.SOURCE_UNKNOWN) {
+ InputDevice device = InputDevice.getDevice(deviceId);
+ if (device != null) {
+ source = device.getSources();
+ }
+ }
+
+// if (event.getAction() == KeyEvent.ACTION_DOWN) {
+// Log.v("SDL", "key down: " + keyCode + ", deviceId = " + deviceId + ", source = " + source);
+// } else if (event.getAction() == KeyEvent.ACTION_UP) {
+// Log.v("SDL", "key up: " + keyCode + ", deviceId = " + deviceId + ", source = " + source);
+// }
+
+ // Dispatch the different events depending on where they come from
+ // Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
+ // So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD
+ //
+ // Furthermore, it's possible a game controller has SOURCE_KEYBOARD and
+ // SOURCE_JOYSTICK, while its key events arrive from the keyboard source
+ // So, retrieve the device itself and check all of its sources
+ if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
+ // Note that we process events with specific key codes here
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
+ if (SDLControllerManager.onNativePadDown(deviceId, keyCode) == 0) {
+ return true;
+ }
+ } else if (event.getAction() == KeyEvent.ACTION_UP) {
+ if (SDLControllerManager.onNativePadUp(deviceId, keyCode) == 0) {
+ return true;
+ }
+ }
+ }
+
+ if ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD) {
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
+ if (isTextInputEvent(event)) {
+ if (ic != null) {
+ ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
+ } else {
+ SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1);
+ }
+ }
+ onNativeKeyDown(keyCode);
+ return true;
+ } else if (event.getAction() == KeyEvent.ACTION_UP) {
+ onNativeKeyUp(keyCode);
+ return true;
+ }
+ }
+
+ if ((source & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) {
+ // on some devices key events are sent for mouse BUTTON_BACK/FORWARD presses
+ // they are ignored here because sending them as mouse input to SDL is messy
+ if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_FORWARD)) {
+ switch (event.getAction()) {
+ case KeyEvent.ACTION_DOWN:
+ case KeyEvent.ACTION_UP:
+ // mark the event as handled or it will be handled by system
+ // handling KEYCODE_BACK by system will call onBackPressed()
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
/**
* This method is called by SDL using JNI.
*/
@@ -1486,9 +1556,11 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
// see SDL_messagebox.h
if ((buttonFlags[i] & 0x00000001) != 0) {
mapping.put(KeyEvent.KEYCODE_ENTER, button);
+ mapping.put(KeyEvent.KEYCODE_BUTTON_A, button);
}
if ((buttonFlags[i] & 0x00000002) != 0) {
mapping.put(KeyEvent.KEYCODE_ESCAPE, button); /* API 11 */
+ mapping.put(KeyEvent.KEYCODE_BUTTON_B, button);
}
}
button.setText(buttonTexts[i]);
@@ -1841,21 +1913,7 @@ class DummyEdit extends View implements View.OnKeyListener {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
- /*
- * This handles the hardware keyboard input
- */
- if (event.getAction() == KeyEvent.ACTION_DOWN) {
- if (SDLActivity.isTextInputEvent(event)) {
- ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
- return true;
- }
- SDLActivity.onNativeKeyDown(keyCode);
- return true;
- } else if (event.getAction() == KeyEvent.ACTION_UP) {
- SDLActivity.onNativeKeyUp(keyCode);
- return true;
- }
- return false;
+ return SDLActivity.handleKeyEvent(v, keyCode, event, ic);
}
//
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
index 77cf176..dcd26d4 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
@@ -189,72 +189,8 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Key events
@Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
-
- int deviceId = event.getDeviceId();
- int source = event.getSource();
-
- if (source == InputDevice.SOURCE_UNKNOWN) {
- InputDevice device = InputDevice.getDevice(deviceId);
- if (device != null) {
- source = device.getSources();
- }
- }
-
-// if (event.getAction() == KeyEvent.ACTION_DOWN) {
-// Log.v("SDL", "key down: " + keyCode + ", deviceId = " + deviceId + ", source = " + source);
-// } else if (event.getAction() == KeyEvent.ACTION_UP) {
-// Log.v("SDL", "key up: " + keyCode + ", deviceId = " + deviceId + ", source = " + source);
-// }
-
- // Dispatch the different events depending on where they come from
- // Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
- // So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD
- //
- // Furthermore, it's possible a game controller has SOURCE_KEYBOARD and
- // SOURCE_JOYSTICK, while its key events arrive from the keyboard source
- // So, retrieve the device itself and check all of its sources
- if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
- // Note that we process events with specific key codes here
- if (event.getAction() == KeyEvent.ACTION_DOWN) {
- if (SDLControllerManager.onNativePadDown(deviceId, keyCode) == 0) {
- return true;
- }
- } else if (event.getAction() == KeyEvent.ACTION_UP) {
- if (SDLControllerManager.onNativePadUp(deviceId, keyCode) == 0) {
- return true;
- }
- }
- }
-
- if ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD) {
- if (event.getAction() == KeyEvent.ACTION_DOWN) {
- if (SDLActivity.isTextInputEvent(event)) {
- SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1);
- }
- SDLActivity.onNativeKeyDown(keyCode);
- return true;
- } else if (event.getAction() == KeyEvent.ACTION_UP) {
- SDLActivity.onNativeKeyUp(keyCode);
- return true;
- }
- }
-
- if ((source & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) {
- // on some devices key events are sent for mouse BUTTON_BACK/FORWARD presses
- // they are ignored here because sending them as mouse input to SDL is messy
- if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_FORWARD)) {
- switch (event.getAction()) {
- case KeyEvent.ACTION_DOWN:
- case KeyEvent.ACTION_UP:
- // mark the event as handled or it will be handled by system
- // handling KEYCODE_BACK by system will call onBackPressed()
- return true;
- }
- }
- }
-
- return false;
+ public boolean onKey(View v, int keyCode, KeyEvent event) {
+ return SDLActivity.handleKeyEvent(v, keyCode, event, null);
}
// Touch events
@@ -466,4 +402,4 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
return false;
}
-}
\ No newline at end of file
+}