Keyboard input in android NDK using NativeActivity

前端 未结 2 655
星月不相逢
星月不相逢 2021-01-03 04:36

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 sof

相关标签:
2条回答
  • 2021-01-03 04:57

    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.

    0 讨论(0)
  • 2021-01-03 05:07

    another important thing to check is the key action (key up, key down, or mixed), otherwise you will be reacting to both keyup and keydown. Here's how you would check for key up:

    if(AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP)
        fprintf("key up!");
    
    0 讨论(0)
提交回复
热议问题