vb.net sendkeys 到特定的活动应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15890944/
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
vb.net sendkeys to specific active application
提问by hack nayan
i already done on making some sendkeys application to help me make my life easier, but im annoy to it when i switch windows and i forgot to switch off the sendskeys, like for example, my application will send "123123" to the notepad (current active application) until i stop it, so when i switch to msword it will not work, then if i switch back again to notepad it will sendkeys again, is that possible? life if there is some argument like
我已经完成了一些 sendkeys 应用程序来帮助我让我的生活更轻松,但是当我切换窗口时我很恼火并且我忘记关闭 sendkeys,例如,我的应用程序将发送“123123”到记事本(当前活动应用程序),直到我停止它,所以当我切换到 msword 时它不起作用,然后如果我再次切换回记事本它会再次发送密钥,这可能吗?生活,如果有一些争论,比如
If activeWindows = notepad.exe then
do this
Else
do nothing
End If
it's just an idea but i dont know how to make something like that in vb.net, and i want to use .exe instead of the title of application cause i think it's much fitting for what im doing.
这只是一个想法,但我不知道如何在 vb.net 中制作类似的东西,我想使用 .exe 而不是应用程序的标题,因为我认为它非常适合我在做什么。
thx in advance
提前谢谢
回答by Amegon
Yes, it should work, good idea. What I did so far was to read the numLock key. As long as it is on, the timer with sendkeys is firing. As soon as turned off, the timer will be stopped as well. But adding a check for the current focus would be a nice addition.
是的,它应该有效,好主意。到目前为止,我所做的是读取 numLock 键。只要它打开,带有发送键的计时器就会触发。一旦关闭,计时器也将停止。但是添加对当前焦点的检查将是一个不错的补充。
What methods to use, please check this, I think it fits How do I find the program with the current focus?
使用什么方法,请检查这个,我认为很合适如何找到当前焦点的程序?
回答by John Koerner
You can use the GetForegroundWindowand GetWindowThreadProcessIdAPIs to do this for you. Here is a quick example:
您可以使用GetForegroundWindow和GetWindowThreadProcessIdAPI 为您执行此操作。这是一个快速示例:
''' <summary>The GetForegroundWindow function returns a handle to the foreground window.</summary>
''' <returns>The return value is a handle to the foreground window. The foreground window can be NULL in certain circumstances, such as when a window is losing activation. </returns>
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Task.Run(AddressOf KeySender)
End Sub
Private Sub KeySender()
While (True)
Dim fgWin = GetForegroundWindow()
Dim fgPid As New Integer()
GetWindowThreadProcessId(fgWin, fgPid)
Dim proc = System.Diagnostics.Process.GetProcessById(fgPid)
Console.WriteLine(proc.ProcessName)
If (proc.ProcessName = "notepad") Then
SendKeys.SendWait("A")
End If
System.Threading.Thread.Sleep(1000)
End While
End Sub

