phillip Posted March 2, 2004 Posted March 2, 2004 Hi I want to be able to terminate my running code using the Escape Key. How would i Go about doing this. Phill Quote phillip Restall
rifter1818 Posted March 3, 2004 Posted March 3, 2004 Hmmm hadle the event form.keypress or form.keydown and check if its the escape key, just make sure that any long loops you have, have application.doevents in them. and when you exit the program (escape key) make sure to set the exit conditions for all your loops, example if the loop is do while running ..... loop then under keypress event if e.keyvalue = keys.esc.esc then running = false exit end if Quote
phillip Posted March 4, 2004 Author Posted March 4, 2004 I Tried your code but could not get it to work, Can you explain a bit further as im a bit of a noob. Thanks in advance Quote phillip Restall
DR00ME Posted March 4, 2004 Posted March 4, 2004 (edited) Maybe this little example helps ? the private sub could be also _KeyPress or _KeyDown. What kind of running code would you like to halt ? [Vb]Private Sub MyLovelyClass_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Escape Then 'Put the running "code" stopper inside here End If End Sub[/code] Edited March 4, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
phillip Posted March 4, 2004 Author Posted March 4, 2004 so how do i feed this into code that is continually looping? Sorry guys :confused: Quote phillip Restall
DR00ME Posted March 4, 2004 Posted March 4, 2004 (edited) I'm not sure but this is what I came up fast.... Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress If e.KeyCode = Keys.Escape Then MyBooleanVariable = False End If End Sub Do While (MyBooleanVariable=True) 'This is your loop Enddo ack... I think this doesnt work because it is busy in the loop maybe... damn my head is mixed up because I used to code in Atmel and it was kind of statemachine coding... Edited March 4, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
phillip Posted March 4, 2004 Author Posted March 4, 2004 I think this will work but i need to have it work from a button. This is what i was using to goto the old routine but it no longer functions? Private Sub Update1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Update1.Click MyLovelyClass_KeyUp() End Sub Quote phillip Restall
DR00ME Posted March 4, 2004 Posted March 4, 2004 Umm, I'm not sure if I understand what you want but erm.... If you press ESC it will press btnStop on your form... Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress If e.KeyCode = Keys.Escape Then btnStop.PerformClick() End If End Sub how is this ? Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
phillip Posted March 4, 2004 Author Posted March 4, 2004 Maybe im better of explaining what im doing again. I have a button that when clicked goes to my SUB RUNMYCODE Using RUNMYCODE() I then have an Array of names I use For each myname in names 'Write a macro file and execute it in a Software package Next myname I want to be able to exit the Code by using the escape key in the for each loop because the operation can take up to an hour. Quote phillip Restall
DR00ME Posted March 4, 2004 Posted March 4, 2004 (edited) Dim flag as boolean = false For each myname in names Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress If e.KeyCode = Keys.Escape Then flag = true End If End Sub if flag = true then Exit end if Next myname lol try this.... I have no idea if it works.. now I wonder if it exits the IF END IF or the for loop... ack... You could also try to put the private sub outside of the for each loop.... I got an new idea... how about if you cause an exception and exit the code with it ? Edited March 4, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
DR00ME Posted March 4, 2004 Posted March 4, 2004 (edited) Dim lol as byte '0-255 Try For Each myname In names Private Sub MyLovelyClass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyPress If e.KeyCode = Keys.Escape Then lol = 1337 'causes an overflow exception End If End Sub Next myname catch ex as exception end try I think my ideas are mad atm... can someone help maybe ? Edited March 4, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
phillip Posted March 4, 2004 Author Posted March 4, 2004 That didnt Seem to work for me. Thanks very much for trying though. I've given up for the day :D Quote phillip Restall
phillip Posted March 4, 2004 Author Posted March 4, 2004 I looked through some of my old vb6 code and found thid Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Function GetPressedKey() As String For cnt = 32 To 180 'Get the keystate of a specified key If GetAsyncKeyState(cnt) <> 0 Then GetPressedKey = Chr$(cnt) Exit For End If Next cnt End Function Sub Main() KeyPressed = GetPressedKey If KeyPressed <> "" Then Select Case KeyPressed Case "Q", "q" GoTo Quit end select I cant get it to work in vb.net though Quote phillip Restall
Heiko Posted March 5, 2004 Posted March 5, 2004 Have you considered to add the "application.doevents" line to your loop ? Possibly your application grabs up 100% CPU power, and no other processes can do anything, especially the system. This will result in a "deaf" program, because KeyPress events will takes ages until they are handled. Quote .nerd
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.