androidandroid-layoutandroid-virtual-keyboard

Listen to virtual keyboard presses without edit text


Is there a way to get key events when forcing keyboard this way in Fragment onCreateView method:

    getActivity().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Also would need the keyboard to be in numeric mode.

Thanks.


Solution

  • You can listen keyboard presses if you override onKeyDown method in your activity

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                ...
                return true;
                ...
            default:
                return super.onKeyDown(keyCode, event);
            }
        }