java - 从自定义键盘获取键盘输入

标签 java android

我刚刚编写了一个小型键盘应用程序,现在想要创建更多功能,例如拼写检查器。 我已经用 JavaScript 编写了一个,但由于这是我的第一个 Android Studio 项目,我在调整它时遇到了问题。

如何将用户在键盘上按下的输入输入到我的 Activity 中以进行使用?

这是我的 Java:

package edmt.dev.androidcustomkeyboard;

import android.app.Service;
        import android.content.Intent;
        import android.inputmethodservice.InputMethodService;
        import android.inputmethodservice.Keyboard;
        import android.inputmethodservice.KeyboardView;
        import android.media.AudioManager;
        import android.os.IBinder;
        import android.view.KeyEvent;
        import android.view.View;
        import android.view.inputmethod.InputConnection;

public class OrthKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    private  boolean isCaps = false;


    //Press Ctrl+O


    @Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard,null);
        keyboard = new Keyboard(this,R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    @Override
    public void onPress(int i) {

    }

    @Override
    public void onRelease(int i) {

    }

    @Override
    public void onKey(int i, int[] ints) {

        InputConnection ic = getCurrentInputConnection();
        playClick(i);
        switch (i)
        {
            case Keyboard.KEYCODE_DELETE:
                ic.deleteSurroundingText(1,0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                isCaps = !isCaps;
                keyboard.setShifted(isCaps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)i;
                if(Character.isLetter(code) && isCaps)
                    code = Character.toUpperCase(code);
                ic.commitText(String.valueOf(code),1);
        }

    }

    private void playClick(int i) {

        AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
        switch(i)
        {
            case 32:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
                break;
            case Keyboard.KEYCODE_DONE:
            case 10:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
                break;
            case Keyboard.KEYCODE_DELETE:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
                break;
            default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
        }
    }

    @Override
    public void onText(CharSequence charSequence) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }
}

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="40dp">



<Row>
    <Key android:keyLabel="1"
        android:keyEdgeFlags="left"
        android:codes="49"/>
    <Key android:keyLabel="2"
        android:codes="50"/>
    <Key android:keyLabel="3"
        android:codes="51"/>
    <Key android:keyLabel="4"
        android:codes="52"/>
    <Key android:keyLabel="5"
        android:codes="53"/>
    <Key android:keyLabel="6"
        android:codes="54"/>
    <Key android:keyLabel="7"
        android:codes="55"/>
    <Key android:keyLabel="8"
        android:codes="56"/>
    <Key android:keyLabel="9"
        android:codes="57"/>
    <Key android:keyLabel="0"
        android:keyEdgeFlags="right"
        android:codes="48"/>
</Row>

<Row>
    <Key android:keyLabel="q" android:keyEdgeFlags="left" android:codes="113"/>
    <Key android:keyLabel="w" android:codes="119"/>
    <Key android:keyLabel="e" android:codes="101"/>
    <Key android:keyLabel="r" android:codes="114"/>
    <Key android:keyLabel="t" android:codes="116"/>
    <Key android:keyLabel="y" android:codes="121"/>
    <Key android:keyLabel="u" android:codes="117"/>
    <Key android:keyLabel="i" android:codes="105"/>
    <Key android:keyLabel="o" android:codes="111"/>
    <Key android:keyLabel="p" android:keyEdgeFlags="right" android:codes="112"/>
</Row>

<Row>
    <Key android:keyLabel="a" android:keyEdgeFlags="left" android:codes="97"/>
    <Key android:keyLabel="s" android:codes="115"/>
    <Key android:keyLabel="d" android:codes="100"/>
    <Key android:keyLabel="f" android:codes="102"/>
    <Key android:keyLabel="g" android:codes="103"/>
    <Key android:keyLabel="h" android:codes="104"/>
    <Key android:keyLabel="j" android:codes="106"/>
    <Key android:keyLabel="k" android:codes="107"/>
    <Key android:keyLabel="l" android:codes="108"/>
    <Key android:keyLabel="\# \@" android:keyEdgeFlags="right" android:codes="35,64"/>
</Row>

<Row>
    <Key android:keyLabel="CAPS" android:keyEdgeFlags="left" android:codes="-1"/>
    <Key android:keyLabel="z" android:codes="122"/>
    <Key android:keyLabel="x" android:codes="120"/>
    <Key android:keyLabel="c" android:codes="99"/>
    <Key android:keyLabel="v" android:codes="118"/>
    <Key android:keyLabel="b" android:codes="98"/>
    <Key android:keyLabel="n" android:codes="110"/>
    <Key android:keyLabel="m" android:codes="109"/>
    <Key android:keyLabel="." android:codes="46"/>
    <Key android:keyLabel="DEL" android:isRepeatable="true" android:codes="-5" android:keyEdgeFlags="right"/>

</Row>

<Row android:rowEdgeFlags="bottom">
    <Key android:keyLabel="," android:keyWidth="10%p" android:keyEdgeFlags="left" android:codes="44"/>
    <Key android:keyLabel="/" android:keyWidth="10%p"  android:codes="47"/>
    <Key android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true" android:codes="32"/>

    <Key android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right" android:codes="-4"/>
    <Key android:keyLabel="\? ! :"  android:codes="63,33,58" android:keyWidth="20%p"/>
</Row>

</Keyboard>

最佳答案

您调用 InputConnection.commitText() 并传入他输入的字符串。你通常不会像你试图那样发送关键事件——太痛苦了。如果您确实发送关键事件,则需要发送向上和向下对 - 仅向下不会执行任何操作。仅当出现故障时,连接就会进入不良状态 - 如果操作系统连续出现 2 次故障,操作系统应该怎么做?忽略第二个?假设缺少一个 up 吗?这就是为什么需要向上。

关于java - 从自定义键盘获取键盘输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990553/

相关文章:

java - HttpUrlConnection 方法在 Android 上始终为 GET

java - JPA QueryException - 必须提供引用类

java - 更换 Xstream 设施

java - 如何将其转换为 lambda 表达式?

android - 即使 url 具有此处使用的 get 方法,连接也会发送响应代码 405

java - 智能 : How to find a method?

java - 是否可以在滑动到特定页面时隐藏工具栏并禁用滚动?

java - Spring jpa存储库查找全部

android - 在智能设备上激活的 mqtt 客户端可用

android - ListView Adapter - 何时收集附加到 View 的监听器?