Solved

KeyPress to Stop Program

Posted on 2006-07-21
5
339 Views
Last Modified: 2011-10-03
I would like to allow user to stop execution of a subroutine by hitting the Escape key.  The subroutine is a program module that is called from a form.  I can make KeyPress events work within the calling form, but they arent trapped while the subroutine is running.
Bill
0
Comment
Question by:BW999
5 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17155418
You'll have to Thread your subroutine if you want to capture key events while it is executing.

  Dim t As System.Threading.Thread

    Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Start the subroutine Thread
         t = New System.Threading.Thread(AddressOf Me.MyProcess)
         t.Start()
    End Sub

    Private Sub MyProcess()
         .
         'Put your subroutine code here
         .
    End Sub

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Escape Then
            t.Abort()
            MsgBox("SUBROUTINE ABORTED")
        End If

        Return MyBase.ProcessDialogKey(keyData)
    End Function
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 125 total points
ID: 17155467
Hi BW999;

In the subroutine you must have a loop. While the system is running this subroutine no events are being processed. Place a Application.DoEvents() in the loop in the subroutine. It should do the trick.

Fernando

0
 
LVL 3

Expert Comment

by:HouseMusic
ID: 17156693
btw... if you want it to work if your application is not active you are going to have to do a global keyboard hook
0
 

Author Comment

by:BW999
ID: 17156967
Fernando's Answer - short & sweet.  Thanks.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17157015
No problem, glad I was able to help. ;=)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now