Faking foreground window focus for Telegram, or: How to minimize an app without letting it know?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Testertime
Posts: 7
Joined: 22 Aug 2017, 16:44

Faking foreground window focus for Telegram, or: How to minimize an app without letting it know?

Post by Testertime » 31 Mar 2020, 17:39

Hi everyone,

recently I started using Telegram Desktop and there seems to be no way to stay online without having the app open. I saw on Github that this also leads to unwanted phone notifications, but the Telegram team doesn't seem to be interested in fixing this.

So I wonder, is there any way to somehow fake that Telegram has foreground status, but is actually a background window without input focus?

It seems to be hard to achieve this, as making Telegram Desktop an always-visible foreground window doesn't do the trick. And I'm honestly not sure what to do, since special tricks seem to be required. I thought about making code adjustments to Telegram Desktop itself, but that would be sadly overkill - due to having to learn more about C++, finding the right code lines (the hardest part >_<), acquiring API keys, and so on. So I truly hope there is a cool workaround to achieve this little change with AutoHotkey. Do you have any suggestions?

Thanks so much!

gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Faking foreground window focus for Telegram, or: How to minimize an app without letting it know?

Post by gregster » 31 Mar 2020, 18:45

Tbh, I don't understand the specific problem, but perhaps using the browser version instead of the Desktop version helps.
It doesn't have all the fancy emojis of the desktop version, but I hadn't any problem so far to "stay online"... I think :think: (will have to look at the current desktop version again - in the past I didn't notice any problems, but I rarely used it, since I have a browser open anyway all the time)

Perhaps you just need to allow notifications?

Testertime
Posts: 7
Joined: 22 Aug 2017, 16:44

Re: Faking foreground window focus for Telegram, or: How to minimize an app without letting it know?

Post by Testertime » 31 Mar 2020, 19:34

gregster wrote:
31 Mar 2020, 18:45
Tbh, I don't understand the specific problem
That's okay, I'm going to explain it in more detail: When you actively use Telegram Desktop, you will be shown as "online". You can chat normally and notifications are directly shown on your PC, without sending superfluous push notifications to your phone. However, this is different as soon as Telegram Desktop is running in the background. People can't see you are active on your PC, they will only see a "last seen" date. And notifications will be sent to your PC and phone. There is no real "grace period" to avoid phone notifications, or even a setting to make Telegram Desktop keep the "online" status. In comparison, Discord shows you as online when you use your PC, and as "away" when you are away for at least 10 minutes. When you are actually away, Discord sends phone notifications. That's a big difference compared to Telegram. And it looks like this has been an active issue since 2014: https://github.com/telegramdesktop/tdesktop/issues/277
gregster wrote:
31 Mar 2020, 18:45
but perhaps using the browser version instead of the Desktop version helps.
Thanks for the suggestion, I just tested it. If I leave the browser tab open, I stay as "online", but there seems to be an aggressive timeout for that. And Telegram's web version also removes the "online" status once the browser tab lost focus.

One "solution" to get around this would be to use a virtual machine and use Telegram Desktop, but it would be really overkill. So I hope there is a way to trick Telegram Desktop into believing it has window focus, even though it hasn't. That would help to avoid unnecessary superfluous phone notifications, and (personally I think it's great) to let others know that you are actually online.

So far I tried to use this to minimize Telegram

Code: Select all

PostMessage, 0x112, 0xF020,,, Telegram,
but the program still notices it lost focus; same with "WinHide". Perhaps there is a right combination of WM message commands to achieve this? I'm honestly puzzled :cry:

Testertime
Posts: 7
Joined: 22 Aug 2017, 16:44

Re: Faking foreground window focus for Telegram, or: How to minimize an app without letting it know?

Post by Testertime » 02 Apr 2020, 12:45

I managed to find a way. This source brought me on the right path: https://stackoverflow.com/questions/13843742/letting-a-window-believe-it-is-still-in-focus-although-it-is-not

While the Telegram window is open and in the background, run this in AutoHotkey:

Code: Select all

PostMessage, 0x06, 2,,, Telegram,
This will trigger the online status and make Telegram even notice mouse movements and keyboard typing (and make you stay "online"). The status will return to "last seen" after a short while, around 20 seconds of idle time. Important: If Telegram was in the foreground, you need to execute this command again. Executing this while Telegram is the foreground window does not work.

To prevent the idle timer of 20 seconds, repeatedly run this command in AutoHotkey with a cooldown shorter than 20 seconds:

Code: Select all

; Important source: https://www.autohotkey.com/docs/misc/SendMessageList.htm
; Left Mouse Button down
PostMessage, 0x0201, 0x0001,,, Telegram,
sleep 100
; Left Mouse Button Up
PostMessage, 0x0202,,,, Telegram.
But please note that this is just the important puzzle piece, not the whole puzzle. For example, it would be important to check if a window called "Telegram" is actually really Telegram.exe. And to make the whole thing work, Telegram must not be minimized. If you still want to minimize the program, a trick needs to be used. Though it would make things more complex. The Telegram window can be hidden with

Code: Select all

WinHide, Telegram
and the PostMessage commands will still work correctly. But to show the Telegram window again, you have to rely on AutoHotkey by triggering

Code: Select all

WinShow, Telegram
And you need to be careful not to hide the window while you have a conversation open. Or else you won't get a notification for the chat with that person. Switch to the "saved" chat entry beforehand.

It would be definitely more convenient to trigger WinShow when clicking the Telegram tray icon on the taskbar, but I haven't tested that yet. For now I'm just leaving this here, to do my part and contribute something to this community :D

And if anyone of the Telegram development team happens to read this: Please add a proper online status for Telegram Desktop while the PC is in use, and not just while using the client! This dirty trick shouldn't be necessary.

Post Reply

Return to “Ask for Help (v1)”