GetConsoleWindow / GetForegroundWindow #23134

Closed
opened 2026-01-31 08:33:20 +00:00 by claunia · 2 comments
Owner

Originally created by @Matzeeee on GitHub (Apr 11, 2025).

Windows Terminal version

1.22.10731.0

Windows build number

10.0.19045.5737

Other Software

I am trying to intercept mouse events in a Visual Basic .NET console application when the console has the focus:

'----------
<DllImport("user32.dll")>
Public Function GetAsyncKeyState(ByVal vKey As Integer) As Short
End Function
Public Const VK_LBUTTON As Integer = &H1 ' Left mouse button
Public Const VK_RBUTTON As Integer = &H2 ' Right mouse button
Public Const VK_MBUTTON As Integer = &H4 ' Middle mouse button
<DllImport("user32.dll")>
Public Function GetForegroundWindow() As IntPtr
End Function
<DllImport("kernel32.dll")>
Public Function GetConsoleWindow() As IntPtr
End Function

Public Sub Console_Maus_Thread_()
        Do
            If GetConsoleWindow() = GetForegroundWindow() Then
                If GetAsyncKeyState(VK_LBUTTON) <> 0 Then 'Maus links
                    'do something...
                ElseIf GetAsyncKeyState(VK_RBUTTON) <> 0 Then 'rechts
                    'do something...
                ElseIf GetAsyncKeyState(VK_MBUTTON) <> 0 Then 'mitte
                    'do something...
                End If
            Else 'click outside console window, throw clicks anway
                GetAsyncKeyState(VK_LBUTTON) 
                GetAsyncKeyState(VK_RBUTTON)
                GetAsyncKeyState(VK_MBUTTON)
            End If
            Thread.Sleep(300)
        Loop
End Sub

'----------

My problem is that GetConsoleWindow() is never equal to GetForegroundWindow() when the console window has the focus. If I run the application in cmd.exe, it works.
How can I fix this?

Steps to reproduce

see above

Expected Behavior

If the console window has the focus: GetConsoleWindow() should be EQUAL to GetForegroundWindow()

Actual Behavior

If the console window has the focus: GetConsoleWindow() is NOT equal to GetForegroundWindow()

Originally created by @Matzeeee on GitHub (Apr 11, 2025). ### Windows Terminal version 1.22.10731.0 ### Windows build number 10.0.19045.5737 ### Other Software I am trying to intercept mouse events in a Visual Basic .NET console application when the console has the focus: '---------- <DllImport("user32.dll")> Public Function GetAsyncKeyState(ByVal vKey As Integer) As Short End Function Public Const VK_LBUTTON As Integer = &H1 ' Left mouse button Public Const VK_RBUTTON As Integer = &H2 ' Right mouse button Public Const VK_MBUTTON As Integer = &H4 ' Middle mouse button <DllImport("user32.dll")> Public Function GetForegroundWindow() As IntPtr End Function <DllImport("kernel32.dll")> Public Function GetConsoleWindow() As IntPtr End Function Public Sub Console_Maus_Thread_() Do If GetConsoleWindow() = GetForegroundWindow() Then If GetAsyncKeyState(VK_LBUTTON) <> 0 Then 'Maus links 'do something... ElseIf GetAsyncKeyState(VK_RBUTTON) <> 0 Then 'rechts 'do something... ElseIf GetAsyncKeyState(VK_MBUTTON) <> 0 Then 'mitte 'do something... End If Else 'click outside console window, throw clicks anway GetAsyncKeyState(VK_LBUTTON) GetAsyncKeyState(VK_RBUTTON) GetAsyncKeyState(VK_MBUTTON) End If Thread.Sleep(300) Loop End Sub '---------- My problem is that GetConsoleWindow() is never equal to GetForegroundWindow() when the console window has the focus. If I run the application in cmd.exe, it works. How can I fix this? ### Steps to reproduce see above ### Expected Behavior If the console window has the focus: GetConsoleWindow() should be EQUAL to GetForegroundWindow() ### Actual Behavior If the console window has the focus: GetConsoleWindow() is NOT equal to GetForegroundWindow()
claunia added the Needs-TriageIssue-Bug labels 2026-01-31 08:33:21 +00:00
Author
Owner

@DHowett commented on GitHub (Apr 14, 2025):

Alas, the concept of a toplevel, visible, hookable "console window" is one that we cannot maintain in a multi-tenant console host such as Terminal. We try our best, but there will always be uses (like intercepting mouse input!) that we can't support.

@DHowett commented on GitHub (Apr 14, 2025): Alas, the concept of a toplevel, visible, hookable "console window" is one that we cannot maintain in a multi-tenant console host such as Terminal. We try our best, but there will always be uses (like intercepting mouse input!) that we can't support.
Author
Owner

@DHowett commented on GitHub (Apr 14, 2025):

Unless you need pixel-perfect mouse reporting, I'd recommend your console application use the ENABLE_MOUSE_INPUT mode or one of the "virtual terminal" xterm mouse reporting modes.

@DHowett commented on GitHub (Apr 14, 2025): Unless you need pixel-perfect mouse reporting, I'd recommend your console application use the `ENABLE_MOUSE_INPUT` mode or one of the "virtual terminal" xterm mouse reporting modes.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#23134