KeyboardHandler.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. package com.amazon.lumberyard.input;
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.util.Log;
  12. import android.view.KeyEvent;
  13. import android.view.ViewGroup;
  14. import android.view.inputmethod.InputMethodManager;
  15. import android.view.View;
  16. ////////////////////////////////////////////////////////////////
  17. public class KeyboardHandler
  18. {
  19. // ----
  20. // KeyboardHandler (public)
  21. // ----
  22. public static native void SendUnicodeText(String unicodeText);
  23. ////////////////////////////////////////////////////////////////
  24. public KeyboardHandler(Activity activity)
  25. {
  26. m_activity = activity;
  27. m_inputManager = (InputMethodManager)m_activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  28. }
  29. ////////////////////////////////////////////////////////////////
  30. public void ShowTextInput()
  31. {
  32. m_activity.runOnUiThread(new Runnable() {
  33. @Override
  34. public void run()
  35. {
  36. if (m_textView == null)
  37. {
  38. m_textView = new DummyTextView(m_activity);
  39. ViewGroup viewGroup = (ViewGroup)GetView();
  40. viewGroup.addView(m_textView);
  41. }
  42. m_textView.Show();
  43. m_inputManager.showSoftInput(m_textView, 0);
  44. }
  45. });
  46. }
  47. ////////////////////////////////////////////////////////////////
  48. public void HideTextInput()
  49. {
  50. if (m_textView != null)
  51. {
  52. m_activity.runOnUiThread(new Runnable() {
  53. @Override
  54. public void run()
  55. {
  56. m_inputManager.hideSoftInputFromWindow(m_textView.getWindowToken(), 0);
  57. m_textView.Hide();
  58. }
  59. });
  60. }
  61. }
  62. ////////////////////////////////////////////////////////////////
  63. public boolean IsShowing()
  64. {
  65. if (m_textView != null)
  66. {
  67. return m_textView.IsShowing();
  68. }
  69. return false;
  70. }
  71. // ----
  72. private class DummyTextView extends View
  73. {
  74. ////////////////////////////////////////////////////////////////
  75. public DummyTextView(Context context)
  76. {
  77. super(context);
  78. setFocusableInTouchMode(true);
  79. setFocusable(true);
  80. m_isShowing = false;
  81. }
  82. ////////////////////////////////////////////////////////////////
  83. @Override
  84. public boolean onKeyDown(int keyCode, KeyEvent event)
  85. {
  86. if (event.isPrintingKey())
  87. {
  88. int unicode = event.getUnicodeChar();
  89. String character = String.valueOf((char)unicode);
  90. Log.d(s_tag, String.format("OnKeyDown - Unicode: %s - Printed character: %s", unicode, character));
  91. SendUnicodeText(character);
  92. }
  93. return super.onKeyDown(keyCode, event);
  94. }
  95. ////////////////////////////////////////////////////////////////
  96. @Override
  97. public boolean onKeyMultiple(int keyCode, int count, KeyEvent event)
  98. {
  99. if(event.getAction() == KeyEvent.ACTION_MULTIPLE && keyCode == KeyEvent.KEYCODE_UNKNOWN)
  100. {
  101. String text = event.getCharacters();
  102. Log.d(s_tag, String.format("onKeyMultiple - Text: %s", text));
  103. if (text != null)
  104. {
  105. SendUnicodeText(text);
  106. }
  107. }
  108. return super.onKeyMultiple(keyCode, count, event);
  109. }
  110. ////////////////////////////////////////////////////////////////
  111. @Override
  112. public boolean onKeyPreIme(int keyCode, KeyEvent event)
  113. {
  114. if (keyCode == KeyEvent.KEYCODE_BACK)
  115. {
  116. Hide();
  117. }
  118. return super.onKeyPreIme(keyCode, event);
  119. }
  120. ////////////////////////////////////////////////////////////////
  121. public boolean IsShowing()
  122. {
  123. return m_isShowing;
  124. }
  125. ////////////////////////////////////////////////////////////////
  126. public void Show()
  127. {
  128. m_windowFlags = GetView().getSystemUiVisibility();
  129. setVisibility(View.VISIBLE);
  130. requestFocus();
  131. m_isShowing = true;
  132. }
  133. ////////////////////////////////////////////////////////////////
  134. public void Hide()
  135. {
  136. setVisibility(View.GONE);
  137. m_isShowing = false;
  138. GetView().setSystemUiVisibility(m_windowFlags);
  139. }
  140. // ----
  141. private boolean m_isShowing;
  142. private int m_windowFlags;
  143. }
  144. // ----
  145. ////////////////////////////////////////////////////////////////
  146. private View GetView()
  147. {
  148. return m_activity.getWindow().getDecorView();
  149. }
  150. // ----
  151. private static final String s_tag = "KeyboardHandler";
  152. private Activity m_activity;
  153. private InputMethodManager m_inputManager;
  154. private DummyTextView m_textView;
  155. }