Home All Groups Group Topic Archive Search About

KeyUp + KeyDown Event Handler

Author
6 Feb 2006 9:33 PM
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.

Author
6 Feb 2006 10:24 PM
Gregory Gadow
ShaneO wrote:

Show quoteHide quote
> 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
techb***@serv.net
Author
6 Feb 2006 11:00 PM
ShaneO
>
> Have you tried writing a common method, then having the KeyUp and KeyDown
> events call it? Something like this...
>
> --
> Gregory Gadow
> techb***@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.
Author
6 Feb 2006 11:00 PM
Chris
ShaneO wrote:
Show quoteHide quote
> 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
Author
6 Feb 2006 11:15 PM
ShaneO
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.