How much time should the mouse left button be held? - python

If you want to detect a Bot that is clicking on a button when he has to, could you look how long the left button stays down before being up again?
I mean a script like this one (python):
import win32api, win32con
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
Will click really fast. Probably faster than a human. And always at (really closely) the same speed. Could an anti-bot system detect that?
So you can add a time.sleep(float) instruction between the two mouse_event's. But should it be randomised? We probably have a slight difference of time between two clicks, less precise than a computed click.
So how much time should we make the sleep last (min and max of the randrange)?
It's probably too much effort for being undetected but is that possible?
So quick recap:
Can we detect a bot on the click speed and click time never changing even a bit?
How long does an average person press the button down before letting it go up? If I want to stay undetected should I randomise this time, and with which min and max?
(I'm not talking about other existing security like checking if you press the same pixel over and over.)

Yes, bots can and are detected this way.
Yes, adding a random offset to the click speed, delay, mouse path, movement speed, and many more things is the standard solution to circumvent this kind of detection. There are many other ways to detect bots though.
To get an average click speed, write a short program that logs the timestamps for mouseDown and mouseUp events and click in it.

This is an old question, but anyway, according to this paper here Transfer learning for behavioral biometrics
It shows that
the average mouse click lasts 85ms, with Q3 = 95ms, Q1= 75ms; with an upper fence of 135ms and a lower fence of 55ms.
Then you can turn this distribution into a gaussian for easy implementation.
To implement this in your code(approximately) in python, you can use NumPy normal distribution, with mean 85ms, and to calculate the standard deviation you can use the rule of thumb:
sd = IQR/1.35
And therefore, sd = 14.8148148, u=85ms

Related

Draw a Rectangle over an image and get the coordinates in python

I'm trying to develop a code that open an image where you can select a point quit the mouse and drag to form a rectangle until you don't release the left button.
Then from python I should receive the starting coordinates and the height and width in pixel of the rectangle, how can I do it?
I saw that the packages argparse and cv2 can be used, but I don't really know how to approach it.
I won't do the job for you but I'm willing to help.
You will need 2 blocks of code:
an image displayer
a mouse-event listener
To start, you may forget about the image displayer. You may concentrate on the mouse listener while you draw your rectangle anywhere on the screen.
Select a mouse listener library. There are many on pypi.org.
I propose pynput because it is easy to work with and is well documented.
read documentation (focus on "on_click")
write your code to implement your mouse listener. It's simple (less than 10 lines). At the end of your program, add a statement:
input(">")
run your program. Click anywhere on the screen and drag to another point. Release.
your on_click() function will be called twice (once for button press and once for button release). Record the two sets of X-Y coordinates (unit is pixels).
once the button is released, compute the size of the rectangle (in pixels).
press any key on the keyboard to end the program.
Once your program is working you may work on the imager. If the image is large, you may have to use a scaling factor to reduce it. You will have to introduce the scaling factor in your sizing equations.
When a program skeleton will exist, do not hesitate to ask questions.
Asking for help when there is no visible sweat will not bring you many answers.

Is there a way to use an f-curve to interactively retime keys in Maya?

WHAT: I'm looking for a way to create an f-curve to dynamically retime an arbitrary number of keys in Maya. Something like an Animation Layer, but for timing on a per-asset basis.
WHAT NOT: I've got a great linear retime-from-pivot script already. Used it for years, love it, not looking for that.
WHY: Lets say I've animated several goblins racing toward an enemy. Maybe they start the shot behind a gate or something because I don't want to change the timing of the start of the shot, and I don't want to change the end of the shot because I've already animated the hero striking them, and I can't change the length of the shot because the shot length is locked.
The director wants one of the goblins to get a little ahead while they are running, and then to slow back down into the current overall shot timing. This director is more of a "I'll know it when I see it" kind of guy so I expect several rounds of revisions, and he might throw in a stumble request later for all I know, so I want to be able to mute this retime (so I'm working on whole numbered keys) and have it non-destructive (no baking).
SUMMARY: So, I want to scale an arbitrary number of keys on a selected object by a gradient with an arbitrary start and stop for the retime that can be muted, removed, or adjusted non-destructively.
I'm thinking that setting an f-curve that will affect the timing of selected keys would be perfect. Exactly like how animation layers work, but for timing.
Bonus points if a single retime curve can affect keys on several animation layers, as well.
Is this idea possible? Can you point me in some good direction for getting started, or any tools that have already been written? I'm very new to learning programming, and am just starting to learn python for maya.
This should be possible. Have a look how a scene timewarp works. Every animation curve is connected to a time node. Usually you only have one time node which is connected to all anim curves. A scene timewarp is connected to this time node. What you can try is to create a time node for every bunch of animcurves you want control seperately. And then create a animCurveTT and connect it to the corresponding time node. If this precdure works, you can script it to make it easier to control.

How to prevent time delay/racing in python?

I'm trying to implement a script which will play the game snake using the a* algorithm. The game was given to me by someone else (my teacher) and I have to write an working script that plays the game as good as possible, using this exact algorithm or some variation of it.
Now, I have written the algorithm and tried various simulations of it and it turns out that it works just fine, however since the game is an .exe I have to take a screenshot of the game screen and transform that into input data for my algorithm from that I construct a path and give commands to the snake using pyautogui.
So to summarize, the algorithm:
Take screenshot
Calculate path and moves
Move towards goal
Repeat until snake dies or certain score is reached.
The problem which I face is that the snake has a certain time which it takes to make 1 move and I delay the key press accordingly to that time, however when I reach the goal I have to repeat all the steps and steps 1-2 always take about 45-55ms to complete and in that time my snake will end up dying because it didn't react quick enough. So what would be a good way to prevent this?
The solution I came up with, works sometimes, but a time delay will form and the snake will either make one move too early or too late at some point which might be deadly:
def simulate_movement(moves, speed):
for move in moves:
text, pause = move
interval1 = round(pause * speed, 1)
if move == moves[-1]:
interval1 = 0.05 # I speed up the final move because steps 1 and 2 take about 0.05ms
pyautogui.press(text, interval=interval1)
Example of what the delay might cause:
The snake in the image has died, however the path which was correct was provided for it, but because it didn't react in time (because of mentioned delay) it bumped into the corner. Is there a way to for example make some code block which takes between 50-60ms to make it run 100ms so that is do nothing until that time passes, because that way I could get rid of the delay by subtracting 100ms from the final move and waiting 100ms until next moves are calculated.

How to automated curved mouse movements in Python (with pyautogui or any other similarmodule)

I'm trying to automate mouse movements in a way that would look somewhat human-like. The reason why I need to do this is because I'm currently working on promotional videos for a web-app and I want to show how it's used. I don't want to just record my own movements because what I mean by "somewhat" human-like is that I want it to feel kind of organic, but I still want it to be perfect.
I've been using pyautogui and time for now and it works great. I can get the cursor to move at whatever speed I want. I can make it slow down with tweening functions. It looks almost human, but it's all in straight lines. I'd like to have the cursor move in curves.
Is that possible? Either that means using pyautogui with another module or simply other modules completely, that'd be fine. Also, I've found only one question here about this subject and I'm not sure the poster had the same objective as I do. They were talking about a bezier curve. I'm not particularly familiar with those so ideally, if it's possible of course, I'd like a simpler solution.

Exposing a Large Swath of Tiles in Minesweeper

I was thinking about creating minesweeper in python/pygame. However, I was stumped when I was trying to figure out a way to guarantee a large swath of empty space on the first move (such as in minesweeper on Windows XP). Does anyone have a method for doing this? I don't want code, just words.
Thank you in advance
Firstly, that doesn't happen on Windows XP (or any common minesweeper implementation) every time. It's very likely to if you are playing on a low difficulty though.
There are some ideas though;
Generate the map after the first click. This allows you to avoid the area the user clicked, giving you the large swath you desire - simply by tweaking your mine placement algorithm to avoid the area around where the user clicked.
Generate the map - but change it if insufficient space would be exposed. This will (probably) result in a faster reaction on the first click as the map will likely be already generated.
Don't do this. As mentioned previously - this is not how windows XP worked. But there was a high likelihood of this just happening naturally on easier difficulties. It might be worth recalculating the map if the user clicks on a mine on the first move, but otherwise leave it to your random distribution. Remember that (except some custom modes) there are going to be many more empty squares than ones with mines.
Hopefully that will get you started.

Categories