I am using Visual Basic 2005 express (.net)

1) How would one get a SINGLE keypress in the console? (the equivilant to INKEY$ in QBASIC)

2) crap, i forgot my other question! Oh well. Chances are i will remember in a few minutes :lol:

Member Avatar for iamthwee

A single key press?

What are you trying to do. I bet you can use a String for this instead.

And the console.readline()

I just want to catch a single key press, and make it do something. I want it so when a key is pressed, nothing is displayed on the screen (as console.readline() does...)

I just want a silent (non noticable) action to take place when a key is pressed. Something like this;

Please press 'k' to do this
Please press "x" to exit the program

I just want to press the key, and not have to worry about pressing return.

Bummer. I just read a few posts on other forums, and they all said that it can not be done. :(

Is there a way i could catch the key press from a WinAPI message?

Bummer. I just read a few posts on other forums, and they all said that it can not be done. :(

Is there a way i could catch the key press from a WinAPI message?

well why dont you try the keyPress event.. and is it a windows application or a console application.. if you are using windows then try using the forms-> keypress event :-|

arjunsasidharan is right.
On your form properties set the KeyPreview to true. Then use code simular to this:

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Select Case e.KeyChar
            Case "k", "K"
                MessageBox.Show("k was pressed")
            Case "x", "X"
                Me.Close()
        End Select
    End Sub

It is a console app. I know how to do it in a windows app (gui), but not in the console.

Then try this:

Module Module1
    Private theKey As ConsoleKeyInfo = New ConsoleKeyInfo

    Sub Main()
        Do
            theKey = Console.ReadKey(True)
            Select Case theKey.Key
                Case ConsoleKey.X
                    Exit Do
                Case ConsoleKey.N
                    PrintScreen()
            End Select
        Loop
    End Sub

    Private Sub PrintScreen()
        Console.Write("000,000.00", 1234.5F)
        Console.WriteLine()
    End Sub

End Module

Hope this is what you want.

Thank you! ;)

Now could i have you explain how it works? :)

I understand how most of it works, just not these two parts;

Private theKey As ConsoleKeyInfo = New ConsoleKeyInfo
.....
.....
.....
            theKey = Console.ReadKey(True)

Ok, i figured out what the "theKey = Console.ReadKey(true)" does (that is why my code didn't work. I didn't use the "(true)".

Anyways, i still can't quite figure out what the purpose of the

Private theKey As ConsoleKeyInfo = New ConsoleKeyInfo

is for

I know that it defines what theKey is, but why is it needed? (poor statement, yes, but i really don't see the need for it...)

It works basically like the inkey command.
dim x as string
x=inkey$

The variable that is to except the keyboard entry has to be defined. It is more like a class I figure. This then gives you access to information on the key.
The " = New ConsoleKeyInfo" is there so you don't get a warning that the key is used before it is initiated.

I am sorry, but i don't follow...:-|


I was able to run the code without having the "= New ConsoleKeyInfo" there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.