Getting keyboard events ONLY if my app is frontmost

I have an app that does heavy-duty math calculations that occupy 100% of the core that it’s running on. It does not have even have time to check for system events (like mouse or keyboard activity) and if I add do.system.events it slows the program down so bad that it renders the program useless. If I add a keyboard.async.(whatever) instruction it works just fine and allows me to shut the program down or display current status. The problem is that even when another program is running and is frontmost (is receiving current events) when I press command-Q to quit the intended (other) program quits as it should, but so does my calc program. Plus, when my calc program is frontmost and I press command-Q my program quits, but so does the other app that is running.

When I first built this program under RB2006 I was able to use the Carbon Declares library to use a “is my app frontmost” in an IF so that my calc program would only respond to keyboard events if IT was frontmost (where it could capture the keystrokes) and that stopped the problem. Well, that Carbon Declares library no longer works with Xojo.

So my question is this … has that functionality (being able to test if your app is frontmost) been added to Xojo? Or is there another library out there somewhere, that now works with Xojo that would give me back that function?

Here’s the code I use to tell if Xojo is frontmost in a Cocoa App. There’s probably a more direct way though… and the app is IDed by name which can change.

[code]Private Function xojoIsFrontMost() As boolean
const CocoaLib = “Cocoa.framework”
declare function NSClassFromString lib CocoaLib (aClassName as CFStringRef) as Ptr
declare function sharedWorkspace lib CocoaLib selector “sharedWorkspace” (class_id as Ptr) as Ptr
declare function frontmostApp lib CocoaLib selector “frontmostApplication” (inst_id As Ptr) As Ptr
declare function getLocName lib CocoaLib selector “localizedName” (inst_id As Ptr) As CFStringRef

dim workspaceCls, workspace, frontRunningApp As Ptr

workspaceCls = NSClassFromString(“NSWorkspace”)

workspace = sharedWorkspace(workspaceCls)

frontRunningApp = frontmostApp(workspace)

return getLocName(frontRunningApp) = “Xojo”

End Function[/code]

Also, have you considered a more unusual key pattern to kill your math loop, like Control+Option+Command+Shift+Z? Something you’re very unlikely to hit whether your app is front or not.

Why not have checks in the calculation to call app.DoEvents only once a second?

BTW, the “Xojo” way to check if your app is frontmost is to implement the Activate and Deactivate events of your App class - if the former is called, your app is moved to front, if the latter is called, your app is moved to the back.

Also, the “proper” solution to your issue is to run your computational task in a Thread, then use the “#pragma disableBackgroundTasks” into all your functions that do tight and shortlived loops. Then either leave the outer loops without that pragma (then, every time such a loop turns around, events get checked and the app thus remains responsive), or call your thread’s Yield function from time to time. You can use the Microseconds function to get the current time, and maybe invoke Yield every 100ms.

Or, if you cannot figure threads out, just use the Microseconds function to invoke App.DoEvents(1) once every 100ms.

[quote=70078:@Will Shank]Here’s the code I use to tell if Xojo is frontmost in a Cocoa App. There’s probably a more direct way though… and the app is IDed by name which can change.

[code]Private Function xojoIsFrontMost() As boolean
const CocoaLib = “Cocoa.framework”
declare function NSClassFromString lib CocoaLib (aClassName as CFStringRef) as Ptr
declare function sharedWorkspace lib CocoaLib selector “sharedWorkspace” (class_id as Ptr) as Ptr
declare function frontmostApp lib CocoaLib selector “frontmostApplication” (inst_id As Ptr) As Ptr
declare function getLocName lib CocoaLib selector “localizedName” (inst_id As Ptr) As CFStringRef

dim workspaceCls, workspace, frontRunningApp As Ptr

workspaceCls = NSClassFromString(“NSWorkspace”)

workspace = sharedWorkspace(workspaceCls)

frontRunningApp = frontmostApp(workspace)

return getLocName(frontRunningApp) = “Xojo”

End Function[/code][/quote]