KeyUp + KeyDown Event Handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ShaneO

    #1

    KeyUp + KeyDown Event Handler

    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.KeyEventA rgs) _
    Handles ListBox1.KeyUp, ListBox1.KeyDow n

    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.
  • Gregory Gadow

    #2
    Re: KeyUp + KeyDown Event Handler

    ShaneO wrote:
    [color=blue]
    > 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.KeyEventA rgs) _
    > Handles ListBox1.KeyUp, ListBox1.KeyDow n
    >
    > 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?[/color]

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

    Protected Sub CommonKeyHandle r( _
    ByVal sender As Object, _
    ByVal e As System.Windows. Forms.KeyEventA rgs, _
    ByVal SentBy As Byte)
    (insert code here)
    End Sub

    Private Sub Button1_KeyDown ( _
    ByVal sender As Object, _
    ByVal e As System.Windows. Forms.KeyEventA rgs)
    CommonKeyHandle r(sender, e, 0)
    End Sub

    Private Sub Button1_KeyUp( _
    ByVal sender As Object, _
    ByVal e As System.Windows. Forms.KeyEventA rgs)
    CommonKeyHandle r(sender, e, 1)
    End Sub
    --
    Gregory Gadow
    techbear@serv.n et


    Comment

    • ShaneO

      #3
      Re: KeyUp + KeyDown Event Handler

      >[color=blue]
      > Have you tried writing a common method, then having the KeyUp and KeyDown
      > events call it? Something like this...
      >
      > --
      > Gregory Gadow
      > techbear@serv.n et
      >
      >[/color]
      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.

      Comment

      • Chris

        #4
        Re: KeyUp + KeyDown Event Handler

        ShaneO wrote:[color=blue]
        > 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.KeyEventA rgs) _
        > Handles ListBox1.KeyUp, ListBox1.KeyDow n
        >
        > 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.[/color]

        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

        Comment

        • ShaneO

          #5
          Re: KeyUp + KeyDown Event Handler

          Chris wrote:[color=blue][color=green]
          >>
          >> 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?
          >>[/color]
          >
          > 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[/color]
          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.

          Comment

          Working...