Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

KeyPress to Stop Program

Posted on 2006-07-21
5
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 64

Accepted Solution

by:
Fernando Soto earned 500 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 64

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

705 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