473,765 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(lngMilli Sec As Long)
If lngMilliSec 0 Then
Call sapiSleep(lngMi lliSec)
End If
End Sub
'************** *** Code End *************** ******

' My Test Code Follows:

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

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

Mar 21 '07 #1
5 3029

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
7028
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 though the event works correctly? How can I get rid of this beep? I thought that setting the e.Handled to true would do it but no luck private void txtbxEmail_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e if ( (e.KeyCode == Keys.E)...
3
9234
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 click on a row (withs will turn blue then) and then click on it again . Now if I press any key the event will fire (except for the arrow keys). I also tried to override the IsInputKey => no luck. I also tried to override the ProcessCmdKey => With...
4
8637
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 System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRequestor.KeyDown If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
2
4174
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 PageUp, PageDn, Home, End, and arrow keys for scrolling the Panel control. Resizing the image using the and keys works fine, deriving a resized thumbnail from the image and reassigning it to the Image property of the PictureBox. However I'm...
1
3310
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 add a keydown event handler for some control on the form, say a textbox ... Private Sub txtBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBox.KeyDown) and then give that control focus the keydown...
0
1758
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 += new KeyEventHandler(ListBox_KeyDown); I have set a breakpoint in the event handler method ListBox_KeyDown but this method is never called. So this event KeyDown for control ListBox is never occuring.
3
5252
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 handler does not see the combination. Now this control is contained within another control which is contained within another. The top most control does see the CTRL+Z. I can easily pass down the key info, but why does the nested control see...
7
1818
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 the form does not triggered. Can anyone help me? Thanks a lot.
2
19295
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 have created for this purpose is derived from class UserControl.
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.