B4J Question [SOLVED] How to set the focus to a non Java application in Windows

jroriz

Active Member
Licensed User
Longtime User
Is there a way to set the focus to a non Java application in Windows?

I'm doing using vbscript...

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("Firefox")
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub SetForegroundWindow(Title As String)
   Dim WU As JavaObject
   WU.InitializeStatic("com.sun.jna.platform.WindowUtils")
   Dim user32 As JavaObject
   user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
   Dim L As List = WU.RunMethod("getAllWindows",Array(False)) 
   For Each JO As JavaObject In L
       Dim t As String = JO.RunMethod("getTitle",Null)
       If t.Contains(Title) Then
           Dim hwnd As Object = JO.RunMethod("getHWND", Null)
           user32.RunMethod("SetForegroundWindow", Array(hwnd))
           user32.RunMethod("SetFocus", Array(hwnd))
           user32.RunMethod("ShowWindow", Array(hwnd, 9))'SW_RESTORE
           Return
       End If
   Next
   Log("Window not found")
End Sub

B4X:
#AdditionalJar: jna-5.0.0
#AdditionalJar: jna-platform-5.0.0

Jars: http://repo1.maven.org/maven2/net/java/dev/jna/jna/5.0.0/jna-5.0.0.jar
http://repo1.maven.org/maven2/net/java/dev/jna/jna-platform/5.0.0/jna-platform-5.0.0.jar
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
B4X:
Sub SetForegroundWindow(Title As String)
   Dim WU As JavaObject
   WU.InitializeStatic("com.sun.jna.platform.WindowUtils")
   Dim user32 As JavaObject
   user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
   Dim L As List = WU.RunMethod("getAllWindows",Array(False))
   For Each JO As JavaObject In L
       Dim t As String = JO.RunMethod("getTitle",Null)
       If t.Contains(Title) Then
           Dim hwnd As Object = JO.RunMethod("getHWND", Null)
           user32.RunMethod("SetForegroundWindow", Array(hwnd))
           user32.RunMethod("SetFocus", Array(hwnd))
           user32.RunMethod("ShowWindow", Array(hwnd, 9))'SW_RESTORE
           Return
       End If
   Next
   Log("Window not found")
End Sub

B4X:
#AdditionalJar: jna-5.0.0
#AdditionalJar: jna-platform-5.0.0

Jars: http://repo1.maven.org/maven2/net/java/dev/jna/jna/5.0.0/jna-5.0.0.jar
http://repo1.maven.org/maven2/net/java/dev/jna/jna-platform/5.0.0/jna-platform-5.0.0.jar
Perfec!
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Code:
Sub SetForegroundWindow(Title As String)
Dim WU As JavaObject
WU.InitializeStatic("com.sun.jna.platform.WindowUtils")
Dim user32 As JavaObject
user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
Dim L As List = WU.RunMethod("getAllWindows",Array(False))
For Each JO As JavaObject In L
Dim t As String = JO.RunMethod("getTitle",Null)
If t.Contains(Title) Then
Dim hwnd As Object = JO.RunMethod("getHWND", Null)
user32.RunMethod(
"SetForegroundWindow", Array(hwnd))
user32.RunMethod(
"SetFocus", Array(hwnd))
user32.RunMethod(
"ShowWindow", Array(hwnd, 9))'SW_RESTORE
Return
End If
Next
Log("Window not found")
End Sub

Hi to all
It's possible to get focus (RunMethod) only on the latest window focused?
Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If you know the context which was the last one. Focus this one.
Windows does not have a history. At least not that i know of.
I fear the answer is no.
 
Upvote 0
Top