Hi,
Could anyone give me some suggestion?
How can I detect the button is pressed continuously? Actually, I use Command1_MouseDown , but it is not succeed.

I would like to do the application continuously if the button is pressed. If the user release (mouse up), I will stop the application.

How should I do?

Best regards,
zawpai

Recommended Answers

All 7 Replies

when you say running the application continuously, i'm assuming you mean there's looping going on?

alright... well, there are a few ways you could do it... easiest way might be defining a boolean variable... for instance

option explicit
private bRunning as boolean

then you can set the variable and run whatever you're running with the mouse down event...

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 'set the variable
 bRunning = true
 'run the program
 call doLoopedStuff
end sub

then with the mouse up event you terminate it

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
 bRunning = false
end sub

all this is nice and all, but won't really help much if your program is looping intencely and doesnt' pay attention to the events... so be sure to DoEvents

private sub doLoopedStuff()
 do while(bRunning=true)
  doEvents
  'your code stuff
 loop
end sub
commented: Absolutely Well Done. +8

Hello kain_mcbride,

Thank you for your information.

I confuse MouseDown and KeyPress . So, I posted it. You know it is doing the looping automatically if we use Form_KeyDown and press any key.

So, I want to make sure them clearly. Can I ask you one more question? What the difference between MouseDown and Click ?

Best regards,
zawpai

hi kain_mcbride,

I have tried the method you gave me. It is working well, but

private sub doLoopedStuff()
 do while(bRunning=true)
  doEvents
  'your code stuff
 loop
end sub

have one problem. If we change it to

private sub doLoopedStuff()
 do while(bRunning=true)
  'your code stuff
  doEvents
 loop
end sub

You know, after doing doEvents , our program should Exit Do .

Thank a lot.
zawpai

Can I ask you one more question? What the difference between MouseDown and Click ?

MouseDown is when you push the mouse button down... it's not a full click, just when you press the button... Click is a full click... mouse down / mouse up in close succession over the same object / location...

typicly click would be used... just detects if the user has clicked something in a normal kind of way... however, i understood your specifications to say that the program would run continually until the button was released... so, mouse down fires where as click won't... then you can use mouse up later to break the loop :)

private sub doLoopedStuff()
 do while(bRunning=true)
  'your code stuff
  doEvents
 loop
end sub

You know, after doing doEvents , our program should Exit Do .

Thank a lot.
zawpai

hello zawpai

I don't fully understand, and may have misunderstood your initial problem. You were having problems with the MouseDown event, which you were trying to use to cause your program to run continually until the button was released. I assumed that you meant that you had a section of code that you wanted to run repeatedly while the button was held down.

As such, i put it in a do while loop, with the clause that bRunning had to be true for the loop to continue... DoEvents prevents the program from taking full priority in the loop, causing the events that should be firing to fire vs waiting in the loop for whatever clause or justification that needs to be met to be met...

Anyway, i'm not really sure what you mean now. :(

Hello kain_mcbride,

MouseDown is when you push the mouse button down... it's not a full click, just when you press the button...

Click is a full click... mouse down / mouse up in close succession over the same object / location...

Thank you for your good explanation. Now, I am clear about it.

(Toggle Plain Text)

private sub doLoopedStuff()
 do while(bRunning=true)
  'your code stuff
  doEvents
 loop
end sub

You know, after doing doEvents , our program should Exit Do .

Sorry for above case, I didn't mean you are wrong. You are right.
Because of my application, I just change the sequence.:)

Thank you for your helping.

zawpai

using ascii checking on your button event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.