在 VB.NET 中发送击键的简单应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9789733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Application for sending Keystroke in VB.NET
提问by Operagust
I have an issue regarding Sendkeys Class, as i want to use this class in order to send some keystroke to the active application. As a first step i want to test the {Enter} keystroke, so in order to achieve that i made a simple application in vb.net 2010
我有一个关于 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
End Class
结束类
When i tried to run for {Enter} i have received infinit loop error: An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll Could someone help me?
当我尝试运行 {Enter} 时,我收到了无限循环错误:System.Windows.Forms.dll 中发生了类型为“System.StackOverflowException”的未处理异常有人可以帮助我吗?
LATER EDIT : In meantime i have found another example
后期编辑:同时我发现了另一个例子
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
If in background i have two applications :
如果在后台我有两个应用程序:
- Visual Studio
- Target application
- 视觉工作室
- 目标应用
How can i focus on the target application, because now when i am running the form , that form became active..is there any solution to call the winform from command prompt with some parameter (the key that i want to send)?
我如何专注于目标应用程序,因为现在当我运行表单时,该表单变为活动状态。
Later Edit2 strong texti have give it up the idea for windows form, so finnally i have made a simple program in console application that simulates the keystroke ... Imports System Imports System.Windows.Forms
后来 Edit2 强文本我放弃了 Windows 窗体的想法,所以最后我在控制台应用程序中做了一个简单的程序来模拟击键......导入系统导入 System.Windows.Forms
Module Module1
模块模块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
End Module
终端模块
By the way in order to use double quotes in a parameter you need \"test\"...(i have spent 15 min to find out) so it might be usefull... Thanks again for the informations.
顺便说一句,为了在参数中使用双引号,您需要 \"test\"...(我花了 15 分钟来找出答案)所以它可能很有用...再次感谢您提供的信息。
采纳答案by PraveenVenu
In order to sendkey to another application, you need to first activate that application on the button click and then send keys
为了将密钥发送到另一个应用程序,您需要先通过单击按钮激活该应用程序,然后再发送密钥
// 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("=");
}
回答by UnhandledExcepSean
Button has focus. Button click event sends an enter keystroke. Sending an enter causes the button click event to fire. Infinite loop.
按钮有焦点。按钮单击事件发送回车键击。发送输入会导致按钮单击事件触发。无限循环。
You could disable the button before and re-enable the button after the sendkeys routine is done to stop the infinite loop.
您可以先禁用该按钮,然后在完成 sendkeys 例程以停止无限循环后重新启用该按钮。
What you probably would want to do is switch the focus back to the application you want to send your keystrokes to.
您可能想要做的是将焦点切换回您要将按键发送到的应用程序。