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

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 ?
Regards,
RGuillermoProject ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChloesDadCommented:
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()
        Me.Controls.Add(tb)
        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.
0
RGuillermoProject ManagerAuthor Commented:
CloesDad,
we are using console mode, please your advice
0
ChloesDadCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RGuillermoProject ManagerAuthor Commented:
Thank you very much!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.