[SOLVED] take over system focus

Post your Gambas programming questions here.
User avatar
sadams54
Posts: 143
Joined: Monday 9th July 2018 3:43am
Contact:

[SOLVED] take over system focus

Post by sadams54 »

I am working on another project and need a little help.

My program is running in the background all the time but I want to interrupt a user no matter what he is doing or working on and show a message from my program on the screen on top of anything else including full screen apps. I have no clue how to make this happen and my research is obviously not going well. Any ideas that work?
Last edited by sadams54 on Monday 9th October 2023 11:35pm, edited 1 time in total.
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: take over system focus

Post by thatbruce »

There are several, if not many, ways to do this. I think the most elegant from the users point of view is using the dbus Notify demon.
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: take over system focus

Post by cogier »

This may help.

Public Sub Form_Open()

  Wait 5
  Shell "notify-send IMPORTANT! 'IMPORTANT message'"
  Message("IMPORTANT\n\nImportant message", "OK")

End
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: take over system focus

Post by vuott »

If your program is a command-line interface (CLI), then :? I would proceed as follows:

1) activate a Graphics Component anyway (for example: gb.gui.qt);

2) place, where needed in the code, the display of "Message" window.

Simple explanatory example:
Public Sub Main()

  Wait 4
  
  Message.Warning("Message !")

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: take over system focus

Post by BruceSteers »

You need to find the active screen. i find the best way is with the Mouse.ScreenX, Mouse.ScreenY properties.

By default you cannot force a Message box to appear on a selected screen (by default Message only appears on Screens[0])

There are ways around it.
you can either open a custom message Form moved to the required screen.

Or i made a hack that will move any gambas Modal window or you can set a list of window names like FMessage (Message) FInput (InputBox)

See the attached program.

It creates and starts a Timer just before the ShowModal function is called. then the Timer force moves the active Message window to the desired screen or window depending on how it is set

Normal (normal gambas mode, always Screens[0])
Same Screen (opens on the screen the mouse is on)
Same Window (stacked at top-left) (attaches to top-left of active window)
Same Window (centered) (centered on active window)

To use it in your own prog just put the Form.class in your .src folder and compile then set the modal mode as I have in the example.
Attachments
MessageCorrectScreen-0.0.2.tar.gz
(12.75 KiB) Downloaded 76 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: take over system focus

Post by BruceSteers »

The first method is to make your notification happen by your own Form popping up.

You set the following...

MyNotifyForm.Stacking = Window.Above
MyNotifyForm.TopOnly = True

Then when MyNotifyForm opens it should be on top of any other window.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
sadams54
Posts: 143
Joined: Monday 9th July 2018 3:43am
Contact:

Re: take over system focus

Post by sadams54 »

I tested out some of this and it works fine except for where it is really needed. None seem to pop up a notification over another application that is running full screen such as VLC or totem. I need to be able to put a message up even over a full screen app.
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: take over system focus

Post by BruceSteers »

Odd , because i used this simple code..

Public Sub OpenTestWindow()

  Dim w As Window = New Window
  w.W = 300
  w.H = 200
  w.Move(Screens[1].X + 100, 100)
  w.Stacking = Window.Above
  w.TopOnly = True

  w.Show

End




if SMPlayer or vlc is running on Screens[1] then the window opens on top, perfectly visible.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: take over system focus

Post by thatbruce »

The notify (dbus) method will. That's it job. If it doesn't work then something is awry with the distribution you are using.
Note Cogier's code above may need to be adjusted depending on what notification daemon you have and are running.*
In a terminal type "notify-send --help" which should give you the syntax needed.

* The notification daemon is a user optioned thing set at login. For example here the distro notifier is some kde thingo which I dislike, so I use dunst instead which is predictable and simple. Others on this machine use the kde one, so just beware of this if you are in a multiuser situation.

b
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
sadams54
Posts: 143
Joined: Monday 9th July 2018 3:43am
Contact:

Re: take over system focus

Post by sadams54 »

I will give another try to the code bruce gave. But the mention of KDE made me cringe because now I am wondering if I have to worry about what desktop is being used. Something I had not considered.
Post Reply