471,355 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

KeyDown Event Question

This follows a previous post, when I was trying to capture a key
pressed during the immediate opening of a form (ie in the first
3-secs before processing any of the code that followed it.

It was mentioned that the KeyDown event fires the instant a key is
pressed down.

So, if you place the following code in a form, you'll see my
concern.

Test1: First run the code 'as is', with the Msgbox off in the
Form_Open. Press any key the instant the form opens.
It seems the KeyDown event occurs during the pause and Msg1 shows the
key value.

Test2: Now UnComment the Form_Open msgbox and see what happens when
you press a key the instant the form opens. Its value is blank and
Msg1 is never revealed.

Any Ideas???

Thanks Greg

Private MyKey As Variant

'***************** Code Start *******************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Sub sapiSleep Lib "kernel32" _
Alias "Sleep" _
(ByVal dwMilliseconds As Long)

Sub sSleep(lngMilliSec As Long)
If lngMilliSec 0 Then
Call sapiSleep(lngMilliSec)
End If
End Sub
'***************** Code End *********************

' My Test Code Follows:

Public Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
MyKey = KeyCode
MsgBox "Msg1 " & MyKey
End Sub

Public Sub Form_Open(Cancel As Integer)
Dim I As Integer
sSleep (3000) 'stop the code for 3-secs
' MsgBox "Msg2 " & MyKey
End Sub

Mar 21 '07 #1
5 2899

Because the form must finish the Open event before other events can
run. MyKey does not contain a value in the Open event as KeyDown has
not yet occurred.

Mar 21 '07 #2
Because the form must finish the Open event before other events can
run. MyKey does not contain a value in the Open event as KeyDown has
not yet occurred.
Mike

In Test1:
It seems like the KeyDown does occur before the Open event of the Form
concludes.
The key is pressed in the 3-sec gap before the Open Form is finished,
and the MyKey variable reports the value in Msg1?

Mar 21 '07 #3
I don't think you understand the way it's working. You have to think
in linear terms and remember that the computer is doing things faster
than we can thunk it out.

In 'Test1' the Open event msgbox is disabled and nothing else is coded
after it. Therefore the event finishes a nano second before the form
is visible and other events are free to occur - but they won't be
processed until the timer is done. As a third test, use the 'Test1'
guidelines but instead of pressing a key, click the close button on
the form. It won't do anything until the timer stops. So in this test,
the keydown event still occurs and detects the pressed key, but
dosen't report it until the timer stops.

In 'Test2' there is code after the timer, the MsgBox. But since no
other processing occurs until the timer is done AND there is still
Open event code to run, the KeyDown event dosen't occur and you only
see Msgbox2. Since the KeyDown event has not happened yet (Open is
still running until the Msgbox goes away) the variable is not set and
the msgbox appears empty. As proof, give the variable an obvious value
(bigger than 255 or text) in the OpenEvent. In the first test it will
be replaced by the KeyCode and in the second it will display the Open
even value.

Mar 22 '07 #4
>Since the KeyDown event has not happened yet (Open is
>still running until the Msgbox goes away) the variable is not set and
the msgbox appears empty.
GOT IT ! I was thinking that KeyDown was being monitored during the
timer stop
not realizing that the form event needed to conclude before this would
happen. So,
the FormEvent did not conclude because the msgbox stopped it.

I'm training myself OnTheFly, but really need to attend some formal
training. Or, stop coding and start reading. But, I have to get my
project done! Catch22.

Thanks again for the great insight.

Greg

Mar 23 '07 #5

No problem.
I recommend the reading over classes anyday. You go at your pace,
spend as much time in application as you need, and it only costs a
book or two. Groups fill in the gaps.

Mar 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Peter | last post: by
3 posts views Thread by bardo | last post: by
4 posts views Thread by Anne | last post: by
1 post views Thread by fripper | last post: by
3 posts views Thread by MLM450 | last post: by
7 posts views Thread by win | last post: by
2 posts views Thread by Tony Johansson | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.