Android: InputMethodService.Dialog.setOnKeyListener not taking any key events

469 Views Asked by At

I'm trying to create a custom ime for the Android TV similar to the "Leanback Keyboard" app. The issue I have right now is that I need to process KeyEvents (we have a remote control and I need to listen to D-pad inputs specifically). Because I need to use a service (InputMethodService), I can not use setOnKeyListener on it unlike an Activity. But since InputMethodService actually uses a dialog to output some UI on the screen, then maybe I could use the dialog's setOnKeyListener instead. I've tried the following:

public class MainActivity extends InputMethodService implements DialogInterface.OnKeyListener {

@Override
public View onCreateInputView() {
    final LayoutInflater inflater = getLayoutInflater();
    layout_root = (RelativeLayout) inflater.inflate(R.layout.activity_main, null);

    ...

    getWindow().takeKeyEvents(true);
    getWindow().setOnKeyListener(this);

    return layout_root;
}

@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
    ...
}

I've even explicitly made every view in xml to be unfocusable to make sure that the dialog gets the OnKey events:

android:focusable="false"
android:focusableInTouchMode="false"

I understand that Services do not get OnKey events but InputMethodService is outputting some UI elements and I feel like there should be a way.

My last resort is for the InputMethodService to start a transparent Activity that listens to KeyEvents and broadcasts it back to the InputMethodService. I don't really want to do that since I'm not even sure it'll work.

Appreciate the help in advance.

1

There are 1 best solutions below

0
On

I figured it out. InputMethodServce has onKeyDown, onKeyUp, onKeyLongPress and onKeyMultiple methods that you overwrite.