android - 在 C/C++ 中接收完整的 android unicode 输入

标签 android c++ unicode opengl-es android-ndk

(安卓、NDK、C++、OpenGL ES)

我需要一种方法来可靠地接收来自(软)键盘的文本输入。 解决方案可以通过 Java 使用 NativeActivity 子类或任何有效的方法。 最后我需要输入任何文本,这样我就可以用 OpenGL 自己渲染它

一些背景: 到目前为止,我一直在通过 JNI 调用 showSoftInput 或 hideSoftInputFromWindow 来触发软键盘。到目前为止,这从未失败过。 但是,问题是 native Activity 不会发送所有字符。特别是一些超出 ASCII 范围的 unicode 字符,或者一些运动软键盘将不起作用(AKeyEvent_getKeyCode)

过去可以获取一些其他 unicode 字符,为什么要检查 KeyEvent.ACTION_MULTIPLE 并读取一串字符。 但即使这样也不再可靠地工作了。

到目前为止,我没能找到替代方法。 我尝试以编程方式添加 EditText,但从未成功。即使尝试添加一个简单的 Button 也会导致 OpenGL View 不再呈现。

在 iOS 上,我通过隐藏编辑框来解决这个问题,我只需激活它即可显示键盘。然后我会读出编辑框并使用字符串在 OpenGL 中渲染自己。

最佳答案

我有同样的问题,我已经使用与 InputEvent 分开处理的“Character”事件解决了这个问题。

问题是这样的:AKeyEvent_getKeyCode不会为某些软键事件返回 KeyCode,特别是当您按住某个键时扩展的“unicode/latin”字符。这会阻止方法 @Shammi 和 @eozgonul 工作,因为 KeyEvent在 Java 端重建没有足够的信息来获取 unicode 字符。

另一个问题是 InputQueuedispatchKeyEvent 之前在 C++/Native 端被耗尽事件被触发。这意味着 KEYDOWN/KEYUP 事件都在 Java 代码可以处理事件之前触发。 (它们没有交错)。

我的解决方案是通过覆盖 dispatchKeyEvent 在 Java 端捕获 unicode 字符并将字符发送到 Queue<Integer> queueLastInputCharacter = new ConcurrentLinkedQueue<Integer>();

// [JAVA]
@Override
public boolean dispatchKeyEvent (KeyEvent event)
{
    int metaState = event.getMetaState(); 
    int unichar = event.getUnicodeChar(metaState);

    // We are queuing the Unicode version of the characters for
    // sending to the app during processEvents() call.

    // We Queue the KeyDown and ActionMultiple Event UnicodeCharacters
    if(event.getAction()==KeyEvent.ACTION_DOWN){
        if(unichar != 0){
            queueLastInputCharacter.offer(Integer.valueOf(unichar));
        }
        else{
            unichar = event.getUnicodeChar(); 

            if(unichar != 0){
                queueLastInputCharacter.offer(Integer.valueOf(unichar));
            }
            else if (event.getDisplayLabel() != 0){
                String aText = new String();
                aText = "";
                aText += event.getDisplayLabel();
                queueLastInputCharacter.offer(Integer.valueOf(Character.codePointAt(aText, 0)));
            }
            else
                queueLastInputCharacter.offer(Integer.valueOf(0));
        }
    }
    else if(event.getAction()==KeyEvent.ACTION_MULTIPLE){
        unichar = (Character.codePointAt(event.getCharacters(), 0));
        queueLastInputCharacter.offer(Integer.valueOf(unichar));
    }


    return super.dispatchKeyEvent(event);
}

并发队列将使线程一起玩得很好。

我有一个返回最后输入字符的 Java 端方法:

// [JAVA]
public int getLastUnicodeChar(){
    if(!queueLastInputCharacter.isEmpty())
        return queueLastInputCharacter.poll().intValue();
    return 0;
}

在循环程序代码的末尾,我进行了额外的检查以查看队列是否保留了任何 unicode 字符:

// [C++]
int ident;
int events;
struct android_poll_source* source;

// If not rendering, we will block 250ms waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((ident = ALooper_pollAll(((nv_app_status_focused(_lpApp)) ? 1 : 250),
                                NULL,
                                &events,
                                (void**)&source)) >= 0)
{
    // Process this event.
    if (source != NULL)
        source->process(_lpApp, source);

    // Check if we are exiting.  If so, dump out
    if (!nv_app_status_running(_lpApp))
        return;
}

static int modtime = 10; // let's not run on every call
if(--modtime == 0) {
    long uniChar = androidUnicodeCharFromKeyEvent();
    while (uniChar != 0) {
        KEvent kCharEvent; // Game engine event
        kCharEvent.ptkKey = K_VK_ERROR;
        kCharEvent.unicodeChar = uniChar;
        kCharEvent.character = uniChar;

        /* Send unicode char */
        kCharEvent.type = K_EVENT_UNICHAR;
        _lpPortableHandler(&kCharEvent);

        if (kCharEvent.character < 127) {
            /* Send ascii char for source compatibility as well */
            kCharEvent.type = K_EVENT_CHAR;
            _lpPortableHandler(&kCharEvent);
        }

        uniChar = androidUnicodeCharFromKeyEvent();
    }
    modtime = 10;
}

androidUnicodeCharFromKeyEvent功能与@Shammi 的 GetStringFromAInputEvent 非常相似方法,只使用CallIntMethod返回 jint .

注意事项 这确实需要修改您的引擎以处理与键事件分开的字符事件。 Android 仍然有像 AKEYCODE_BACK 这样的关键代码或 AKEYCODE_ENTER不是字符事件,仍然需要处理(并且可以在主输入循环器上处理)。

编辑框、控制台等...可以修改需要用户输入的内容以接收单独的字符事件来构建字符串。如果您在多个平台上工作,那么除了正常的键输入事件之外,您还需要生成这些新的字符事件。

关于android - 在 C/C++ 中接收完整的 android unicode 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124051/

相关文章:

perl - 如何判断特定字体是否包含 PDF::API2 中的特定字符

android - 无法启动服务 Intent

c++ - "pure virtual function called"在 gcc 4.4 上,但不在新版本或 clang 3.4 上

c++ - 使用 'undefined reference to ` boost::system::get_system_category()' 链接 boost barfs

c++ - 为什么我不能将迭代器应用于接受 const_iterator 引用的函数?

python - 如何在 Python 中将 Unicode 文件读取为 Unicode 字符串

android - 没有命名空间的自定义 Android 属性

android - “There is a problem parsing the package”

android - 相当于android :cropToPadding的代码

python - 避免Python编写的html文件中的不可打印字符