Is it possible to hold down mouse button for certain time using pynput?

Viewed 490

I know there are similar questions, but those are about the Keyboard Controller and I need the mouse Controller.

So I have a program, where I need to hold down lmb for a certain amount of time. I've tried this:

mouse.press(Button.left)
time.sleep(t)
mouse.release(Button.left)

But for some reason it presses the mouse once instead of holding for t seconds. So is there a way to do this? If there is, I'd like to see the implementation.

1 Answers

That code works. I've written a short C# program which starts a timer at mouse down and stops counting at mouse up. And it counts for roughly the amount of seconds that Python sleeps.

Here's the relevant C# WinForms code. All elements like Form, Timer and Label just have their default names:

private int x;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    x = 0;
    timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
    x += 100;
    label1.Text = x.ToString();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    timer1.Enabled = false;
}

If it doesn't work, then the program you are using is handling input differently and you need to find out how exactly it processes mouse input.

Related