Event Handlers (KeyPressEventArgs/KeyEventArgs)

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
I am pretty familiar with eventargs and have designed a few of my own recently (and I am proud of my work because it took forever). However I have a very basic question. I was looking through the posts and saw someone asking about the alt key pressed stuff and was trying to write something up and for some reason the form is just not catching the any key strokes at all here is what I have in there was trying to catch ALT + 3 but no such luck inserting breakpoints and catching any keys at the moment never have really dealt much with the keyevent args so do not know exactly what control or form or whatever catches them so I dont know what to put after the handles. so here is the snippet:

PrivateSub keypressed(ByVal sender AsObject, ByVal e As _ System.Windows.Forms.KeyPressEventArgs) HandlesMe.keypress

EndSub
Private _AltDown AsBoolean = False
Property AltDown() AsBoolean
Get
Return _AltDown
EndGet
Set(ByVal value AsBoolean)
_AltDown = value
EndSet
EndProperty
PrivateSub keydown_(ByVal sender AsObject, ByVal e As _ System.Windows.Forms.KeyEventArgs) HandlesMyBase.KeyDown
If e.Alt Then
Me.AltDown = True
Else
IfMe.AltDown Then
MsgBox(e.KeyData.ToString)
MsgBox(e.KeyCode.ToString)
MsgBox(e.KeyValue.ToString)
EndIf
EndIf
EndSub

naturally I know the obvious things that are missing like setting it to false on keyup and a possible select case statement to handle various other combinations.
 
One More question

This still is not really telling me what goes after the handles statement there or do I have to do this in a constructor on the form and then use the me.addhandler, address of sub() command?
 
No, if you want to capture key down/press/up for the FORM you have to enable its KeyPreview property. If you are handling one or more of these events for a specific control (like TextBox), then this property is irrelevant.
 
Back
Top