You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't summon soft_input with egui + winit + wgpu #44
Googling I found a lot complains about ANativeActivity_showSoftInput()
There is another way to do it — via JNI call. But it requires access to native_activity, which is currently hidden.
Good Android IME support is something that still needs work in Egui, Winit and android-activity unfortunately but is something I'd like to make some progress on.
I would probably recommend first looking at the GameActivity backend if you are looking to enable soft keyboard support since we should be able to utilize the GameText bindings in that backend to also support the InputMethod protocol.
Maybe you could take a look over some of the above branches to see if they are a helpful starting point.
At least in terms of using JNI to access the Activity you can use the ndk_context crate to get to the Activity, e.g. something like:
let ctx = ndk_context::android_context();let jvm_ptr = ctx.vm();let jvm = unsafe{ jni::JavaVM::from_raw(jvm_ptr.cast()).expect("Expected to find JVM via ndk_context crate")};let env = jvm.attach_current_thread_permanently().unwrap();let activity_ptr = ctx.context();let activity = unsafe{ jni::objects::JObject::from_raw(activity_ptr as jni::sys::jobject)};
ndk_context provides a general (also usable for native Rust libraries that don't know whether they are part of a standalone Rust application) way to access a JavaVM on Android and an Activity/Context reference. android-activity initializes the state for ndk_context before your android_main() runs.
I've at least smoke tested that show_soft_input has worked for me in the past when working with experimental branches to start enabling better IME support so I'm not currently sure why nothing was happening for @inferrna when calling show_soft_input via the linked example code.
I have a similar issue, I do use show_soft_input (in a native activity) but nothing happens. I just get the following message: InputMethodManager: Ignoring showSoftInput() as view=android.app.NativeActivity$NativeContentView{a4db4 V.ED..... ......ID 0,0-1080,2084} is not served.
and in game_activity/mod.rs, i put the check of textInputState before the check of android_app_swap_input_buffers. because the second is null, when the first is valid.
I'm also looking into keyboard support on android (and iOS). Based on @rib s work I have been able to get pretty decent support working on android 🥳
Screen_Recording_20230708_144153.mp4
(opening and closing the keyboard also works, even if it's not in the video)
Unfortunately for this to work, I had to add completely new events to winit, basically passing the TextInputState struct from android activity all the way through to egui, and then handling the logic there.
This is not ideal, but I'm not sure how much we could improve on this, with the way the GameTextInput works.
The most difficult part was finding the right places in egui to send the correct state updates when selecting a new text field or updating the selection.
If you'd like to try this you can use my relevant branches by adding this to your Cargo.toml (your project must be using game-activity for keyboard input to work):
For this to feel native on android what's still missing would be egui showing the composition range with underlined text instead of the blue selection highlight. But I think that should be pretty straightforward.
Also missing of course is to update the insets / resize the window when the keyboard opens.
Next, I'll look into iOS input support, I already have opening the keyboard and typing working but I'll also try to get autocomplete and suggestions.
Reacted by Piotr Podusowski, bilabila and Robert Bragg
Reacted by ardocrat and Robert Bragg
Reacted by William Casarin, Snail with tea and Lauda
show_soft_input() is still not working in NativeActivity with winit 0.29.10 (android-activity 0.5.0), nothing happens, no error on Android 8.1 or Android 11. I call it in android_main and after a touch event.
On Android 13 I get:
InputMethodManager: Ignoring showSoftInput() as view=android.app.NativeActivity$NativeContentView{454c3ca V.ED..... ......ID 0,0-1080,2023} is not served.
I suspect this has to do with using a GPU surface.
I will see if the window flags are correct (if I manage to access them).
GameActivity examples (rust-android-examples) immediately crash on my devices (Android 8.1, Android 11 and Android 13) with various errors, so I can't consider that for production.
On Android 13 the error is undefined symbol: ANativeActivity_onCreate; this may have to do with cargo apk/xbuild, they're quite limited.
Since this repository was migrated/transferred to the rust-mobile organization (whew you created the issue), then Rib forked the repo again for personal contributions, GitHub no longer redirects. Instead, replace rib with rust-mobile to find the right link: #24
Although a few more general text input topics came up in the discussion here, I think the original issue here (show_soft_input() wasn't working with the native-activity backend, due to issues with ANativeActivity_showSoftInput) has been fixed by #178 (and tested with egui + winit)
It's perhaps worth noting here that the NativeActivity backend is not expected to ever get full InputConnection / IME support and so there's quite a significant limitation on what kind of text input can be supported, but with some keyboards you can at least get physical KeyEvents for basic ascii input.
For InputConnection / IME support it's recommended to use the game-activity backend if possible.
Activity
rib commentedon Nov 29, 2022
Hi,
Good Android IME support is something that still needs work in Egui, Winit and android-activity unfortunately but is something I'd like to make some progress on.
You can see an initial attempt I made at enabling input method support in
android-activityhere: https://github.com/rib/android-activity/pull/24 but I hit some odd input issues at the time which I didn't have a chance to debug.More recently I also looked at creating initial branches for Winit + Egui to experiment with this too:
https://github.com/rib/winit/tree/android-activity-ime-events
https://github.com/rib/egui/tree/android-winit-ime-support
I would probably recommend first looking at the
GameActivitybackend if you are looking to enable soft keyboard support since we should be able to utilize theGameTextbindings in that backend to also support the InputMethod protocol.Maybe you could take a look over some of the above branches to see if they are a helpful starting point.
At least in terms of using JNI to access the
Activityyou can use thendk_contextcrate to get to theActivity, e.g. something like:ndk_contextprovides a general (also usable for native Rust libraries that don't know whether they are part of a standalone Rust application) way to access aJavaVMon Android and anActivity/Contextreference.android-activityinitializes the state forndk_contextbefore yourandroid_main()runs.tkkcc commentedon Dec 26, 2022
There is an implement in miniquad/miniquad, use a normal Activity.
not-fl3/macroquad#526
https://github.com/not-fl3/miniquad/search?q=showKeyboard
rib commentedon Dec 27, 2022
@tkkcc if you just want to show the keyboard you should be able to use https://docs.rs/android-activity/0.4.0/android_activity/struct.AndroidApp.html#method.show_soft_input which should be equivalent to what macroquad does.
...but, unfortunately, that won't be very useful without also having input method support too.
macroquad has a custom subclass of the
Activityclass which effectively lets you call theInputMethodManager::showSoftInput(https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#showSoftInput(android.view.View,%20int)) API via JNIIn
android-activitytheshow_input_methodAPI should do be equivalent to what macroquad does:When android-activity is built for
NativeActivityIt calls an NDK API calledANativeActivity_showSoftInputwhich will end up calling an internalandroid_NativeActivity_setWindowFlags(https://github.com/aosp-mirror/platform_frameworks_base/blob/ffeb11af1c22a1b8e2af57aac338e1fb57261ec5/core/jni/android_app_NativeActivity.cpp#L202) and using some IPC to invoke this: https://github.com/aosp-mirror/platform_frameworks_base/blob/ffeb11af1c22a1b8e2af57aac338e1fb57261ec5/core/jni/android_app_NativeActivity.cpp#L249 and that will call ashowImemethod forNativeActivity.java- similar to the macroquad subclass ofActivity: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/NativeActivity.java#329When android-activity is built for
GameActivitytheshow_soft_inputwill call an internalGameActivity_showSoftInputthat uses IPC like in NativeActivity to send aCMD_SHOW_SOFT_INPUTcommand that then callsGameTextInput_showImewhich then calls a JNI method calledsetSoftKeyboardActivethat also does the same thing as macroquad: https://android.googlesource.com/platform/frameworks/opt/gamesdk/+/refs/heads/master/game-text-input/src/main/java/com/google/androidgamesdk/gametextinput/InputConnection.java#113I've at least smoke tested that
show_soft_inputhas worked for me in the past when working with experimental branches to start enabling better IME support so I'm not currently sure why nothing was happening for @inferrna when callingshow_soft_inputvia the linked example code.LinusDikomey commentedon Jun 16, 2023
I have a similar issue, I do use
show_soft_input(in a native activity) but nothing happens. I just get the following message:InputMethodManager: Ignoring showSoftInput() as view=android.app.NativeActivity$NativeContentView{a4db4 V.ED..... ......ID 0,0-1080,2084} is not served.tkkcc commentedon Jun 16, 2023
agdk-eframe demo is able to show and hide input method via button
code
i don't know if clone an AndroidApp will cause something wrong. i don't know how to access options.event_loop_builder from ui update fn.
tkkcc commentedon Jun 17, 2023
tmp.mp4
now i can achieve some level (very buggy) of input experience. based on agdk-eframe demo, picking the last commit of
and in
game_activity/mod.rs, i put the check oftextInputStatebefore the check ofandroid_app_swap_input_buffers. because the second is null, when the first is valid.issues:
lucasmerlin commentedon Jul 8, 2023
I'm also looking into keyboard support on android (and iOS). Based on @rib s work I have been able to get pretty decent support working on android 🥳
Screen_Recording_20230708_144153.mp4
(opening and closing the keyboard also works, even if it's not in the video)
Unfortunately for this to work, I had to add completely new events to winit, basically passing the TextInputState struct from android activity all the way through to egui, and then handling the logic there.
This is not ideal, but I'm not sure how much we could improve on this, with the way the GameTextInput works.
The most difficult part was finding the right places in egui to send the correct state updates when selecting a new text field or updating the selection.
If you'd like to try this you can use my relevant branches by adding this to your Cargo.toml (your project must be using game-activity for keyboard input to work):
These are the relevant branches:
https://github.com/lucasmerlin/winit/tree/mobile_ime_support
https://github.com/lucasmerlin/egui/tree/mobile_ime_support
https://github.com/lucasmerlin/android-activity/tree/ime_support (based on the work of @rib)
For this to feel native on android what's still missing would be egui showing the composition range with underlined text instead of the blue selection highlight. But I think that should be pretty straightforward.
Also missing of course is to update the insets / resize the window when the keyboard opens.
Next, I'll look into iOS input support, I already have opening the keyboard and typing working but I'll also try to get autocomplete and suggestions.
fr-an-k commentedon Jan 21, 2024
show_soft_input() is still not working in NativeActivity with winit 0.29.10 (android-activity 0.5.0), nothing happens, no error on Android 8.1 or Android 11. I call it in android_main and after a touch event.
On Android 13 I get:
InputMethodManager: Ignoring showSoftInput() as view=android.app.NativeActivity$NativeContentView{454c3ca V.ED..... ......ID 0,0-1080,2023} is not served.
I suspect this has to do with using a GPU surface.
I will see if the window flags are correct (if I manage to access them).
https://github.com/rib/android-activity/pull/24 is a dead link.
GameActivity examples (rust-android-examples) immediately crash on my devices (Android 8.1, Android 11 and Android 13) with various errors, so I can't consider that for production.
On Android 13 the error is undefined symbol: ANativeActivity_onCreate; this may have to do with cargo apk/xbuild, they're quite limited.
I'll try to do it through JNI.
MarijnS95 commentedon Jan 21, 2024
Since this repository was migrated/transferred to the
rust-mobileorganization (whew you created the issue), then Rib forked the repo again for personal contributions, GitHub no longer redirects. Instead, replaceribwithrust-mobileto find the right link: #24fr-an-k commentedon Jan 22, 2024
Ok, thanks.
It looks like a View is needed (created in onCreate) to request focus first, as done here:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/NativeActivity.java
Shouldn't ANativeActivity_onCreate simply call it's super.onCreate?
MarijnS95 commentedon Jan 22, 2024
Why would that be required, given that
NativeActivityexists to not have to write Java/JNI (to at least get a basic app going)?The Java layer that powers
NativeActivitydoes callsuper.onCreate:https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/app/NativeActivity.java;l=180;drc=5d123b67756dffcfdebdb936ab2de2b29c799321
It's unclear what line your link is supposed to link to.
fr-an-k commentedon Jan 23, 2024
Ok, I see.
I managed to get GameActivity to work with soft input (but not the latest winit/wgpu versions) on Android 8.1.
I guess you don't get soft input on a window surface; you'd need a view.
It might be possible to achieve this with NativeActivity if you create a SurfaceView in the same way that is done for GameActivity.
But I guess that's kind of deprecated and people will just have to use GameActivity for soft input on a GPU surface.
ibaryshnikov commentedon Jul 10, 2024
It works with NativeActivity if you use
getDecorView, here's an example which worked for mehttps://github.com/mvvvv/StereoKit-rust/blob/master/src/tools/os_api.rs#L196
Or it may be a few lines of Java, something like
Still, there's a number of issues which prevent it to be usable in current state:
android:windowSoftInputMode="adjustResize"propertyrib commentedon Mar 20, 2026
Although a few more general text input topics came up in the discussion here, I think the original issue here (
show_soft_input()wasn't working with the native-activity backend, due to issues withANativeActivity_showSoftInput) has been fixed by #178 (and tested with egui + winit)It's perhaps worth noting here that the NativeActivity backend is not expected to ever get full InputConnection / IME support and so there's quite a significant limitation on what kind of text input can be supported, but with some keyboards you can at least get physical KeyEvents for basic ascii input.
For
InputConnection/ IME support it's recommended to use thegame-activitybackend if possible.