Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Key Press Event on command button 2

Status
Not open for further replies.

Kflasph

Programmer
Apr 8, 2002
65
US
How can I tell if a command button is being pressed and held in the down position ?

Thanks in advance,
KFlasph
 
KeyPress event (from F1 Help):
Occurs when the user presses and releases an ANSI key.
ANSI - American National Standards Institute (ANSI) 8-bit character set used to represent up to 256 characters (0–255) [blue]using your keyboard[/blue]. The first 128 characters (0–127) correspond to the letters and symbols on a standard U.S. keyboard. The second 128 characters (128–255) represent special characters, such as letters in international alphabets, accents, currency symbols, and fractions.

You may want to use MouseDown and MouseUp events:
Code:
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Caption = "Down"
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Caption = "Up"
End Sub

Have fun.

---- Andy
 
What I want to do is have an event continue as long as the user is pressing the command button down.

For instance
Private Sub cmdDown_Click()
lblarrow.caption = lblarrow.caption & "-"
End Sub

So if the user is holding down the command button it would create "------------" and continue to add as long as they hold the button down.

Thanks,
KFlasph
 
The click event also fires when the mouse button is let up. That means If you're already using the click event, you don't need the mouseup as well.
 
If you mean "pressing the command button down" by using your mouse, how about this:

On the Form - put Command button, a Label and a Timer, paste this code:
Code:
Option Explicit

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label1.Caption = ""
    Timer1.Enabled = True
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Timer1.Enabled = False
End Sub

Private Sub Form_Load()
    Timer1.Enabled = False
    Timer1.Interval = 500
    Label1.AutoSize = True
    Label1.Caption = ""
End Sub

Private Sub Timer1_Timer()
    Label1.Caption = Label1.Caption & "-"
End Sub


Have fun.

---- Andy
 
An adaptation of Andrzejek's code:
Code:
Public downCount As Long

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    downCount = 0
    Timer1.Enabled = True
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Timer1.Enabled = False
    
    MsgBox "Button held down for " & downCount / 10 & " seconds."
End Sub

Private Sub Form_Load()
    Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
    downCount = downCount + 1
End Sub


 
Star's for both of you ! That did the trick !

Kflasph
 
Again, for both of those, you should be able to use the Click event instead of the mouseup event. I have to go to a meeting so I can't test it right now, but I'll check it out later if someone hasn't already.
 
Bob, I tried the Click event, it fires after you let go of the button. You need the MouseDown and MouseUp events to start and stop the timer.


 
Do not have a VB installation to check, for those who have it, you might want to check the following series of actions.

1. Mousedown on the command button
2. Drag your mouse without leaving the button.
3. Mouseup when the mouse is outside the client area of the command button/form.
4. Did the timer stop?

It might have stopped, but sadly i dont have the capability to test at the moment.

 
Hence my comment about 'very different behaviour'

If you use the originally proposed solution (MouseDown and MouseUp) then yes, the timer stops under the conditions you describe

If you use the suggested variant (MouseDown and Click), it doesn't.
 
vbSun - I tried your experiment. The timer does indeed keep going if the pointer leaves the area of the button, or even the form. If you let go of the mouse button when pointer is away from the command button, the Click event does not fire.

If the OP doesn't want the timer to continue when the pointer leaves the button, I guess he code write code in the MouseMove event, checking if the coordinates are still in the area of the button. If they aren't, he could stop the timer at that point.


 
Ok, so the click event doesn't fire if you drag the mouse off the button before you let it go, so you have to use mouseup. So, we tested a possible efficiency improvement hypothesis and found it wanting. Score one for the scientific method...:)
 
Well, I was going to say "we (with the exception of strongm and possibly others) tested", but I was on my way to a meeting so I was in a hurry.

Maybe I'm the only one who didn't know, in which case, well, score one for the scientific method. :)
 
Because I've put my foot in my mouth so many times here, I usually fire up Visual Studio even when I think I know. I've got a Frakenstein MDB file now because of the Access forums. I am afraid the random convergence of code will one day ahieve sentience and take control of my computer.


 
<I am afraid the random convergence of code will one day ahieve sentience and take control of my computer.

And we now leave Mr. AtWork to his musings, secure in the belief that he is something more than a random convergence of code that has achieved sentience and taken control of his computer. For beliefs are the only reality in....The Twilight Zone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top