Integrating Input SDK

The Input SDK provides a unified interface that allows players to easily find the mouse and keyboard bindings for your game on Google Play Games for PC, thereby enhancing their gaming experience.

Prerequisites

If you are using Input SDK on a new Android build, simply check the Input SDK option in the build panel.

If the Android project was built previously, you will need to manually add the following code to the native/engine/android/app/build.gradle file.

  1. dependencies {
  2. implementation 'com.google.android.libraries.play.games:inputmapping:1.0.0-beta'
  3. ...
  4. }

Create a the class named MyInputMapProvider

Create a new file MyInputMapProvider.java which contains the following code:

  1. public class MyInputMapProvider implements InputMappingProvider {
  2. public enum InputEventIds {
  3. JUMP,
  4. LEFT,
  5. RIGHT,
  6. USE,
  7. SPECIAL_JUMP,
  8. SPECIAL_DUCK,
  9. CMB_MOVE,
  10. CMB_DASH,
  11. CMB_WAYPOINT
  12. }
  13. @Override
  14. public InputMap onProvideInputMap() {
  15. InputAction jumpInputAction = InputAction.create(
  16. getString(R.string.key_jump),
  17. InputEventIds.JUMP.ordinal(),
  18. InputControls.create(
  19. Collections.singletonList(KeyEvent.KEYCODE_SPACE),
  20. Collections.emptyList()
  21. )
  22. );
  23. InputAction leftAction = InputAction.create(
  24. getString(R.string.key_Left),
  25. InputEventIds.LEFT.ordinal(),
  26. InputControls.create(
  27. Collections.singletonList(KeyEvent.KEYCODE_DPAD_LEFT),
  28. Collections.emptyList()
  29. )
  30. );
  31. InputAction rightAction = InputAction.create(
  32. getString(R.string.key_Right),
  33. InputEventIds.RIGHT.ordinal(),
  34. InputControls.create(
  35. Collections.singletonList(KeyEvent.KEYCODE_DPAD_RIGHT),
  36. Collections.emptyList()
  37. )
  38. );
  39. InputAction cmbMove = InputAction.create(
  40. getString(R.string.key_Move),
  41. InputEventIds.CMB_MOVE.ordinal(),
  42. InputControls.create(
  43. Collections.emptyList(),
  44. Collections.singletonList(InputControls.MOUSE_RIGHT_CLICK)
  45. )
  46. );
  47. InputAction cmbDash = InputAction.create(
  48. getString(R.string.key_Dash),
  49. InputEventIds.CMB_DASH.ordinal(),
  50. InputControls.create(
  51. Arrays.asList(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.KEYCODE_SPACE),
  52. Collections.emptyList()
  53. )
  54. );
  55. InputAction cmbWaypoint = InputAction.create(
  56. getString(R.string.key_Waypoint),
  57. InputEventIds.CMB_WAYPOINT.ordinal(),
  58. InputControls.create(
  59. Collections.singletonList(KeyEvent.KEYCODE_SHIFT_LEFT),
  60. Collections.singletonList(InputControls.MOUSE_RIGHT_CLICK)
  61. )
  62. );
  63. InputGroup movementInputGroup = InputGroup.create(
  64. getString(R.string.key_BasicMove),
  65. Arrays.asList(jumpInputAction, leftAction, rightAction, cmbMove, cmbDash, cmbWaypoint)
  66. );
  67. return InputMap.create(
  68. Arrays.asList(movementInputGroup),
  69. MouseSettings.create(true, true)
  70. );
  71. }
  72. }

Add binding names for keys

Define key code names in the res/values/strings.xml file to enable internationalization of the app. These descriptions will be used in the previous steps to provide context and clarity for the user when mapping input events to corresponding actions.

  1. <resources>
  2. ...
  3. <string name="key_jump">Jump</string>
  4. <string name="key_Left">Left</string>
  5. <string name="key_Right">Right</string>
  6. <string name="key_Move">Move</string>
  7. <string name="key_Dash">Dash</string>
  8. <string name="key_Waypoint">Add Waypoint</string>
  9. <string name="key_BasicMove">Basic Movement</string>
  10. </resources>

Register InputMapping

To register the InputMapping, modify AppActivity.java located in native\engine\android\app\src\main\cocos\game by adding the necessary code.

  1. ...
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. ...
  6. InputMappingClient inputMappingClient = Input.getInputMappingClient(this);
  7. inputMappingClient.setInputMappingProvider(new MyInputMapProvider());
  8. }
  9. ...
  10. @Override
  11. protected void onDestroy() {
  12. InputMappingClient inputMappingClient = Input.getInputMappingClient(this);
  13. inputMappingClient.clearInputMappingProvider();
  14. super.onDestroy();
  15. ...
  16. }

Test Result

To bring up the page, use Shift + Tab while running on the HPE emulator provided by Google Play Games.

key codes

Refer to the official documentation of the Input SDK for more details.