Although this should work it is important to not that this is unreliable for 2 reasons:
It doesn't work if the user clicks and release faster than your program takes for one loop.
It doesn't work if the user clicks, released and then clicks again faster than your program.
BUT, since on modern machines you can do this so fast that no human could really keep up, you can usually ignore both of these.