SendKey 到特定应用程序

SendKey to specific application

我的要求是自动切换 Internet Explorer 的多个选项卡。在浏览了 web 和 Whosebug 之后,我提到 this 附带一个解决方案 .vbs 文件作为 -

Set WshShell = WScript.CreateObject("WScript.Shell")
while 1
    WshShell.AppActivate "Internet Explorer"
    WScript.Sleep 7000
    WshShell.SendKeys("^{TAB}")
wend

但是这种方法的问题是它会将密钥发送给所有活动的应用程序。 我需要它来触发仅 Internet Explorer 的键。 请帮忙。 提前致谢。

没问题。 我可以使用 C# 脚本实现此目的 -

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace SendKeyAppDemo
{
    class Program
    {
        [DllImport("User32.dll")]
        private static extern int SetForegroundWindow(IntPtr ptr);
        static void Main(string[] args)
        {
            Process p = Process.GetProcessesByName("iexplore").FirstOrDefault();
            if (p.ProcessName == "iexplore")
            {
                while (true)
                {
                    IntPtr h = p.MainWindowHandle;
                    SetForegroundWindow(h);
                    SendKeys.SendWait("^{TAB}");
                    Thread.Sleep(5000);
                }

            }
        }
    }
}