Page 1 of 1

Opening/closing on-screen keyboard on Android

Posted: Mon Feb 10, 2014 9:31 pm
by Rado1
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.

Posted: Tue Feb 11, 2014 8:29 am
by VilleK
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

Posted: Tue Feb 11, 2014 10:03 am
by Rado1
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 https://groups.google.com/forum/#!topic ... mpwzH_PItc. 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.

Posted: Tue Feb 11, 2014 9:56 pm
by Rado1
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?

Posted: Wed Feb 12, 2014 8:49 am
by VilleK
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?

Posted: Wed Feb 12, 2014 11:11 pm
by Rado1
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.

Posted: Thu Feb 13, 2014 11:40 am
by VilleK
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()));

Posted: Thu Feb 13, 2014 12:07 pm
by Rado1
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.

Posted: Thu Feb 13, 2014 8:08 pm
by Rado1
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.