Question: I would like to add a custom view for my application. For this I use WindowsManager:

```
        final WindowManager wm = getWindowManager();
        final View view = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.game_menu, null);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_FULLSCREEN;
        lp.dimAmount = (float) 0.6;
        lp.format = PixelFormat.TRANSPARENT;
        lp.windowAnimations = android.R.style.Animation_Dialog;
        view.setOnKeyListener(new OnKeyListener() 
        {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) 
            {
                Log.d("12", "12");
                if (keyCode == KeyEvent.KEYCODE_BACK)
                {
                    wm.removeView(view); // This I need to hide my menu
                }
                return false;
            }
        });
        wm.addView(view, lp); // I add menu like in Angry Birds and other games

```

But I cannot capture device key events for hiding this view.

Why my key listener not invoked in view added by WindowsManager? What must I do to hide my view by device back key pressed?

Answer: 
you may add these `setFocusableInTouchMode(true);` in your view
I'm still fairly new at this, but I'm trying to get similar functionality working. Here's what got it working for me (in Kotlin):

```
mOverlayView = LayoutInflater.from(this).inflate(R.layout.overlay_layout, null)

val params = WindowManager.LayoutParams(         
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
    // This was the piece I was missing
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 
        or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, 
    PixelFormat.TRANSLUCENT
)

mWindowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
mWindowManager!!.addView(mOverlayView, params)

mOverlayView!!.isFocusableInTouchMode = true
mOverlayView!!.setOnKeyListener(object : View.OnKeyListener {
    override fun onKey(v: View?, keyCode: Int, event: KeyEvent?): Boolean {
        // Custom code here...
        return true
    }
}

```