KeyUp + KeyDown Event Handler

S

ShaneO

I would like to handle the KeyUp & KeyDown events in the same event
handler but can't find how to determine which event was fired -

Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles ListBox1.KeyUp, ListBox1.KeyDown

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue
= Keys.End Or e.KeyValue = Keys.Home Then

'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

End Sub


Apart from duplicating the code in the individual KeyUp and KeyDown
Events, does anyone know how to determine which action triggered the
event handler in this case?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
G

Gregory Gadow

ShaneO said:
I would like to handle the KeyUp & KeyDown events in the same event
handler but can't find how to determine which event was fired -

Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles ListBox1.KeyUp, ListBox1.KeyDown

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue
= Keys.End Or e.KeyValue = Keys.Home Then

'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

End Sub

Apart from duplicating the code in the individual KeyUp and KeyDown
Events, does anyone know how to determine which action triggered the
event handler in this case?

Have you tried writing a common method, then having the KeyUp and KeyDown
events call it? Something like this...

Protected Sub CommonKeyHandler( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs, _
ByVal SentBy As Byte)
(insert code here)
End Sub

Private Sub Button1_KeyDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs)
CommonKeyHandler(sender, e, 0)
End Sub

Private Sub Button1_KeyUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs)
CommonKeyHandler(sender, e, 1)
End Sub
 
C

Chris

ShaneO said:
I would like to handle the KeyUp & KeyDown events in the same event
handler but can't find how to determine which event was fired -

Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles ListBox1.KeyUp, ListBox1.KeyDown

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue
= Keys.End Or e.KeyValue = Keys.Home Then

'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

End Sub


Apart from duplicating the code in the individual KeyUp and KeyDown
Events, does anyone know how to determine which action triggered the
event handler in this case?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.

How are you duplicating code if you want and if statement seperating the
two.

You can call the same sub from both if you want the two to runsome of
the same code, but not all.

Chris
 
S

ShaneO

Have you tried writing a common method, then having the KeyUp and KeyDown
events call it? Something like this...
Thanks for your reply Gregory.

I have thought about doing that, however that would require entries in 3
separate Event Handlers. (1 for KeyUp, 1 for KeyDown & 1 for the final
Event Handler).

I guess I really just like to have my code as compact as possible and by
having only one Handler to cover the two events it would help me in that
regard.

Maybe my desire isn't achievable with these events?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
S

ShaneO

Chris said:
How are you duplicating code if you want and if statement seperating the
two.

You can call the same sub from both if you want the two to runsome of
the same code, but not all.

Chris
Thanks Chris for your reply.

I suppose I should have written my example as -

If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or
e.KeyValue = Keys.End Or e.KeyValue = Keys.Home Then

'Do Some Stuff....
'Do More Stuff....
..
..
..
'If User "Pressed" key (KeyDown) then do something...
'If User "Released" key (KeyUp) then do something else...

End If

Please see my reply to Gregory Gadow regarding the option of creating a
common handler.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top