123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- package com.amazon.lumberyard.input;
- import android.app.Activity;
- import android.content.Context;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.ViewGroup;
- import android.view.inputmethod.InputMethodManager;
- import android.view.View;
- ////////////////////////////////////////////////////////////////
- public class KeyboardHandler
- {
- // ----
- // KeyboardHandler (public)
- // ----
- public static native void SendUnicodeText(String unicodeText);
- ////////////////////////////////////////////////////////////////
- public KeyboardHandler(Activity activity)
- {
- m_activity = activity;
- m_inputManager = (InputMethodManager)m_activity.getSystemService(Context.INPUT_METHOD_SERVICE);
- }
- ////////////////////////////////////////////////////////////////
- public void ShowTextInput()
- {
- m_activity.runOnUiThread(new Runnable() {
- @Override
- public void run()
- {
- if (m_textView == null)
- {
- m_textView = new DummyTextView(m_activity);
- ViewGroup viewGroup = (ViewGroup)GetView();
- viewGroup.addView(m_textView);
- }
- m_textView.Show();
- m_inputManager.showSoftInput(m_textView, 0);
- }
- });
- }
- ////////////////////////////////////////////////////////////////
- public void HideTextInput()
- {
- if (m_textView != null)
- {
- m_activity.runOnUiThread(new Runnable() {
- @Override
- public void run()
- {
- m_inputManager.hideSoftInputFromWindow(m_textView.getWindowToken(), 0);
- m_textView.Hide();
- }
- });
- }
- }
- ////////////////////////////////////////////////////////////////
- public boolean IsShowing()
- {
- if (m_textView != null)
- {
- return m_textView.IsShowing();
- }
- return false;
- }
- // ----
- private class DummyTextView extends View
- {
- ////////////////////////////////////////////////////////////////
- public DummyTextView(Context context)
- {
- super(context);
- setFocusableInTouchMode(true);
- setFocusable(true);
- m_isShowing = false;
- }
- ////////////////////////////////////////////////////////////////
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- if (event.isPrintingKey())
- {
- int unicode = event.getUnicodeChar();
- String character = String.valueOf((char)unicode);
- Log.d(s_tag, String.format("OnKeyDown - Unicode: %s - Printed character: %s", unicode, character));
- SendUnicodeText(character);
- }
- return super.onKeyDown(keyCode, event);
- }
- ////////////////////////////////////////////////////////////////
- @Override
- public boolean onKeyMultiple(int keyCode, int count, KeyEvent event)
- {
- if(event.getAction() == KeyEvent.ACTION_MULTIPLE && keyCode == KeyEvent.KEYCODE_UNKNOWN)
- {
- String text = event.getCharacters();
- Log.d(s_tag, String.format("onKeyMultiple - Text: %s", text));
- if (text != null)
- {
- SendUnicodeText(text);
- }
- }
- return super.onKeyMultiple(keyCode, count, event);
- }
- ////////////////////////////////////////////////////////////////
- @Override
- public boolean onKeyPreIme(int keyCode, KeyEvent event)
- {
- if (keyCode == KeyEvent.KEYCODE_BACK)
- {
- Hide();
- }
- return super.onKeyPreIme(keyCode, event);
- }
- ////////////////////////////////////////////////////////////////
- public boolean IsShowing()
- {
- return m_isShowing;
- }
- ////////////////////////////////////////////////////////////////
- public void Show()
- {
- m_windowFlags = GetView().getSystemUiVisibility();
- setVisibility(View.VISIBLE);
- requestFocus();
- m_isShowing = true;
- }
- ////////////////////////////////////////////////////////////////
- public void Hide()
- {
- setVisibility(View.GONE);
- m_isShowing = false;
- GetView().setSystemUiVisibility(m_windowFlags);
- }
- // ----
- private boolean m_isShowing;
- private int m_windowFlags;
- }
- // ----
- ////////////////////////////////////////////////////////////////
- private View GetView()
- {
- return m_activity.getWindow().getDecorView();
- }
- // ----
- private static final String s_tag = "KeyboardHandler";
- private Activity m_activity;
- private InputMethodManager m_inputManager;
- private DummyTextView m_textView;
- }
|