Create an anti-idle App for Microsoft Teams with .NET
Anti-idle apps for Microsoft Teams and the like are a dime a dozen, but how about creating your own? In this article, I'll show you how to create an anti-idle app for Microsoft Teams using .NET and C#.
Idle Detection
Idle detection is as simple as it is annoying: it simply reacts to user input. If the user does not look at the screen for a while, the status is set to "absent". But you are not always really absent, sometimes you are just sitting in front of your computer and writing something down on a piece of paper, but Teams thinks you are lying in bed and sets you to absent.
Anti-Idle
The Anti-Idle app is a small application that simulates a user input at regular intervals. This can be a mouse click or a keyboard input. In our case, we simply use the Windows API to send a new position for the mouse cursor.
The application
First we need a .NET console application whose project settings we set as follows:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
This defines that we want to create a console application for .NET 8. More precisely, specifically for Windows, as in our case we need to address the Windows API directly.
Now we only need the code that moves the cursor at regular intervals, whereby I have decided to reset the cursor to the original point after setting the cursor.
using System.Runtime.InteropServices;
namespace BenjaminAbt.AntiIdleApp;
internal partial class Program
{
// Import the Windows API functions to control the mouse position
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool SetCursorPos(int X, int Y);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool GetCursorPos(out POINT lpPoint);
// Structure to store the mouse position
public struct POINT
{
public int X;
public int Y;
}
public static async Task Main(string[] args)
{
Console.WriteLine("Anti Idle is running. Press CTRL+C to exit.");
// Create a PeriodicTimer that ticks every 1 minute (60 seconds)
using PeriodicTimer timer = new(TimeSpan.FromMinutes(1));
// Loop that waits for each tick of the timer
while (await timer.WaitForNextTickAsync())
{
// Get the current mouse position
if (GetCursorPos(out POINT currentPos))
{
// Move the mouse cursor 1 pixel to the right
SetCursorPos(currentPos.X + 1, currentPos.Y);
// Short delay to ensure the movement is registered
await Task.Delay(50);
// Move the mouse back to the original position
SetCursorPos(currentPos.X, currentPos.Y);
}
}
}
}
Our Microsoft Teams anti-idle app is ready.
The complete code is available at https://github.com/BenjaminAbt/dotnet-anti-idle-app.