Capture Text from CMD Window That Is Already Running (VB.NET)

前端 未结 1 1253
一生所求
一生所求 2021-01-24 21:33

There is an application that runs pretty much 24/7 on this computer. It is run inside a command prompt Window. I would like to be able to capture all of the text currently displ

1条回答
  •  隐瞒了意图╮
    2021-01-24 21:45

    I've written a library called InputHelper which could come in handy here. It includes different methods of performing input simulation, one being sending it to a specific window.

    Download from GitHub:
    https://github.com/Visual-Vincent/InputHelper/releases

    Its wiki is sadly far from complete, but the library itself includes XML documentation describing every method inside it (which is automatically shown by Visual Studio's IntelliSense when you select a method or member in the members list that pops up while typing).

    The library currently consists of four main categories:

    • InputHelper.Hooks: Classes for capturing system-wide (some times referred to as global) mouse and keyboard input. Can be used to make for instance hot keys.

    • InputHelper.Keyboard: Methods for simulating real keyboard input/key strokes.

    • InputHelper.Mouse: Methods for simulating real mouse input.

    • InputHelper.WindowMessages: Methods for simulating mouse and keyboard input at a more virtual level, for instance targeting specific windows. This utilizes window messages (thus SendMessage() and PostMessage()).

    The last one mentioned would be the one of your interest. Using InputHelper.WindowMessages.SendKeyPress() you can send a specific key stroke to a window of your choice or, if omitted, the currently active window.

    Something like this should work:

    Dim hWnd As IntPtr = GetWindowHandle("programname.exe")
    
    'Send CTRL + A twice.
    InputHelper.WindowMessages.SendKeyPress(hWnd, Keys.Control Or Keys.A)
    InputHelper.WindowMessages.SendKeyPress(hWnd, Keys.Control Or Keys.A)
    
    'Send ENTER.
    InputHelper.WindowMessages.SendKeyPress(hWnd, Keys.Enter)
    

    Note that doing Keys.Control Or Keys.A sends the combination CTRL + A, however this works only when using either Keys.Control, Keys.Shift or Keys.Alt (or a combination of them). Using any other keys (for instance Keys.A Or Keys.B or Keys.ControlKey Or Keys.A) won't work.

    0 讨论(0)
提交回复
热议问题