|
-
May 14th, 2022, 11:32 PM
#1
Thread Starter
Fanatic Member
Which form eats the key presses?
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!
-
May 14th, 2022, 11:44 PM
#2
Thread Starter
Fanatic Member
Re: Which form eats the key presses?
I know the "Why" now. I have a webbrowser control on my form which fires an error event, and this seems to steal the focus from my form.
However, I would still like to know how to "detect" this. "GetForegroundWindow()" still reports my form as the foreground window.
-
May 15th, 2022, 02:54 AM
#3
Re: Which form eats the key presses?
theres many solutions.
1. subclass webbrowser
or
2. use windowshook for keyboard
or
3. use a timer to get the key press
or
4. use peekmessage
Last edited by baka; May 15th, 2022 at 03:07 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|