Hello!

First I have to say that my problem is not trivial, but reproducable.

I process the pressed keys in my main form like this:

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Debug.Print(KeyCode)

End Sub
The form has KeyPreview set to True.

All this works fine.

There is just one reproducible situation where this suddenly doesn't work anymore:

Form_KeyDown does not receive the key down anymore.

I am not sure what exactely I am doing which causes this. My program is huge, and I have a lot of controls on my main form, and I also have many other forms, so it's not really easy to find out what is happening that causes this. Before the problem occurs, I move controls around and show some and hide some, and I set window styles like this...

SetWindowPos Me.hwnd, 0, tr.Left, tr.Top, tr.Right - tr.Left, tr.Bottom - tr.Top, SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED

... but still I didn't expect this problem.

Because it left me so speechless and clueless what might be going on here, I installed a system-wide keyboard hook that would report me which keys are being pressed.
The keyboard hooks reports that the key is pressed like before (where the form receives the key).

So my next idea was that perhaps another control / window / form gets the input instead of my new form.

I created a timer on my form and let it report the active control like this each second:

Debug.Print Now & ": " & Me.ActiveControl.Name

Code:
Private Sub tmrCheck_Timer()

    If Me.ActiveControl Is Nothing Then
        Debug.Print Now & " nothing!"
    Else
        Debug.Print Now & " " & Me.ActiveControl.Name
    End If

End Sub
It reports that Me.ActiveControl is Nothing.

I have then tried what happens if I use the following:

Code:
Private Sub tmrCheck_Timer()

    Me.SetFocus

End Sub
I let the timer tick 5 seconds after my window-change action. It works fine.

I suspected that my window lost focus due to my window changing actions, so I thought I would check in the timer if my window is the foreground window, and if not, set focus to it.

Here is my code:

Code:
Private Sub tmrCheck_Timer()

    Dim lFore&
    lFore = GetForegroundWindow()
    
    If lFore <> Me.hwnd Then
        Debug.Print Now & " Some other window was the foreground window. So I need to set focus on my window again."
        Me.SetFocus
    Else
        Debug.Print Now & " All is good. My window is the foreground window. :-)"
    End If

End Sub
However, my window was always the foreground window, so there was not reason to set the focus anew.

My observations so far are the following:

My borderless maximized form is the foreground window.
But it doesn't receive the keyboard input.
If I call Me.SetFocus, the form receives the keyboard input again.

How could I check if my form needs .SetFocus?
I could use a timer and call .SetFocus each second, but that would be horrible.

I would first need to find out how I could check if it actually needs it, and then I could investigate where it loses the focus.

Does anybody have any idea how I could find out if Me.SetFocus is required?

Thank you!

Thank you!