Keyboard input in android NDK using NativeActivity

こ雲淡風輕ζ 提交于 2019-11-30 14:38:54

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.

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!");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!