Imports System.Threading.Thread
Module Module1
Private Second As Integer = 1
Private Tick As New Timers.Timer(1000)
Sub Main()
AddHandler tick.Elapsed, AddressOf Ticker 'Event for the timer tick
Dim NewThread As System.Threading.Thread = New System.Threading.Thread(AddressOf LookForKeyPress) 'New thread to check for key press
NewThread.Start() 'Start thread
Console.WriteLine("Now awaiting key presses...")
tick.Enabled = True 'Enable the timer
End Sub
Private Sub Ticker()
'every tick this sub is called and the seconds are displayed till a key is pressed
Console.WriteLine(Second)
Second += 1
End Sub
Private Sub LookForKeyPress()
While True
Dim k As ConsoleKeyInfo = Console.ReadKey() 'read for a key press
If Not k.Key.ToString.Length <= 0 Then 'check the length of the key string pressed, mainly because the key in this case will be apart of the system.consolekey
Console.WriteLine(Environment.NewLine & "Key Pressed: " & k.Key.ToString) 'display the key pressed
Second = 1 'restart the timer
End If
End While
End Sub
End Module
Page 1 of 1
Waiting For Key Press: Console App
#1
Posted 09 November 2011 - 10:40 PM
Often I see the question "How do I check for a key press without stopping my code from being run?". I came up with a solution using threading, and timers. My program counts each second until a key is pressed, to which it displays the key pressed and restarts counting. It's not very complex at all, and granted there are probably many other ways to get it done than mine, I chose my method just to simply demonstrate it being done. Thank you. 
Replies To: Waiting For Key Press: Console App
#3
Posted 13 November 2011 - 03:24 PM
Very basic tutorial, lack depth. What if I want to do something based on the key pressed? How do I that?
Note: If your use .net4.0 or higher I strongly suggest using the task based approach rather that threads. url="http://www.dreamincode.net/code/snippet5821.htm"]See snippet.[/url]
Note: If your use .net4.0 or higher I strongly suggest using the task based approach rather that threads. url="http://www.dreamincode.net/code/snippet5821.htm"]See snippet.[/url]
#4
Posted 14 November 2011 - 07:29 PM
Thanks Adam I just wanted to throw a basic example for those who google or search cause I see it come up a lot. Appreciate the input it's my first tutorial.
#5
Posted 22 November 2011 - 05:18 PM
Hi,
Great tutorials, it run smoothly. I ask a favor, how can do that like a simple game.
let say typing a words using timer like your sample program. the interval is 10 seconds and if the user
can type a certain words within the range of time they minus the given life a total of 3. at the same time has done, the total score will display.
Thanks in advance!!
Great tutorials, it run smoothly. I ask a favor, how can do that like a simple game.
let say typing a words using timer like your sample program. the interval is 10 seconds and if the user
can type a certain words within the range of time they minus the given life a total of 3. at the same time has done, the total score will display.
Thanks in advance!!
#6
Posted 22 November 2011 - 05:50 PM
Hi there will discuss in private message later today.
Page 1 of 1
|
|





MultiQuote






|