473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 3003

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter | last post by:
Hello Thanks for reviewing my question. I am trying to consume the Keydown event of the Cntrl-shift-E in a textbox and have noticed that when I press key this combination a beep is made even...
3
by: bardo | last post by:
I have a Datagrid that is inside a panel. I want to use the keyDown event to reconize the arrow keys. But I have no luck at all. The problem is that the keydown event won't fire at all, unless I...
4
by: Anne | last post by:
hie again, i have 3 textbox and i would like the user to go to the next textbox by pressing the 'ENTER' key. i have tried using this: Private Sub txtRequestor_KeyDown(ByVal sender As...
2
by: Phil Galey | last post by:
I have a Panel control docked on all sides on a form and the panel control contains a PictureBox. I'm using the KeyDown event of the form to respond to the and keys for resizing the image and the...
1
by: fripper | last post by:
I have a VB 2005 windows app and I want to recognize keydown events. I have a form key down event handler but it does not get control when a key is depressed. In playing around I found that if I...
0
by: tony | last post by:
Hello!! I have a derived class called StringClassEditor which inherit from UITypeEditor listed below. Now to my question in method EditValue in this class I have this statement lb.KeyDown ...
3
by: MLM450 | last post by:
I have a control that handles the KeyDown event but it does not seem to execute when a combination of keys is pressed - like CTRL+Z. If I press CTRL, it executes. If I press Z, it executes. But the...
7
by: win | last post by:
I'm convert a VB6 program to .Net platform. My program use function key (F12) to close a form. Now I found that the coding in the keydown event of control triggered, the coding in the keydown of...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.