将ENTER键发送到VB.NET中的表单应用程序中的控制台 [英] Send ENTER key to console in form application in VB.NET

查看:53
本文介绍了将ENTER键发送到VB.NET中的表单应用程序中的控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题从未得到完全解决,但是下面的答案提供了有趣的结果.

该应用程序最终将由Powershell脚本调用,在这种情况下,我没有下面说明的问题.PS控制台中不需要其他{ENTER},因此不再有问题!通过cmd.exe手动启动我的应用程序时,我不在乎是否需要额外的ENTER键.

The app will eventually be called by a powershell script and in this situation, I don't have the issue explained below. No additional {ENTER} is required in the PS console, so no more issue! I couldn't care less if an extra ENTER is required when my app is launched manually via cmd.exe

问题:在VB.NET中表单应用程序,代码运行完毕后,无法将控制台恢复为默认"状态.我需要手动按Enter键.

Problem: In a VB.NET form app, I'm unable to get the console back to it's "default" state after the code is finished running. I need to press enter manually.

我的应用程序也可以从命令行执行(在这种情况下,不会打开任何表单.代码将自动执行,并将输出发送到控制台以供用户查看会发生什么情况)

My app can also be executed from command line (in this case, no form is opened. Code is being executed automatically and output sent to console for user to see what happens)

我调用 AttachConsole(-1),运行一些代码,当一切完成后,我会在控制台中看到我的最新消息,但这似乎还没有完成该过程.

I call AttachConsole(-1), run some code, and when everything's finished I see my latest message in console, but it's as if the process wasn't quite finished.

我已经尝试过 SendKeys.SendWait("{ENTER}").它运行良好,但仅在控制台是当前焦点时才有效.如果在代码运行时在控制台外部单击,则ENTER键将发送到我激活的任何窗口.

I have tried SendKeys.SendWait("{ENTER}"). It works well, but only when the console is the current focus. If I click outside the console while the code is running, the ENTER key is sent to whichever window I made active.

因此,我尝试将控制台设置为当前窗口:

So I tried to make the console the current window:

Dim bProcess As Process = Process.GetProcessesByName("cmd").FirstOrDefault()
SetForegroundWindow(bProcess.MainWindowHandle)
// I also tried AppActivate(bProcess.Id)

SendKeys.SendWait("{ENTER}")

FreeConsole()

不是,ENTER键仍将发送到其他地方,而不发送到控制台.但这确实会使控制台闪烁橙色,所以 SetForegroundWindow 似乎可以执行某些操作...

Nope, the ENTER key will still be sent somewhere else and not to the console. But it does make the console blink orange, so SetForegroundWindow seems to do something...

任何帮助将不胜感激:)

Any help will be greatly appreciated :)

编辑

响应@TnTinMn的回答:

In response to @TnTinMn's answer:

FreeConsole()
// SendKeys.SendWait("test")
PieceOfCodeFromTnTinMn()

与到目前为止的行为相同:如果在代码运行时失去焦点,它将在控制台外部发送 SendKeys.SendWait(〜")命令.

Same behavior as I've had so far: This will send the SendKeys.SendWait("~")command "outside" the console if it loses focus while the code is running.

上面第二行没有注释的"test"被发送到控制台之外,而 SendKeys.SendWait(〜")被发送控制台,如预期的那样.

BUT with the 2nd line above un-commented, "test" is sent outside the console and SendKeys.SendWait("~") is sent to the console, as expected.

我仍在试图弄清这里正在发生什么...

I'm still trying to figure out what is happening here...

推荐答案

您可以使用VB

You can use the VB Interaction.AppActivate Method to activate the parent console prior to calling SendKeys.SendWait. This requires that you obtain the ProcessID of the console window that is the parent process of your application.

执行此操作的一种方法是使用Windows Management Instrumentation(WMI).以下内容不是很漂亮,但它似乎可以工作.您可以在调用 FreeConsole

One way to do this is using Windows Management Instrumentation (WMI). The following is not pretty, but it appears to work. You would execute this after calling FreeConsole

Using currentProcess As Process = Process.GetCurrentProcess
  Dim query As New SelectQuery()
  Dim props As New StringCollection
  props.Add("ProcessId")
  props.Add("ParentProcessId")
  With query
    .ClassName = "Win32_Process"
    .Condition = $"ProcessId={currentProcess.Id}"
    .SelectedProperties = props
  End With

  Dim parentProcessId As Int32
  Using searcher As New ManagementObjectSearcher(query)
    Using mos As ManagementObjectCollection = searcher.Get
      Using en As ManagementObjectCollection.ManagementObjectEnumerator = mos.GetEnumerator
        If en.MoveNext() Then
          parentProcessId = CInt(en.Current.Item("ParentProcessId"))
        End If
      End Using 'en
    End Using ' mos
  End Using 'searcher

  If parentProcessId <> 0 Then
    AppActivate(parentProcessId)
    SendKeys.SendWait("~")
  End If
End Using 'currentProcess

这篇关于将ENTER键发送到VB.NET中的表单应用程序中的控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆