Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard input in android NDK using NativeActivity

I'm looking for a way of getting input from the software keyboard from a Android NativeActivity.

I found this, that provides some sample code of how to get the software keyboard out (and it works), but lacks a few points of interest:

  1. How to get the input of the keyboard once displayed.
  2. How to avoid the app crashing when the user uses the back button to close the keyboard.

If anyone has any answers to either of these questions, or better yet, a simpler way of showing the keyboard that does not require the use of JNI, please share.

Thanks in advance,

Jaime

like image 897
JaimeBarrachina Avatar asked Dec 04 '12 16:12

JaimeBarrachina


1 Answers

If anyone wonders, you access keyboard input the usual way, in your callback assigned to the struct android_app where you get the AInputEvents:

if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY)
{
lint32_t key_val = AKeyEvent_getKeyCode(event);
fprintf("Received key event: %d\n", key_val);

if((key_val >= AKEYCODE_A && key_val <= AKEYCODE_Z))
{
    fprintf("Got a letter");
}
return 0;
}

You can also get access to other "hardware" buttons here by checking against key codes such as AKEYCODE_BACK or AKEYCODE_VOLUME_UP.

like image 130
JaimeBarrachina Avatar answered Sep 23 '22 21:09

JaimeBarrachina