我有一个关于Sendkeys类的问题,因为我想使用这个类来向活动应用程序发送一些击键。 作为第一步,我想测试{Enter}键击,所以为了实现我在vb.net 2010中做了一个简单的应用程序
Public Class Form1
Public Shared data As String
Private Sub SendintText(command As String)
Try
SendKeys.SendWait(command)
Catch e As Exception
MsgBox(e.StackTrace)
End Try
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
data = TextBox1.Text.ToString
SendingText(data)
End Sub
结束班
当我试图为{Enter}运行时,我收到了无限循环错误: System.Windows.Forms.dll中发生了未处理的“System.StackOverflowException”类型异常 有人能帮助我吗?
LATER EDIT:同时我找到了另一个例子
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
System.Threading.Thread.Sleep(2000)
SendKeys.SendWait("{ENTER}")
End Sub
如果在后台我有两个应用程序:
我如何专注于目标应用程序,因为现在当我运行表单时,该表单变为活动状态..是否有任何解决方案从命令提示符调用winform并带有一些参数(我要发送的密钥) ?
后来的Edit2 强文 我已经放弃了windows窗体的想法,所以最后我在控制台应用程序中制作了一个简单的程序来模拟击键...进口系统 Imports System.Windows.Forms
模块模块1
Sub Main(ByVal args() As String)
Dim data As String = args(0)
Console.WriteLine("You insert the : {0}", data)
System.Threading.Thread.Sleep(5000)
SendKeys.SendWait(data)
End Sub
结束模块
顺便说一句,为了在参数中使用双引号你需要“测试”...(我花了15分钟才能找到)所以它可能是有用的...... 再次感谢您的信息。
答案 0 :(得分:0)
按钮有焦点。按钮单击事件发送输入击键。发送输入会导致按钮单击事件触发。无限循环。
您可以在之前禁用该按钮,并在完成sendkeys例程后重新启用该按钮以停止无限循环。
您可能想要做的是将焦点切换回您想要发送击键的应用程序。
答案 1 :(得分:0)
要将sendkey发送到另一个应用程序,您需要首先在按钮上单击激活该应用程序,然后发送密钥
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
// Send a series of key presses to the Calculator application.
private void button1_Click(object sender, EventArgs e)
{
// Get a handle to the Calculator application. The window class
// and window name were obtained using the Spy++ tool.
IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");
// Verify that Calculator is a running process.
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
// Make Calculator the foreground application and send it
// a set of calculations.
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
}