Add experimental support for system screen keyboard on android.

Disabled by default, needs screen_keyboard = 3 in config.xml
This commit is contained in:
Deve 2018-12-01 00:43:17 +01:00
parent bf76a2a1e2
commit ddbe42680d
5 changed files with 103 additions and 3 deletions

View File

@ -1320,6 +1320,82 @@ void CIrrDeviceAndroid::hideNavBar(ANativeActivity* activity)
} }
} }
void CIrrDeviceAndroid::showKeyboard(bool show)
{
bool was_detached = false;
JNIEnv* env = NULL;
jint status = Android->activity->vm->GetEnv((void**)&env, JNI_VERSION_1_6);
if (status == JNI_EDETACHED)
{
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6;
args.name = "NativeThread";
args.group = NULL;
status = Android->activity->vm->AttachCurrentThread(&env, &args);
was_detached = true;
}
if (status != JNI_OK)
{
os::Printer::log("Cannot show keyboard.", ELL_DEBUG);
return;
}
jobject activity_obj = Android->activity->clazz;
jclass activity_class = env->GetObjectClass(activity_obj);
jclass context = env->FindClass("android/content/Context");
jfieldID im_service_field = env->GetStaticFieldID(context,
"INPUT_METHOD_SERVICE", "Ljava/lang/String;");
jobject im_service_obj = env->GetStaticObjectField(context,
im_service_field);
jclass im_manager = env->FindClass(
"android/view/inputmethod/InputMethodManager");
jmethodID get_system_service = env->GetMethodID( activity_class,
"getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jobject im_manager_obj = env->CallObjectMethod(activity_obj,
get_system_service, im_service_obj);
jmethodID get_window = env->GetMethodID(activity_class, "getWindow",
"()Landroid/view/Window;");
jobject window_obj = env->CallObjectMethod(activity_obj, get_window);
jclass window = env->FindClass("android/view/Window");
jmethodID get_decor_view = env->GetMethodID(window, "getDecorView",
"()Landroid/view/View;");
jobject decor_view_obj = env->CallObjectMethod(window_obj, get_decor_view);
if (show)
{
jmethodID show_soft_input = env->GetMethodID(im_manager,
"showSoftInput", "(Landroid/view/View;I)Z");
jint flags = 0;
jboolean result = env->CallBooleanMethod(im_manager_obj,
show_soft_input, decor_view_obj, flags);
}
else
{
jclass view = env->FindClass("android/view/View");
jmethodID get_window_token = env->GetMethodID(view, "getWindowToken",
"()Landroid/os/IBinder;");
jobject token = env->CallObjectMethod(decor_view_obj, get_window_token);
jmethodID hide_soft_input = env->GetMethodID(im_manager,
"hideSoftInputFromWindow", "(Landroid/os/IBinder;I)Z");
jint flags = 0;
jboolean result = env->CallBooleanMethod(im_manager_obj,
hide_soft_input, token, flags);
}
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
}
int CIrrDeviceAndroid::getRotation() int CIrrDeviceAndroid::getRotation()
{ {
bool was_detached = false; bool was_detached = false;

View File

@ -74,6 +74,7 @@ namespace irr
virtual bool isGyroscopeActive(); virtual bool isGyroscopeActive();
virtual bool isGyroscopeAvailable(); virtual bool isGyroscopeAvailable();
virtual void setTextInputEnabled(bool enabled) {TextInputEnabled = enabled;} virtual void setTextInputEnabled(bool enabled) {TextInputEnabled = enabled;}
virtual void showKeyboard(bool show);
class CCursorControl : public gui::ICursorControl class CCursorControl : public gui::ICursorControl
{ {

View File

@ -529,7 +529,7 @@ namespace UserConfigParams
PARAM_DEFAULT( IntUserConfigParam(0, "screen_keyboard_mode", PARAM_DEFAULT( IntUserConfigParam(0, "screen_keyboard_mode",
&m_multitouch_group, &m_multitouch_group,
"Screen keyboard mode: 0 = disabled, 1 = enabled if no hardware " "Screen keyboard mode: 0 = disabled, 1 = enabled if no hardware "
"keyboard, 2 = always enabled") ); "keyboard, 2 = always enabled, 3 = android keyboard (experimental)") );
PARAM_PREFIX BoolUserConfigParam m_hidpi_enabled PARAM_PREFIX BoolUserConfigParam m_hidpi_enabled
PARAM_DEFAULT( BoolUserConfigParam(false, "hidpi_enabled", PARAM_DEFAULT( BoolUserConfigParam(false, "hidpi_enabled",

View File

@ -415,7 +415,7 @@ bool ScreenKeyboard::onEscapePressed()
*/ */
bool ScreenKeyboard::shouldUseScreenKeyboard() bool ScreenKeyboard::shouldUseScreenKeyboard()
{ {
bool use_screen_keyboard = UserConfigParams::m_screen_keyboard > 1; bool use_screen_keyboard = UserConfigParams::m_screen_keyboard == 2;
#ifdef ANDROID #ifdef ANDROID
if (UserConfigParams::m_screen_keyboard == 1) if (UserConfigParams::m_screen_keyboard == 1)

View File

@ -609,6 +609,15 @@ bool CGUIEditBox::processKey(const SEvent& event)
} }
else else
{ {
#ifdef ANDROID
if (irr_driver->getDevice()->getType() == irr::EIDT_ANDROID &&
UserConfigParams::m_screen_keyboard == 3)
{
CIrrDeviceAndroid* dl = dynamic_cast<CIrrDeviceAndroid*>(
irr_driver->getDevice());
dl->showKeyboard(false);
}
#endif
sendGuiEvent( EGET_EDITBOX_ENTER ); sendGuiEvent( EGET_EDITBOX_ENTER );
} }
break; break;
@ -1296,6 +1305,17 @@ bool CGUIEditBox::processMouse(const SEvent& event)
openScreenKeyboard(); openScreenKeyboard();
return true; return true;
} }
#ifdef ANDROID
else if (UserConfigParams::m_screen_keyboard == 3)
{
if (irr_driver->getDevice()->getType() == irr::EIDT_ANDROID)
{
CIrrDeviceAndroid* dl = dynamic_cast<CIrrDeviceAndroid*>(
irr_driver->getDevice());
dl->showKeyboard(true);
}
}
#endif
else else
{ {
// move cursor // move cursor
@ -1756,6 +1776,9 @@ void CGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadW
void CGUIEditBox::openScreenKeyboard() void CGUIEditBox::openScreenKeyboard()
{ {
if (UserConfigParams::m_screen_keyboard == 3)
return;
if (GUIEngine::ScreenKeyboard::getCurrent() != NULL) if (GUIEngine::ScreenKeyboard::getCurrent() != NULL)
return; return;