-
Nov 16th, 2000, 06:17 AM
#1
Thread Starter
Fanatic Member
Hello,
I really hope some genius of you vb gurus can help me out here. I'm sure the solution must be simple, but *damn* I've been trying and I can't get out of it.
Let's say I've got a control1 on form1. In the Control1_Click Sub, I just have a counter, that works like this:
Private Sub Control1_Click
Dim a as Long
For a = 1 to 100000000
'What here??????
next a
End Sub
What I need is to be able to STOP this counter from going on when the ESCAPE key is pressed on the keyboard, AT ANY MOMENT.
I want just a stupid trigger that allows me to exit the Sub!
Right now, the counter simply has to finish before I can actually access to any other part of the program.
Please help me out!!
Thank you very very much in advance,
R.
-
Nov 16th, 2000, 06:31 AM
#2
Fanatic Member
Use DoEvents to release control. You would not want to call DoEvents in every loop so you might use:
Code:
If a Mod 1000 = 0 Then 'every thousand loops
DoEvents
End If
You could then use the form's KeyPress event to test for an Escape key...
Cheers,
Paul.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 16th, 2000, 06:34 AM
#3
PowerPoster
KeyboardProc function
All you need is to create the keyboard hook liek below:
Code:
'code in form
Option Explicit
Private Sub Command1_Click()
Dim a As Long
a = 0
Do While (a < 100000000) And Not STOP_PROC
a = a + 1
DoEvents
Loop
End Sub
Private Sub Form_Load()
STOP_PROC = False
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
UnhookWindowsHookEx hHook
End Sub
'Code in module
Option Explicit
Public Const WH_KEYBOARD = 2
Public Const VK_ESCAPE = &H1B
Public STOP_PROC As Boolean
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If idHook < 0 Then
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
Else
If (GetKeyState(VK_ESCAPE) And &HF0000000) Then STOP_PROC = True
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End If
End Function
-
Nov 16th, 2000, 06:39 AM
#4
Lively Member
Try this...
I would suggest the following:
Set the KeyPreview property of your form to true.
Set a module level variable ( provided your Control1_Click code is in the same form module ) to indicate if escape has been pressed.
Use the Forms KeyDown event to check for the escape key being pressed. If the Escape key is pressed then set your module level variable to true.
(Something along the lines of -
If keycode = vbKeyEscape then ModLevelVar = True)
Change your Control1_Click code to :
Private Sub Control1_Click
Dim a as Long
ModLevelVar = False
For a = 1 to 100000000
DoEvents
If ModLevelVar = True Then Exit For
---rest of code here
next a
End Sub
( Substitute the name of your variable for ModLevelVar )
Note that the KeyDown event is not invoked for the ESC key if the form has a CommandButton control with the Cancel property set to True.
Hope this helps!
-
Nov 16th, 2000, 06:50 AM
#5
Fanatic Member
You live and learn. I'd never evan heard of Windows Hook etc.
and I had forgotten about the Cancel = True property...
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 16th, 2000, 06:54 AM
#6
PowerPoster
Hi! Cenobite, you suggestion is easy & clear butit only applied when the form is in focus. You'll not get the same result when the form is lost focus.
-
Nov 16th, 2000, 07:00 AM
#7
Thread Starter
Fanatic Member
Thank you very much all of you, I'll try the Chris option tonight.
Basically, if I correctly understood it now I can use this system to periodically check if STOP_PROC = True in every form.
So, even if I use a Command1_Click to actually fire a Sub inside a module, this system should still work, just by putting in the Sub module, in the routines:
If STOP_PROC = true then
goto End
End if
Chris I hope I'm correct in my assumption, if you can confirm it to be it would help.
Thank you again!
R.
-
Nov 16th, 2000, 07:03 AM
#8
_______
<?>
This works.
Code:
Option Explicit
Private Sub Form_Load()
Form1.KeyPreview = True
End Sub
Private Sub Command1_Click()
Dim i As Double
For i = 1 To 1000000
Print i
Next i
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 27) Then Unload Me
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Nov 16th, 2000, 07:08 AM
#9
PowerPoster
wildcat_2000, you're right. As long as, you application is running and active. Then it will be able to detect the Escape.
-
Nov 16th, 2000, 07:12 AM
#10
Thread Starter
Fanatic Member
I
I seriously can't thank you enough, guys.
All of you.
I'm going to try these tonight at home, hope they'll work (I'm sure they'll do!). Otherwise you'll see me in postings again tomorrow ;-)
Best regards,
R.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|