在Visual Basic控制台应用程序中发送按键

时间:2018-05-24 18:52:57

标签: vb.net visual-studio

我正在尝试开发一个Visual Basic控制台应用程序,以使用遗传算法优化结构。我正在使用的软件偶尔抛出弹出窗口来保存更新的结构。我想在活动窗口上写一段模拟按键的代码(我想按回车键),这样我就不用每15分钟手动按一次回车键。

我尝试使用

    SendKeys.SendWait({"ENTER"})

但是控制台应用程序中没有SendKeys。

2 个答案:

答案 0 :(得分:0)

尝试这个

<DllImport("user32.dll")> _
Private Shared Sub keybd_event(bVk As Byte, bScan As Byte, dwFlags As UInteger, dwExtraInfo As UInteger)
End Sub

并使用此

模拟keypess
 keybd_event(Keys.Enter, MapVirtualKey(Keys.Enter, 0), 0, 0)
 Threading.Thread.Sleep(10)
 keybd_event(Keys.Enter, MapVirtualKey(Keys.Enter, 0), &H2, 0)

答案 1 :(得分:0)

要使用我的InputHelper library,请按以下步骤操作:

  1. 从GitHub下载库(不是源代码):
    https://github.com/Visual-Vincent/InputHelper/releases

      

    Download the InputHelper library

  2. 在某处提取.zip - 文件。

  3. 在Visual Studio中,右键单击Solution Explorer中的项目,然后按Add Reference...

      

    Project > Add Reference...

  4. 转到Browse标签,找到步骤2中解压缩的文件夹,然后选择InputHelper.dll文件。

      

    Locate InputHelper.dll

  5. 现在再次执行第3步,但这次转到.NET标签并导入System.Windows.Forms(只有在使用控制台应用程序时才需要这样做。)

      

    Add a reference to System.Windows.Forms

  6. 最后,通过在代码文件的顶部写下InputHelperLib命名空间以及System.Windows.Forms来导入:

    Imports System.Windows.Forms
    Imports InputHelperLib
    
  7. 现在,您可以使用InputHelper.Keyboard.PressKey()方法以编程方式按键:

    Imports System.Windows.Forms
    Imports InputHelperLib
    
    Module MainModule
    
        Public Sub Main()
            'Some code here...
    
            InputHelper.Keyboard.PressKey(Keys.Enter)
    
            'Some code here...
        End Sub
    End Module
    
      

    保持谨慎如果您要分发此内容,则还必须在 的每个应用副本中包含InputHelper的LICENSE.txt文件

         

    只要用户仍然了解文件的用途,您就可以重命名该文件。

    InputHelper目前包含四个主要类别:

    • InputHelper.Hooks :用于捕获系统范围鼠标和键盘输入的类。例如,这可用于制作热键。有关实施帮助,请参阅project's wiki

    • InputHelper.Keyboard :模拟物理键盘输入/击键的方法。

    • InputHelper.Mouse :模拟物理鼠标输入的方法。

    • InputHelper.WindowMessages :在更虚拟级别模拟鼠标和键盘输入的方法。这利用窗口消息,可用于定位特定窗口。

    要浏览我的库的内容,请右键单击代码中的命名空间,然后按Go To Definition

    这将打开Object Browser,您可以在其中浏览我的库的内容,左侧是所有类型(&#34;类别&#34;),右侧是方法/成员

    • 右键单击&gt;转到定义

        

      Go To Definition

    • 浏览InputHelperLib的内容

        

      Browsing the contents of InputHelper