ncssar / radiolog

SAR radio log program
Other
15 stars 3 forks source link

take focus, at the OS level, from any other programs, on incoming call #749

Open caver456 opened 3 months ago

caver456 commented 3 months ago

e.g. if using caltopo or browsing the web, an incoming call should steal focus and raise the new entry dialog.

Also, after the new entry dialog is closed in this scenario (after taking focus from another application), seems like the radiolog main window should keep focus, rather than handing focus back to the other application. The might be subjective / debatable, but, seemed like it would be the more helpful for radiolog to keep focus during the MRA mock 8/17/24.

caver456 commented 2 months ago

Researching before trying anything: looks like Windows has a few restrictions on this ability to 'steal' focus. Good explanations here, with details on how to work around it. Definitely worth a try!

caver456 commented 2 months ago

(don't steal focus on mic bumps!)

caver456 commented 2 months ago

Works well but has some massive side effects that would make this non-useful until figured out.

Starting the fsTester script then starting typing in another application (notepad, typing 'asdf' repeatedly): the radiolog main window does steal focus, but, that's probably not the right way to go - if another radiolog sub-window already has focus, then the main window shouldn't steal it, because it resulted in this (due to corresponding a, s, f hotkey actions in the main window): image

Anyway, here are the changes made so far:

  1. in imports: from pynput import keyboard

  2. at the end of MyWindow init:

        #749 - see relevant code in fsParse
        self.kbd=keyboard.Controller()
        # self.windowHandle=win32gui.FindWindow('MyWindow',None)
        # self.windowHandle=win32gui.GetForegroundWindow()
        self.windowHandle=int(selfwinId())

3, in fsParse, after the entire 'for line' clause, before 'checking for existing open new entry tabs':

        # 749 - take OS focus from other applications an all incoming calls (except mic bumps, which have already been filtered by this point)
        # https://stackoverflow.com/a/73921057/3577105
        rprint('taking system focus')
        self.kbd.press(keyboard.Key.alt)
        try:
            win32gui.SetForegroundWindow(self.windowHandle)
        except:
            rprint(' failed to set foreground window')
        finally:
            self.kbd.release(keyboard.Key.alt)
caver456 commented 2 months ago

made a branch for this issue and committed as-is. Will probably table this issue since it's not the highest priority and has potential side-effects... stealing focus seems like a mixed bag at best, and may just be a flat-out bad idea. Should probably be accompanied by some beeps and visual notification, and maybe pausing the keyboard input for a second - who knows. But, passing input to the main window seems dodgy because keys typed on auto-pilot would act as hotkeys, as above.