-
Sep 23rd, 2023, 10:19 PM
#1
Thread Starter
Frenzied Member
[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
-
Sep 23rd, 2023, 10:35 PM
#2
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.
Last edited by VanGoghGaming; Oct 13th, 2023 at 04:00 PM.
-
Sep 24th, 2023, 01:48 AM
#3
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.
-
Sep 24th, 2023, 01:07 PM
#4
Thread Starter
Frenzied Member
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!
-
Sep 25th, 2023, 01:28 AM
#5
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.
-
Sep 25th, 2023, 01:35 AM
#6
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
-
Sep 25th, 2023, 08:12 PM
#7
Thread Starter
Frenzied Member
Re: [RESOLVED] KeyUp or KeyDown? : What to consider when choosing.
 Originally Posted by Zvoni
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|