SendKey到特定的应用程序

时间:2017-04-24 03:37:26

标签: windows batch-file browser scheduled-tasks monitoring

我的要求是自动切换Internet Explorer的多个标签。在查看了web和stackoverflow之后,我提到了this来解决.vbs文件作为 -

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

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

1 个答案:

答案 0 :(得分:0)

没问题。 我能够使用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);
                }

            }
        }
    }
}