keeehlan
keeehlan

Reputation: 8054

keyOutputText attribute on a custom KeyboardView doesn't output any text

I'm trying to make a custom keyboard but it's not outputting the text value I set in keyOutputText on the Key node.

Here's activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Here's the keyboard layout:

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

    <Row>
        <Key
            android:keyLabel="test"
            android:keyOutputText="test" />
    </Row>
</Keyboard>

Here's the Java class:

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.View;
import android.view.inputmethod.InputConnection;

public class TestKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

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

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
    }

    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }
}

I haven't really found any great tutorials on this so I'm not sure if I haven't implemented any code that I'm supposed to? I'll provide more code and resources if necessary. Everything works fine, the app builds and launches and I can set my current keyboard to this one, but nothing happens when I actually press the key. It responds as if I pressed it but no text is outputted.

Upvotes: 0

Views: 423

Answers (2)

keeehlan
keeehlan

Reputation: 8054

It turns out that I simply had to implement the onText method.

@Override
public void onText(CharSequence text) {
    getCurrentInputConnection().commitText(text, 1);
}

Upvotes: 1

Farshad
Farshad

Reputation: 3150

When you press a key, onKey(int primaryCode, int[] keyCodes) method will be called, and your method's body is empty , so nothing happens!

the primaryCode parameter actually is the android:codes attribute of the pressed Key that you define in the keyboard xml file, not the android:keyOutputText! Fillandroid:codes attribute with a proper ASCII code.

In order to commit the text you can use a simple code block like this:

@Override
public void onKey(int primaryCode, int[] keyCodes) {
  InputConnection ic = getCurrentInputConnection();
  ic.commitText(String.valueOf(code),1);
}

The IME is a pretty wide topic that you can find a lot about it on the internet, Please check this tutorials, It helped me a lot.

By the way you can find some source code here.

Good luck !

Upvotes: 0

Related Questions