944,117 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3611
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 2nd, 2007
0

Couple of questions

Expand Post »
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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
Marks256 is offline Offline
117 posts
since Jun 2006
Mar 2nd, 2007
0

Re: Couple of questions

A single key press?

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

And the console.readline()
Last edited by iamthwee; Mar 2nd, 2007 at 3:02 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 3rd, 2007
0

Re: Couple of questions

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;

Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Marks256 is offline Offline
117 posts
since Jun 2006
Mar 3rd, 2007
0

Re: Couple of questions

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster
Marks256 is offline Offline
117 posts
since Jun 2006
Mar 3rd, 2007
0

Re: Couple of questions

Click to Expand / Collapse  Quote originally posted by Marks256 ...
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 :-|
Reputation Points: 347
Solved Threads: 13
Practically a Posting Shark
arjunsasidharan is offline Offline
812 posts
since Aug 2006
Mar 3rd, 2007
0

Re: Couple of questions

arjunsasidharan is right.
On your form properties set the KeyPreview to true. Then use code simular to this:
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
  2. Select Case e.KeyChar
  3. Case "k", "K"
  4. MessageBox.Show("k was pressed")
  5. Case "x", "X"
  6. Me.Close()
  7. End Select
  8. End Sub
Last edited by waynespangler; Mar 3rd, 2007 at 9:45 am.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Mar 3rd, 2007
0

Re: Couple of questions

It is a console app. I know how to do it in a windows app (gui), but not in the console.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Marks256 is offline Offline
117 posts
since Jun 2006
Mar 3rd, 2007
0

Re: Couple of questions

Then try this:
VB.NET Syntax (Toggle Plain Text)
  1. Module Module1
  2. Private theKey As ConsoleKeyInfo = New ConsoleKeyInfo
  3.  
  4. Sub Main()
  5. Do
  6. theKey = Console.ReadKey(True)
  7. Select Case theKey.Key
  8. Case ConsoleKey.X
  9. Exit Do
  10. Case ConsoleKey.N
  11. PrintScreen()
  12. End Select
  13. Loop
  14. End Sub
  15.  
  16. Private Sub PrintScreen()
  17. Console.Write("000,000.00", 1234.5F)
  18. Console.WriteLine()
  19. End Sub
  20.  
  21. End Module
Hope this is what you want.
Last edited by waynespangler; Mar 3rd, 2007 at 10:43 pm.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Mar 4th, 2007
0

Re: Couple of questions

Thank you!

Now could i have you explain how it works?

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

VB.NET Syntax (Toggle Plain Text)
  1. Private theKey As ConsoleKeyInfo = New ConsoleKeyInfo
  2. .....
  3. .....
  4. .....
  5. theKey = Console.ReadKey(True)
Reputation Points: 10
Solved Threads: 0
Junior Poster
Marks256 is offline Offline
117 posts
since Jun 2006
Mar 4th, 2007
0

Re: Couple of questions

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
VB.NET Syntax (Toggle Plain Text)
  1. 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...)
Reputation Points: 10
Solved Threads: 0
Junior Poster
Marks256 is offline Offline
117 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Very easy problem- i can't seem to get
Next Thread in VB.NET Forum Timeline: barcode conversion in vb.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC