|
-
Jul 16th, 2010, 02:48 PM
#1
Thread Starter
New Member
[RESOLVED] Detecting ESCAPE or ENTER key to control display
Hi,
This is my first post on this forum and am relatively new to VB6.
Although there are other similar posts regards my problem, I cannot get any to work for me.
The code (listed below) is a deliberately primitive attempt at a slide show,
without using picture or image boxes - the code is also just an independent BAS module, but can be invoked by a form, if need be.
It works well in that Paint Shop Pro is initiated only once and subsequent jpg files appear over each other after ? seconds specified in the SLEEP command.
My problem, is to replace the SLEEP line with code that allows me to use the ESCAPE or ENTER key to display the next jpg file.
Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal _
lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Main()
MyPath = InputBox("Specify full path of .xls file", "Query 1 of 1", "c:\jpg_files")
If MyPath = "" Then End
NextFile = Dir(MyPath & "\*.*")
If NextFile = "" Then MsgBox "Directory does not exist": End
Do While Len(NextFile) ' Repeats until length of NextFile is ZERO.
NextFile = Dir ' Gets next file name.
zitpath = MyPath & "\" & NextFile
ShellExecute 0&, vbNullString, zitpath, vbNullString, vbNullString, vbNormalFocus
Sleep 500
Loop
End
End Sub
I have spent many hours attempting to get this to work with no success.
Can anyone provide me with the solution ?
Thanks and regards,
Zorrozac
-
Jul 16th, 2010, 03:21 PM
#2
Re: Detecting ESCAPE or ENTER key to control display
Welcome to the forums
A suggestion: Set your form's KeyPreview property to true then respond to the form's KeyDown or KeyPress event. Here's an example of those events from the CodeBank section of the forum
-
Jul 16th, 2010, 04:16 PM
#3
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
Thank you LaVolpe for your link to an interesting and involved post.
The code I am using is an isolated BAS module and does not use any forms.
However, I have taken on board your reference to KEYPREVIEW and made a very simple module (listed below) that uses a form.
When I run the form and press ESCAPE, it works and ends the program.
However, when I run it and click on the command button to execute some looping msgboxes, it does not detect the ESCAPE key and continues to issue msgbox messages.
Does the answer to this problem possibly involve DOEVENTS or SETFOCUS ?
If it does, a few lines of code would be greatly appreciated.
If it does not, I still would like some lines of code that would solve this apparently simple problem.
Code:
Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Private Sub form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then
MsgBox "You have clicked on the ESC key": End
End If
End Sub
Private Sub Command1_Click() ' How can you breakout of this loop using the Escape key ?
For i = 1 To 100
MsgBox i
Next i
End Sub
Regards,
Zorrozac
-
Jul 16th, 2010, 05:42 PM
#4
Fanatic Member
Re: Detecting ESCAPE or ENTER key to control display
Why not use GetAsyncKeyState? I have noticed that that picks up any key that is pressed, even if the form is not focused.
-
Jul 16th, 2010, 06:14 PM
#5
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
Thank you Gamemaster1494 for your link, for which I have looked at the code, which is quite extensive.
I know that the principle employed on the forum is for the problemee to discover the answer for himself, but I have tried numerous attempts without success.
What I need is the simplest code that has to be added to the msgbox loop example above to make it exit once a key has been pressed ( i.e. ESCAPE or ENTER ).
If someone can add code to that listed above to make it work and either list it in this post or possibly provide me with a zip file containing the vbp file
then that would be perfect.
Regards,
Zorrozac
-
Jul 16th, 2010, 06:23 PM
#6
Fanatic Member
Re: Detecting ESCAPE or ENTER key to control display
What link are you talking about? Do:
Code:
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
If GetAsyncKeyState(vbKeyReturn) <> 0 Then
MsgBox("You Hit The Enter Key")
End If
If GetAsyncKeyState(vbKeyEscape) <> 0 Then
MsgBox("You Hit The Escape Key")
End If
-
Jul 16th, 2010, 07:30 PM
#7
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
Thanks a million Gamemaster1494 your code works brilliantly !
I put the PUBLIC statement in a BAS module and this is the code that I used:
Code:
Private Sub Command1_Click()
For i = 1 To 100
MsgBox i
If GetAsyncKeyState(vbKeyEscape) <> 0 Then MsgBox ("You Hit The Escape Key"): End
Next i
End Sub
I did try to put a simple DO-LOOP around the FOR-NEXT loop with the IF statement commented out, but it failed repeatedly.
Could you or anyone provide me with the few lines of code that can make it work with a DO-LOOP ?
I really do appreciate the help that you have given - Thanks again.
Regards,
Zorrozac
-
Jul 16th, 2010, 07:34 PM
#8
Fanatic Member
Re: Detecting ESCAPE or ENTER key to control display
So wait, are you wanting it to check the keys from value 0 to 100? If so, you may want to check here: http://msdn.microsoft.com/en-us/library/aa243025.aspx that is all the values of the keys.
For i = 1 to 100
msgbox i
If GetAsyncKeyState(i) <> 0 Then
Msgbox ("You hit the " & i & " key")
end if
next i
Last edited by Gamemaster1494; Jul 16th, 2010 at 07:53 PM.
-
Jul 16th, 2010, 07:52 PM
#9
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
Sorry - I did not make it clear what I wanted.
I simply want a DO-LOOP to perform while the ESCAPE key has not been pressed.
Code:
DO WHILE (Escape key not being pressed)
. . .
LOOP
or
Code:
DO
. . .
LOOP UNTIL (Escape key is pressed)
It appears that it should be very easy, but again I made a number of unsuccessful attempts using the GetAsyncKeyState value.
Thanks for your interest and help.
Zorrozac
-
Jul 16th, 2010, 08:52 PM
#10
Re: Detecting ESCAPE or ENTER key to control display
If you try showing a message box right after the escape key is detected using GetAsyncKeyState the msgbox will most likely close because ESC keys can close a msgbox.
One way might be to first make sure the ESC ley is not being pressed, then do your loop and detect the ESC key, once the loop exits wait until the ESC key is no longer held down so the msgbox can be shown.
Code:
Private Sub Command1_Click()
' make sure escape key is not already pressed
Do Until GetAsyncKeyState(vbKeyEscape) = 0
DoEvents
Loop
' loop until escape key is detected
Do Until GetAsyncKeyState(vbKeyEscape) <> 0
'< add your code here >
DoEvents
'< and/or your code here >
Loop
' wait till escape key is no longer pressed
Do Until GetAsyncKeyState(vbKeyEscape) = 0
DoEvents
Loop
' show message box
MsgBox "The escape key was pressed!", vbInformation
End Sub
-
Jul 16th, 2010, 09:26 PM
#11
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
Thank you Edgemeal for your interesting code.
I have tried various bits of your code by putting in a FOR-NEXT loop, but cannot get any of them to end after issuing a message that the ESCAPE
key had been hit.
On my version of VB6, hitting the ESCAPE key does not terminate the program which is looping through msgbox messages.
Could you or someone possibly provide me with the minimum amout of code that would include this simple FOR-NEXT loop that demonstrates it working.
If you can, that would put this problem to bed - Thanks.
Code:
For i = 1 to 100
Msgbox i
Next i
Regards,
Zorrozac
-
Jul 16th, 2010, 09:54 PM
#12
Fanatic Member
Re: Detecting ESCAPE or ENTER key to control display
how bout:
Code:
Do While GetAsyncKeyState(vbKeyEscape) = 0
'Code
Loop
-
Jul 16th, 2010, 10:47 PM
#13
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
Thanks for your continuing interest.
I have added the msgbox loop to your code and it still does not terminate when you hit the ESCAPE key.
Please try the following code for yourself and see if you can cure problem - Thanks again.
Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Command1_Click()
Do While GetAsyncKeyState(vbKeyEscape) = 0
For i = 1 To 100
MsgBox i
Next i
Loop
MsgBox "Hit ESCAPE key ?"
End Sub
Regards,
Zorrozac
-
Jul 16th, 2010, 10:51 PM
#14
Fanatic Member
Re: Detecting ESCAPE or ENTER key to control display
So, are you wanting it to stop going threw the mesagge boxes when you hit the escape key?
-
Jul 16th, 2010, 11:00 PM
#15
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
That is correct Gamemaster1494.
This latest code with the msgbox loop is a simplification of the original code.
My original listing at the top of this post displays succesive JPGs are controlled by the SLEEP statement.
It would be much better if I can hit a key after each JPG has been shown to advance to the next one.
Hence why I was after code that detected key presses.
I hope I have clarified the problem.
Regards,
Zorrozac
-
Jul 16th, 2010, 11:03 PM
#16
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
That is correct Gamemaster1494.
This latest code with the msgbox loop is a simplification of the original code.
My original listing at the top of this post displays succesive JPGs which are controlled by the SLEEP statement.
It would be much better if I can hit a key after each JPG has been shown to advance to the next JPEG image.
Hence why I was after code that detected key presses.
I hope I have clarified the problem.
Regards,
Zorrozac
-
Jul 16th, 2010, 11:04 PM
#17
Fanatic Member
Re: Detecting ESCAPE or ENTER key to control display
Code:
For i = 0 to 100
If GetAsyncKeyState(vbKeyEscape) <> 0 Then
Exit For
Else
msgbox i
End if
next i
Last edited by Gamemaster1494; Jul 16th, 2010 at 11:21 PM.
-
Jul 16th, 2010, 11:29 PM
#18
Thread Starter
New Member
Re: Detecting ESCAPE or ENTER key to control display
Thanks Gamemaster1494 that code works superbly.
Thank you for your interest, time, patience and skill in helping me solve this problem.
Very best regards,
Zorrozac
-
Jul 16th, 2010, 11:35 PM
#19
Fanatic Member
Re: Detecting ESCAPE or ENTER key to control display
No problem Zorrazac. Glad i could help. Please mark this thread resolved. To do so, you go to the blue bar above your first post. There it says "Thread Tools". Click on that, then click on the "Mark Thread Resolved" button. Glad I could help. =)
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
|