【问题标题】:SendKey to specific application将密钥发送到特定应用程序
【发布时间】:2017-09-20 14:56:18
【问题描述】:
我的要求是自动切换 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 的键。
请帮忙。
提前致谢。
【问题讨论】:
标签:
windows
batch-file
browser
scheduled-tasks
monitoring
【解决方案1】:
没问题。
我可以使用 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);
}
}
}
}
}