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

时间:2019-07-11 13:37:17

标签: java android

我刚刚编写了一个小型键盘应用程序,现在想创建更多功能,例如拼写检查器。 我已经用JavaScript编写过一个,但是由于这是我的第一个Android Studio项目,因此在适应它方面存在问题。

如何将用户在键盘上按下的输入输入到我的活动中进行操作?

这是我的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>

1 个答案:

答案 0 :(得分:0)

您调用InputConnection.commitText()并传入他键入的字符串。通常,您不会像尝试尝试那样发送关键事件,这会让您痛苦不堪。如果您确实发送了关键事件,则需要发送上下配对-仅凭下行不会执行任何操作。而且单独关闭可能会使连接进入不良状态-如果操作系统连续出现2次关闭,操作系统应该怎么做?忽略第二个?假设没有人上去?这就是为什么需要上升的原因。