Results 1 to 7 of 7

Thread: [RESOLVED] KeyUp or KeyDown? : What to consider when choosing.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Resolved [RESOLVED] KeyUp or KeyDown? : What to consider when choosing.

    This is one of those questions where I'd like the experts to please express their thoughts on.


    The action in question: This will trigger the ZOOM IN or OUT of my price chart.

    Now, I already have the code in two test buttons. A "+" button, and a "-" button.

    I'll be deleting these buttons because the action to ZOOM IN will be activated by pressing the "-" key. The ZOOM OUT will be the "+" key.

    Now all I have to do is choose:

    The KeyDown event or the KeyUp event.

    Note: No other key will be held down or pressed in conjunction. Just a quick click of the "+" or "-" key will do the trick.


    How does one normally decide and based on what conditions?

    TIA

  2. #2
    Fanatic Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    960

    Talking Re: KeyUp or KeyDown? : What to consider when choosing.

    When you keep a key pressed the "KeyDown" event starts repeating itself, while the "KeyUp" event requires separate keystrokes every time.

    Of course there's also the "KeyPress" event which combines the two, so it's up to you which one you prefer.

  3. #3
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,632

    Re: KeyUp or KeyDown? : What to consider when choosing.

    Code:
    Dim LastKey%
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = LastKey Then Exit Sub
        LastKey = KeyCode
        Debug.Print "KeyDown: " & KeyCode
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        LastKey = 0
    End Sub
    if don't like the repeat thing while also want to have the "Down" as the trigger not the "Up", u can do something like this.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: KeyUp or KeyDown? : What to consider when choosing.

    @VanGoghGaming @baka

    Your responses make the answer clear.

    In my current task, I prefer that the action occurs once and that another press is required if you want to do again.

    So the 'repeating' part tells me to go with KeyUP when repeating is not what you want...OR...

    If you just have to have KeyDown (for whatever reason), check if that key was already pressed last time and get out of dodge.

    Thanks!

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,467

    Re: [RESOLVED] KeyUp or KeyDown? : What to consider when choosing.

    I always start assuming I want KeyPress. If that doesn't work for my purpose at hand I consider KeyUp. In rare cases I might choose KeyDown, but mainly to cancel a key.

    Each has its uses and limitations.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,143

    Re: [RESOLVED] KeyUp or KeyDown? : What to consider when choosing.

    What VanGogh said for KeyDown.
    Usually the decision is, if you want to validate the pressed key before displaying it, which wouldn't be the case for you.

    On a sidenote: You might think about evaluating the Shift-Key.
    Zoom-In is "-".
    "-" individually is, say, zoom in by 5%
    SHIFT+"-" is zoom-in, say, 25%
    ALT+"-" is zoom-in, say, 50%
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: [RESOLVED] KeyUp or KeyDown? : What to consider when choosing.

    Quote Originally Posted by Zvoni View Post
    What VanGogh said for KeyDown.
    Usually the decision is, if you want to validate the pressed key before displaying it, which wouldn't be the case for you.

    On a sidenote: You might think about evaluating the Shift-Key.
    Zoom-In is "-".
    "-" individually is, say, zoom in by 5%
    SHIFT+"-" is zoom-in, say, 25%
    ALT+"-" is zoom-in, say, 50%
    That's a good idea if I had more increments.

    But in my project case, it's about 2 presses to get to maximum display, and about 4 presses for the most zoomed-in.

    So adding Shift would be overkill.

    If the increments were smaller and more of them, then I could see using 'Shift' to make bigger jumps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width