Jump to content
The simFlight Network Forums

Determine if FSX has focus in Lua scripts


Recommended Posts

Hi!
 

I want to do something in a Lua script only when FSX currently has the focus.

Currently I work around this problem by using ext.run to call a small autohotkey script, but this has the drawback (besides not being the most efficient way to do it) that this causes an hourglass to be shown next to the mouse pointer for a very brief moment.

 

FSUIPC4.log already contains lines like these:

Lost focus to PID=2056, "VirtuaWin.exe"
Focus lost: culprit unknown

So FSUIPC already should have this information.

 

Maybe similar to ext.isrunning(), e.g.:

bool = ext.hasfocus()

And if later you want to implement a method to query focus for any window, this can be extended to:

bool = ext.hasfocus(handle)
bool = ext.hasfocus("name")

Of course having an offset to make this available without Lua would work for me, too, whatever you think is more useful.

 

Can you implement something like this?

 

Regards,

Thomas

Link to comment
Share on other sites

Hi!

 

I want to do something in a Lua script only when FSX currently has the focus.

Currently I work around this problem by using ext.run to call a small autohotkey script, but this has the drawback (besides not being the most efficient way to do it) that this causes an hourglass to be shown next to the mouse pointer for a very brief moment.

 

FSUIPC4.log already contains lines like these:

Lost focus to PID=2056, "VirtuaWin.exe"
Focus lost: culprit unknown

So FSUIPC already should have this information.

 

Maybe similar to ext.isrunning(), e.g.:

bool = ext.hasfocus()

And if later you want to implement a method to query focus for any window, this can be extended to:

bool = ext.hasfocus(handle)
bool = ext.hasfocus("name")

Of course having an offset to make this available without Lua would work for me, too, whatever you think is more useful.

 

Can you implement something like this?

 

I'll take a look, but I'm not likely to be able to implement anything between now and Feb7th -- daughter's wedding and another break intervening.

 

I've made a note and will get back to you as and when.

 

Pete

Link to comment
Share on other sites

  • 4 weeks later...

Hi!

 

Just to show you (and possibly other interested people) what I am doing, and to indicate that I'm still interested in ext.hasfocus() :):

To get rid of the hourglass, I'm now using ext.postmessage to do the focus check (which I want to do in lua) and mouse movement (which is already possible in lua).

 

I'd like to post this to the user contributions subforum when I can completely implement it in lua, because then the following problems will be solved:

- having to enter the window title of the FSX window (which changes when I'm using FSUIPC to monitor offsets)

- requiring an external program to run (and to be compiled beforehand)

- having to adjust the path for ext.run

 

FSX/Modules/lua/mousemove.lua (called via ipc.runlua('lua\\movemouse') in ipcReady.lua):

-- Move mouse to upper window edge of FSX window mouse is not used.
-- Workaround for a performance issue when the mouse pointer is
-- inside the FSX window.

mousemove = ext.run('D:\\FSX\\FSX\\Modules\\mousemove\\mousemove.exe',
                    EXT_HIDE, EXT_FOCUS, EXT_CLOSE)

function mousemove_reset()
  mousemove_timeout = 3
end

function mousemove_timer()
  x, y = mouse.getpos()
  if x ~= mousemove_old_x or y ~= mousemove_old_y then
    mousemove_reset()
    mousemove_old_x = x
    mousemove_old_y = y
  elseif mousemove_timeout > 0 then
    mousemove_timeout = mousemove_timeout - 1
  elseif mousemove_timeout == 0 then
    mousemove_timeout = -1
    ext.postmessage(mousemove, 0x1001, 0, 0)
  end
end

mousemove_old_x = nil
mousemove_old_y = nil
event.mouseleft("mousemove_reset")
event.mousemiddle("mousemove_reset")
event.mouseright("mousemove_reset")
event.mousewheel("mousemove_reset")
event.mousehoriz("mousemove_reset")
event.timer(1000, "mousemove_timer")

FSX/Modules/mousemove/mousemove.ahk (compiled to mousemove.exe):

#NoEnv
#SingleInstance force

Suspend := "Suspend/Resume mousemove"

Menu, tray, NoStandard
Menu, tray, Add, %Suspend%, menuSuspend
Menu, tray, Add, Exit, menuExit
Menu, tray, Default, %Suspend%
Menu, Tray, Click, 1

; autohotkey script to move mouse to upper window edge of FSX window
; receives postmessage 0x1001 to avoid having to execute more than once
; workaround for FSUIPC not yet providing FSX focus information to Lua scripts:
; 

mousemove(wParam, lParam, msg, hwnd) {
        FS := "Microsoft Flight"
        FS := "Monitor IPC"

        IfWinActive, %FS%
        {
                WinGetPos, , , Width, Height, %FS%
                MouseMove, Width // 4, 0, 0
        }
}

OnMessage(0x1001, "mousemove", 1)
return

menuSuspend:
        Pause
return

menuExit:
        ExitApp
return
Link to comment
Share on other sites

Thanks for reminding me of this request. I will be getting to it soon, probably this weekend. It's been pretty hectic here since returning from holiday, ad to make matters worse I have come down with a stinking head cold which has progressed downwards to give me a terrible cough! Surprising how much these things slow you down when you are old like me!

 

The first part, checking if FSX has focus, is reasonably easy -- though there can be complications, as when the focus is in a Menu. The extension to test other Windows is not so easy since the normal Focus functions in Windows only work for your current thread Window. There's also some confusion between "Foreground Window", and "Focus" which I need to clarify in my mind. Windows references mention both as if they are separate states, and both have their own functions.

 

Pete

  • Upvote 1
Link to comment
Share on other sites

I replaced the ext.postmessage call in above script with:

    if ext.hasfocus() then
      mouse.move(25, 0, 2) -- left half of FS top edge
    end

and it works great, thank you again!

 

I'll make a proper post in the user contributions forum about this.

 

Edit: here is the post: http://forum.simflight.com/topic/80982-mousemovelua-move-mouse-to-upper-window-edge-of-fsx-if-the-mouse-is-not-used/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.