I’m making app that have to associate external keyboard inputs with taps. Now I’m trying to make my app listen external keyboard but it was unsuccessful.
Here what i’ve tried:
- Accessibility Service – it can listen ONLY inputs in EditTexts.
- OnKeyListener – works perfectly, but only if it is activity. I’ve tried to use such thing with Overlay, here what i’ve done:
It is OverlayService class
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
overlayView = inflater.inflate(R.layout.overlay_layout, null);
TextView result = overlayView.findViewById(R.id.result_view);
Button test = overlayView.findViewById(R.id.btn_test);
test.setOnClickListener(v ->{
stopService(MainActivity.intent);
});
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
windowManager.addView(overlayView, params);
overlayView.setOnKeyListener((v, keyCode, event) -> {
result.setText(result.getText().toString()+keyCode);
overlayView.requestFocus();
return false;
});
TextView isFocused = overlayView.findViewById(R.id.isfocused);
overlayView.requestFocus();
}
But when Im typing with my external bluetooth keyboard nothing happens even tho it works in activity. I’ve made check if overlay is focused and it always was focused. What am i doing wrong?
New contributor
Cookie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.