C# How to Loop While Mouse Button Is Held Down

C# how to loop while mouse button is held down

To avoid using threads you can add a Timer component on your form/control and simply enable it on mouse down and disable it on mouse up. Then put the code you would normally put inside the loop in the Timer_Tick event. If you want to use System.Timers.Timer you can use the Timer.Elapsed event instead.

Example (using System.Timers.Timer):

using Timer = System.Timers.Timer;
using System.Timers;
using System.Windows.Forms;//WinForms example
private static Timer loopTimer;
private Button formButton;
public YourForm()
{
//loop timer
loopTimer = new Timer();
loopTimer.Interval = 500;/interval in milliseconds
loopTimer.Enabled = false;
loopTimer.Elapsed += loopTimerEvent;
loopTimer.AutoReset = true;
//form button
formButton.MouseDown += mouseDownEvent;
formButton.MouseUp += mouseUpEvent;
}
private static void loopTimerEvent(Object source, ElapsedEventArgs e)
{
//this does whatever you want to happen while clicking on the button
}
private static void mouseDownEvent(object sender, MouseEventArgs e)
{
loopTimer.Enabled = true;
}
private static void mouseUpEvent(object sender, MouseEventArgs e)
{
loopTimer.Enabled = false;
}

C# loop while mousedown button pressed

Use a BackGroundWorker. Perfect for your problem.

Put the loop function in the worker and start / stop the worker on mouse events.

Mouse Click Loops using buttons (C#)

Hook into the MouseDown and MouseUp events on the button. The MouseDown event should spawn a thread, or signal to the thread to begin executing the loop. The MouseUp event should signal to the thread to stop executing the loop.

Something like this:

public class InterruptibleLoop
{
private volatile bool stopLoop;
private Thread loopThread;

public void Start() {
// If the thread is already running, do nothing.
if (loopThread != null) {
return;
}

// Reset the "stop loop" signal.
stopLoop = false;

// Create and start the new thread.
loopThread = new Thread(LoopBody);
loopThread.Start();
}

public void Stop() {
// If the thread is not running, do nothing.
if (loopThread == null) {
return;
}

// Signal to the thread that it should stop looping.
stopLoop = true;

// Wait for the thread to terminate.
loopThread.Join();

loopThread = null;
}

private void LoopBody() {
while (!stopLoop) {
// Do your work here
}
}
}

Check MouseButtonState in loop

Here's a function for the left mouse button. The link has a menu on the left side of the screen that shows properties for the other buttons.

Exit while loop of mouseDown event

OK, I fixed it for myself.

I just did this:

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
mouseX = MousePosition.X - 20;
mouseY = MousePosition.Y - 40;
this.SetDesktopLocation(mouseX, mouseY);
}
}

Chargin energy and released it while mouse button is held down

GetMouseButtonUp only returns true on the single frame that the button was released. Your code yields then.

Lets walk through it:

if (Input.GetMouseButton(1) ...

The user has let go of their mouse as they wish to attack, so this is false.

else {
yield return null;
}

So you yield and wait for the next frame. Then when execution resumes:

if (Input.GetMouseButtonUp(1))

This is false because the user released the mouse button last frame. You should nest this entire block inside the else-statement above the yield return null line.

SetWindowsHookEx, execute code while mouse button is pressed

I can't exactly judge what's your intention, but guess you need to combine the WM_RBUTTONDOWN and WM_RBUTTONUP to achieve what you want. While WM_RBUTTONDOWN, start a loop to do something, while WM_RBUTTONUP, stop doing.

private static IntPtr ButtonHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{

if (nCode >= 0)
{
if (wParam == (IntPtr)WM_RBUTTONDOWN)
{
Start();
}

else if (wParam == (IntPtr)WM_RBUTTONUP)
{
Stop();
}
}

return CallNextHookEx(MainWindow._hookId, nCode, wParam, lParam);
}

private static void Satrt()
{
_toStop = false;

while (true)
{
if (_toStop)
{
_toStop = false;
return;
}

DoTask();
}
}

private static void Stop()
{
_toStop = true;
}

private static void DoTask()
{
// do something
}

private static bool _toStop;

See if left mouse button is held down through Powershell?

Ok so it turns out I needed to add...

[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);

for GetAsyncKeyState to work and that was saying that DllImport wasn't found. I also needed to add using System.Runtime.InteropServices; at the top for DllImport to work as well. Now it works perfectly, thank you.



Related Topics



Leave a reply



Submit