Opening/closing on-screen keyboard on Android

If there is something important you think is missing in the current version of ZGameEditor then you can post a feature request here!

Moderator: Moderators

User avatar
Rado1
Posts: 770
Joined: Wed May 05, 2010 12:16 pm

Opening/closing on-screen keyboard on Android

Postby Rado1 » Mon Feb 10, 2014 9:31 pm

Is there any chance to support opening/closing on-screen keyboard on Android and to process KeyPress events from it? I would like to use it as possible and usual input method for typing player names, etc.

I could open/close the keyboard from external library (by use of JNI), but not sure whether KeyPress can read the input from it. Providing some support to this functionality in ZGE, e.g., some function(s), would be quite helpful.

BTW I tested also to HW keyboard and mouse connected via USB OTG and KeyPress did not give any inputs; except of releasing mouse which produced { (LMB press) event.

User avatar
VilleK
Site Admin
Posts: 1920
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Postby VilleK » Tue Feb 11, 2014 8:29 am

I would expect an external keyboard to send KeyDown messages. Try adding a log message in onKeyDown method in Zge.java just to see if it is called at all. Maybe it is just the individual keycodes that is sent to ZGE incorrectly, then it should be easy to fix.

About open/close keyboard I'm not sure how it is done. I found this thread but it does not mention JNI: http://stackoverflow.com/questions/5593 ... mmatically

User avatar
Rado1
Posts: 770
Joined: Wed May 05, 2010 12:16 pm

Postby Rado1 » Tue Feb 11, 2014 10:03 am

VilleK wrote:Try adding a log message in onKeyDown method in Zge.java just to see if it is called at all. Maybe it is just the individual keycodes that is sent to ZGE incorrectly, then it should be easy to fix.

Ok, I'll test it this evening again. In my previous test I just used arrow keys (<>^_).

VilleK wrote:About open/close keyboard I'm not sure how it is done. I found this thread but it does not mention JNI: http://stackoverflow.com/questions/5593 ... mmatically

This thread is about creating applications in Java. To switch soft keyboard on/off during runtime from NDK, one way is to use JNI; as discussed in [url]https://groups.google.com/forum/#!topic/android-ndk/ImpwzH_PItc[/url]. Maybe, there is also another way using NDK native activity...

Anyway, I tried to set android:windowSoftInputMode="stateVisible" in AndroidManifest.xml and did not work for ZGE on Android 4.0. That's maybe it was added only to API level 14. But this is just the means for testing; in the final solution I would like to control showing/hiding soft keyboard by application.

User avatar
Rado1
Posts: 770
Joined: Wed May 05, 2010 12:16 pm

Postby Rado1 » Tue Feb 11, 2014 9:56 pm

Interesting observations:

1. If physical keyboard is not connected by OTG cable, android:windowSoftInputMode="stateVisible" works and opens a soft keyboard on Android 4.4.2 (API 19).

2. ZGE is not able to take events from soft keyboard anyway. I tested several letter keys.

3. If physical keyboard is available, ZGE on Android can react on events, but in different way than on Windows: it takes upper-case / lower-case of KeyPress.Keys into account, so key A means you must press Shift+A, a reacts to key A, key < requires pressing Shift+, (which is < symbol on my keyboard) and not left arrow, etc.

4. If physical keyboard is connected, the android:windowSoftInputMode="stateVisible" does not cause opening the soft keyboard.

5. Mouse connected by OTG cable produces clicks/touches in a normal way, so ZGE can process them without problems.

Usage of physical keyboard on Android devices is quite rare, so developers of ZGE applications should not rely on this feature too much. It's more-or-less a curiosity.

What can be improved in ZGE is the point 2. I can prepare an external library for opening/closing soft keyboard, but if ZGE is not able to react to it, the effort is useless.

BTW for my current project, I'm tempted to implement my own "virtual keyboard" within ZGE application. This would give me full control ... but also would require some more effort. Maybe soft keyboard would be more standard solution...(?) What do you think?

User avatar
VilleK
Site Admin
Posts: 1920
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Postby VilleK » Wed Feb 12, 2014 8:49 am

It is strange if the soft keyboard does not send key events to the active application.

I found this: http://stackoverflow.com/questions/1127 ... -key-press

Maybe you can try the solution suggested there and tell me if it works?

User avatar
Rado1
Posts: 770
Joined: Wed May 05, 2010 12:16 pm

Postby Rado1 » Wed Feb 12, 2014 11:11 pm

Inspired by the previous code example, I extended Zge.java by:

Code: Select all

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
      int keyaction = event.getAction();

      if(keyaction == KeyEvent.ACTION_DOWN) {
        NativeKeydown(event.getUnicodeChar());
        Log.i("key down", String.valueOf(event.getUnicodeChar()));
      } else if(keyaction == KeyEvent.ACTION_UP) {
        NativeKeyup(event.getUnicodeChar());
        Log.i("key up", String.valueOf(event.getUnicodeChar()));
      }

      return super.dispatchKeyEvent(event);
    }


The log shows correct Unicode codes of pressed and released keys, but ZGE application does not react to them. Is calling of native methods correct? I Attach the example project with modified sources and AndroidManifest.xml.

Tested on Google Keyboard and Hacker's Keyboard. Both gave the same results.
Attachments
com.rado1.test.zip
testing ZGE project + Android sources
(5.9 KiB) Downloaded 132 times

User avatar
VilleK
Site Admin
Posts: 1920
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Postby VilleK » Thu Feb 13, 2014 11:40 am

Looks correct to me.

Please try this, press capital "A" key, and tell me the output:

Log.i("key down", String.valueOf(event.getUnicodeChar()) + " : " + Integer.toString(event.getUnicodeChar()));

User avatar
Rado1
Posts: 770
Joined: Wed May 05, 2010 12:16 pm

Postby Rado1 » Thu Feb 13, 2014 12:07 pm

Shift + A gives:
key down 0
key down 65
key up 65
key up 0

I think 0 is for shift key.

A (small a) gives:
key down 97
key up 97

The codes are correct.

User avatar
Rado1
Posts: 770
Joined: Wed May 05, 2010 12:16 pm

Postby Rado1 » Thu Feb 13, 2014 8:08 pm

Just for curiosity, I created a small external library which is able to toggle soft keyboard from ZGE. It works also on old Android versions; it is compiled for android-8. Currently, in order to achieve better flexibility in testing, it is implemented in the way that calls static Java method from ZgeActivity.java by JNI. So in order to run ZGE application properly, you need to replace ZgeActivity.java. Original AndroidManifest.xml file (as generated by ZGE) should be used for compilation; not the modified one as mentioned in my previous post.

The attachment contains all necessary files, including a testing project - toggles the soft keyboard by tapping anywhere.
Attachments
test.zip
library for switching soft keyboard + testing project + modified ZgeActivity.java
(7.78 KiB) Downloaded 144 times


Return to “Feature requests”

Who is online

Users browsing this forum: No registered users and 1 guest