You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在指定时间从后台发送左键点击(适配桌面应用与服务)

Answer

Great question! Let's break this down into three key parts: simulating the left-click without moving the mouse, scheduling the action for a specific time, and adapting your app to run as a Windows service. Here's how to approach each step:

1. Simulate a Left-Click Without Moving the Mouse

You have two practical options here, depending on your exact use case:

Option A: Click a Specific Window/Control

If you need to target a particular UI element (like a button or window), you can send click messages directly to it without touching the mouse cursor. For example, in C#:

// Get the handle of your target window (use FindWindow/FindWindowEx to locate it)
IntPtr targetWindow = FindWindow(null, "Your Window Title");
if (targetWindow != IntPtr.Zero)
{
    // Send left-click down and up messages to the window
    SendMessage(targetWindow, 0x0201, IntPtr.Zero, IntPtr.Zero); // WM_LBUTTONDOWN
    SendMessage(targetWindow, 0x0202, IntPtr.Zero, IntPtr.Zero); // WM_LBUTTONUP
}

For standard buttons, replace the two messages with a single BM_CLICK (0x00F5) for cleaner, more reliable behavior.

Option B: Click at the Current Mouse Position

If you just need to simulate a click where the mouse is already sitting (without moving it), use SendInput to replicate the action while locking the cursor in place:

// Get the current mouse coordinates
POINT currentCursorPos;
GetCursorPos(out currentCursorPos);

// Prepare mouse input structure for absolute positioning
INPUT clickInput = new INPUT();
clickInput.type = INPUT_MOUSE;
clickInput.mi.dx = (currentCursorPos.x * 65535) / Screen.PrimaryScreen.Bounds.Width;
clickInput.mi.dy = (currentCursorPos.y * 65535) / Screen.PrimaryScreen.Bounds.Height;
clickInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;

// Send the down click
SendInput(1, ref clickInput, Marshal.SizeOf(clickInput));

// Send the up click to complete the action
clickInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP;
SendInput(1, ref clickInput, Marshal.SizeOf(clickInput));

This uses screen-relative absolute coordinates to ensure the cursor stays exactly where it was.

2. Schedule the Click for a Specific Date/Time

Choose one of these reliable scheduling methods based on your needs:

  • In-app Timer: Use a timer (like System.Timers.Timer in .NET) that checks the current time against your target at regular intervals. When the time matches, execute your click logic. This is ideal if you need dynamic scheduling (e.g., updating the target time without restarting the service).

  • Windows Task Scheduler: For fixed, static schedules, configure Task Scheduler to trigger a lightweight helper executable (that handles the click) at your specified date/time. This offloads scheduling work to Windows and simplifies service management.

3. Run as a Windows Service

Converting your desktop app to a service requires addressing session isolation (a key Windows security feature):

  • Session Isolation Workaround: Windows services run in Session 0, which is isolated from the user's desktop (Session 1+). You can't send input directly to the user's desktop from Session 0. Instead, spawn a helper process in the user's active session using CreateProcessAsUser—this process runs in the user's context and can safely send the click.

  • Service Setup: Use .NET's ServiceBase class (for C#) or the Windows Service API (for native apps) to build your service. Make sure to handle lifecycle events (start, stop, pause) gracefully.

  • Avoid Deprecated Settings: Skip the "Allow service to interact with desktop" option—it's deprecated and unreliable on modern Windows versions. Stick to the helper process approach instead.

Putting it all together: Your service will manage scheduling, and when the target time arrives, it launches a helper process in the user's session that executes the left-click using one of the methods from step 1.

内容的提问来源于stack exchange,提问作者BabaYaga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:34:15