By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,163 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,163 IT Pros & Developers. It's quick & easy.

KeyUp + KeyDown Event Handler

P: n/a
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.
Feb 6 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ShaneO wrote:
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
--
Gregory Gadow
te******@serv.net
Feb 6 '06 #2

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

--
Gregory Gadow
te******@serv.net

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.
Feb 6 '06 #3

P: n/a
ShaneO wrote:
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
Feb 6 '06 #4

P: n/a
Chris wrote:

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?


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.
Feb 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.