Visual basic: how to capture any keyboard stroke without stopping the execution of the program

Posted on 2014-08-28
Last Modified: 2014-08-29
Hello Experts,
I need to continue the execution of my visual basic 2012 App without stopping execution but if the user types any key while executing I need to be able tocatch it.... somethingh like the old inkey$(),
How can I do this in VB2012 ?
Question by:rguillermo
    LVL 15

    Expert Comment

    This can be done assuming that a form is displayed on the screen. You need to add an event handler to each control to capture the keypress event

    Imports System
    Imports System.Windows.Forms
    Public Class Form1
        Inherits Form
        Public Sub New()
            ' Create a TextBox control. 
            Dim tb As New TextBox()
            AddHandler tb.KeyPress, AddressOf keypressed
        End Sub 'New 
        Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
            ' The keypressed method uses the KeyChar property to check  
            ' whether the ENTER key is pressed.  
            ' If the ENTER key is pressed, the Handled property is set to true,  
            ' to indicate the event is handled. 
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
                e.Handled = True 
            End If 
        End Sub 'keypressed
        End Class 'Form1

    Open in new window

    You can then add your code in the KeyPressed method, setting e.handled = true if you want to consume the keypress and not pass it to the control.

    Author Comment

    we are using console mode, please your advice
    LVL 15

    Accepted Solution

    It would be helpful if you put all relevant information in your post :)

    You would need to include this in your code

            If Console.KeyAvailable Then
                KeyPressed = Console.ReadKey(True).KeyChar
                ' Process the key
            End If

    ReadKey blocks, so that's why the check for keyavailable is performed.

    Author Closing Comment

    Thank you very much!!

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now