FindWindowLike not working for on-screen keyboard

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
I have a code that determines if a window is open or not based on input. It works well for all other programs, but for some reason does not detect when osk.exe is running... Any ideas? I basically put anything into the msgbox in my code between the quotation marks and it will search for a similarly named window that is open.

Code:

Code:
Option Explicit


Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function GetForegroundWindow Lib "user32.dll" () As Long


Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5


'********************************************
'*Give it part of the window text your looking for
'*it will give you the hWnd
'*usefull for windows that text is like "[project] - microsoft visual basic [design]"
'*usage:
'*Msgbox FindWindowLike("visual basic")
'*Returns 0 if not found
'*******************************************
Private Sub CommandButton1_Click()


MsgBox FindWindowLike("adobe")


End Sub


Function FindWindowLike(strPartOfCaption As String) As Long
Dim hWnd As Long
Dim strCurrentWindowText As String
Dim r As Integer


hWnd = GetForegroundWindow


Do Until hWnd = 0
strCurrentWindowText = Space$(255)
r = GetWindowText(hWnd, strCurrentWindowText, 255)
strCurrentWindowText = Left$(strCurrentWindowText, r)
'hWnd = GetWindow(hWnd, GW_CHILD)
If InStr(1, LCase(strCurrentWindowText), LCase(strPartOfCaption)) <> 0 Then GoTo Found
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop


Exit Function
Found:
FindWindowLike = hWnd
End Function
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I wouldn't use for this FindWindowLike which is based on the shaky and language dependent window title.

The osk.exe generates a top level window whose class name (at least on my machine ie: win10) is OSKMainClass.

This makes the FindWindow API ideal for retrieving its hwnd .

Something like this :
Code:
Option Explicit

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If


[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Function Get_OSK_Hwnd() As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Function Get_OSK_Hwnd() As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

    Get_OSK_Hwnd = FindWindow("OSKMainClass", vbNullString)

End Function



Sub Test()
    MsgBox Get_OSK_Hwnd
End Sub
 
Last edited:
Upvote 0
This code worked really well! Thank you much! I had to adjust a little bit using Private Declare PtrSafe, but other than that it works perfectly.
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,188
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top