0

I'm trying to develop a Visual Basic console application to optimise a structure using Genetic Algorithms. The software I'm using occasionally throws popups to save the updated structure. I want to write a piece of code that simulates key presses (I want to press the enter key) on the active window so that I don't have manually press the enter key every 15 minutes.

I tried using

    SendKeys.SendWait({"ENTER"})

but there's no SendKeys in Console Application.

Blackwood
  • 4,504
  • 16
  • 32
  • 41
  • Can you be more specific?...you are throwing `MessageBox` or something like that?...if so, why are you doing that instead of writing to a log or something similar? – Hackerman May 24 '18 at 18:57
  • 2
    Project > Add Reference > pick System.Windows.Forms. Do talk to the owner/developer of this library, hacks like this have a knack for causing more problems than solutions. Consider for example that no user is going to stare at that console window for 15 minutes. – Hans Passant May 24 '18 at 19:04
  • What is pop-up? WPF Window? Windows Forms? – JohnyL May 24 '18 at 19:16
  • Take a look at AutoIt (https://www.autoitscript.com/site/autoit/). You can easily create a script, and then compile it into an executable that you can call from you VB code. You can capture a process handle when you launch it so that you can kill it when not needed. – JerryM May 24 '18 at 20:12
  • @Hackerman It's like a window that appears when you try to close a program saying "Save", "Don't save" and "Cancel". By default Option Selected on that window is Save and i just want to press enter. Just in my case options are such that i can choose them only when that window finally appears. – apoorv srivastava May 26 '18 at 17:30
  • @HansPassant I tried that. although the program compiles. it seems to have no effect. – apoorv srivastava May 26 '18 at 17:31

2 Answers2

0

try this one

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

and simulate keypess with this

 keybd_event(Keys.Enter, MapVirtualKey(Keys.Enter, 0), 0, 0)
 Threading.Thread.Sleep(10)
 keybd_event(Keys.Enter, MapVirtualKey(Keys.Enter, 0), &H2, 0)
  • Did you see @Hans Passant comment? Much cleaner and easier. – Mary May 24 '18 at 20:18
  • if you ok with adding windows forms then why dont you just simply create a new windows form application instead of a console application? – Suleyman DOGAN May 24 '18 at 20:21
  • The [**`keybd_event` function**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx) is deprecated as of a couple of years ago. Please don't use it. Have a look at [**`SendInput`**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx) instead, or my [**InputHelper library**](https://github.com/Visual-Vincent/InputHelper) which is kind of a wrapper for it. – Visual Vincent May 25 '18 at 05:35
  • @snoopcommands When i try to do that first thing that goes red is that i can not declare it as shared and Second it says that MapVirtualKey is not declared. – apoorv srivastava May 26 '18 at 16:50
  • @VisualVincent Can you explain a bit more about how i use SendInput or InputHelper Library. Thank You. – apoorv srivastava May 26 '18 at 16:52
  • @apoorvsrivastava : In your case I rather recommend following [Hans Passant's comment](https://stackoverflow.com/questions/50516293/sending-keypresses-in-visual-basic-console-application/50517433?noredirect=1#comment88045410_50516293), but if you are still interested in `InputHelper` here's a quick introduction: https://stackoverflow.com/a/49603724/3740093 – Visual Vincent May 26 '18 at 16:57
  • @apoorvsrivastava : `InputHelper.Keyboard.PressKey()` would be of your interest. Visual Studio's IntelliSense will display information about it as you type (though you can also place the caret at it and press F12 for a full overview of the library). – Visual Vincent May 26 '18 at 16:59
  • @VisualVincent I'm sorry but i'm new to visual basics and visual studio. can you please help me by telling how to add your solution to my project it'll be really great if you could just help me out. Thank you. – apoorv srivastava May 26 '18 at 17:54
0

To use my InputHelper library follow these steps:

  1. Download the library (not the source code) from GitHub:
    https://github.com/Visual-Vincent/InputHelper/releases

    Download the InputHelper library

  2. Extract the .zip-file somewhere.

  3. In Visual Studio, right-click your project in the Solution Explorer and press Add Reference....

    Project > Add Reference...

  4. Go to the Browse tab and locate the extracted folder from step 2, then select the InputHelper.dll file.

    Locate InputHelper.dll

  5. Now do step 3 again, but this time go to the .NET tab and import System.Windows.Forms (this is only needed if you are using a Console Application).

    Add a reference to System.Windows.Forms

  6. Finally, import the InputHelperLib namespace as well as System.Windows.Forms by writing this in the top of your code file:

    Imports System.Windows.Forms
    Imports InputHelperLib
    

Now you can use the InputHelper.Keyboard.PressKey() method to programmatically press a key:

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

KEEP IN MIND that if you are going to distribute this you must also include InputHelper's LICENSE.txt file with every copy of your application.

You may rename the file as long as the user still understands what it is for.

InputHelper currently consists of four main categories:

  • InputHelper.Hooks: Classes for capturing system-wide mouse and keyboard input. This can be used to make hot keys, for instance. See the project's wiki for implementation help.

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

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

  • InputHelper.WindowMessages: Methods for simulating mouse and keyboard input at a more virtual level. This utilizes window messages and can be used to target specific windows.

To explore the contents of my library right-click the namespace in your code and press Go To Definition.

This will open the Object Browser, in which you can browse the contents of my library, with all the types ("categories") on the left, and the methods/members on the right.

  • Right-click > Go To Definition

    Go To Definition

  • Browsing InputHelperLib's contents

    Browsing the contents of InputHelper

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75