Recent

Author Topic: [Solved] How to set mouse position fastest ?  (Read 5742 times)

loaded

  • Hero Member
  • *****
  • Posts: 824
[Solved] How to set mouse position fastest ?
« on: January 21, 2022, 06:48:36 pm »
Hi All,
In a project I'm thinking of, I want to move the mouse so that it is on every pixel of the screen. I can do this with the code below. But it takes too long. Is there an alternative method that I can do faster?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   crono:cardinal;
  4.   x,y:integer;
  5. begin
  6.    crono:=GetTickCount;
  7.    for x:=0 to screen.Width{mee to 1920} do for y:=0 to screen.Height {1080} do  SetCursorPos(x,y); //uses add windows
  8.    TButton(sender).Caption:=floattostr((GetTickCount-crono)/1000);  //  approximate time 11.28 seconds
  9. end;
   
« Last Edit: January 23, 2022, 10:57:55 am by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to set mouse position fastest ?
« Reply #1 on: January 21, 2022, 08:37:41 pm »
Hi!


I don't know if it is faster, but give it a try:

 
Code: Pascal  [Select][+][-]
  1. Mouse.CursorPos := Point(x,y);

Winni 

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to set mouse position fastest ?
« Reply #2 on: January 21, 2022, 08:45:03 pm »
Thank you very much for your answer winni
It works, but the speed is the same.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

balazsszekely

  • Guest
Re: How to set mouse position fastest ?
« Reply #3 on: January 21, 2022, 09:41:51 pm »
@loaded
1920x1080 = 2,073,600 api call to SetCursorPos, no wonder it takes 11 seconds. What exactly are you after? Maybe there is a better way to achieve it, without moving the mouse.

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to set mouse position fastest ?
« Reply #4 on: January 22, 2022, 06:00:45 am »
My Master GetMem
my purpose; It was to turn off youtube ads automatically.
I actually use javascript for this, they're pretty good at that too, but I've come to the conclusion that the surest way to turn off ads leaking through filters is to inspect the colors and handles on the screen. I decided that this was the simplest method for this process.
@loaded
1920x1080 = 2,073,600 api call to SetCursorPos, no wonder it takes 11 seconds. What exactly are you after? Maybe there is a better way to achieve it, without moving the mouse.
As always you are right.
Now, after some more testing, I saw that the GetPixel command is very fast, like 0.3 seconds, but the WindowFromPoint command is very slow. This means longer processing time.
Apparently, I have to find the way to solve the Problem on the javascript side..  :-\
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

balazsszekely

  • Guest
Re: [Closed] How to set mouse position fastest ?
« Reply #5 on: January 22, 2022, 10:01:36 am »
@loaded

Quote
my purpose; It was to turn off youtube ads automatically.
I actually use javascript for this, they're pretty good at that too, but I've come to the conclusion that the surest way to turn off ads leaking through filters is to inspect the colors and handles on the screen. I decided that this was the simplest method for this process.

Now, after some more testing, I saw that the GetPixel command is very fast, like 0.3 seconds, but the WindowFromPoint command is very slow. This means longer processing time.
Interesting.  :)

1. With bgrabitmap do the following:
Code: Pascal  [Select][+][-]
  1. uses BGRABitmap, BGRABitmapTypes;
  2.  
  3. procedure InspectColors(AWidth, AHeight: Integer);
  4. var
  5.   Bmp: TBGRABitmap;
  6.   X, Y: integer;
  7.   p: PBGRAPixel;
  8. begin
  9.   Bmp := TBGRABitmap.Create(AWidth, AHeight);
  10.   try
  11.     Bmp.TakeScreenshot(TRect.Create(0, 0, AWidth, AHeight));
  12.     for Y := 0 to AHeight - 1 do
  13.     begin
  14.       P := Bmp.Scanline[Y];
  15.       for X := 0 to AWidth - 1 do
  16.       begin
  17.         //do something with  P^.Red, P^.Green, P^.Blue, P^.Alpha
  18.         Inc(P);
  19.       end;
  20.     end;
  21.   finally
  22.     Bmp.Free;
  23.   end;
  24. end;
  25.  
  26. procedure TForm1.Button1Click(Sender: TObject);
  27. begin
  28.   InspectColors(Screen.Width, Screen.Height); //or BrowserWindow width and heigth
  29. end;
  30.  
This will be fast.

2. Split the browser window to 9 regions, spawn 9 working thread and pass each thread a region. Do not synchronize until the regions are scanned.
Please note: creating threads takes time, so create the threads on application start and reuse them whenever is necessary. In between jobs the threads should go idle.
I believe this way you can speed up the scan of the window handles.

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: [Closed] How to set mouse position fastest ?
« Reply #6 on: January 22, 2022, 11:13:29 am »
My esteemed Master,
Thank you very much for taking your precious time to reply.
The code you sent is very fast in pixel scanning; It gave me new ideas.
I think if I can improve this code, I will achieve my goal.
I guess my next stop will be the Graphics Section.
Respects.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: [Closed] How to set mouse position fastest ?
« Reply #7 on: January 22, 2022, 01:09:49 pm »
Hi!

Two remarks to simplify the code from getmem:

If you want to create a TBGRAbitmap and then take a screenbshot, you don't have to specify the size at creation. The TakeScreenshot will do that for you:

Code: Pascal  [Select][+][-]
  1. Bmp := TBGRABitmap.Create;
  2.   try
  3.     Bmp.TakeScreenshot(TRect.Create(0, 0, AWidth, AHeight));

If you want to treat the whole bitmap there is no need to  use the ScanLine. TBGRAbitmap has the property data for the raw data. Use this:

Code: Pascal  [Select][+][-]
  1. P := bmp.Data;
  2.  for i := 0 to bmp.NbPixels -1 do
  3.     begin
  4.     ....
  5.     inc(p);
  6.     end;
  7.     // if you made some writing operations you have to invalidate:
  8.     bmp.InvalidateBitmap;
  9.  


Winni

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: [Closed] How to set mouse position fastest ?
« Reply #8 on: January 22, 2022, 02:40:30 pm »
Thank you very much Winni for your interest and concern.
Now, this message has become a landmark for me.
Thank you for your hard work.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

balazsszekely

  • Guest
Re: [Closed] How to set mouse position fastest ?
« Reply #9 on: January 22, 2022, 10:11:32 pm »
@loaded
What is your Lazarus version? I have a surprise for you.

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: [Closed] How to set mouse position fastest ?
« Reply #10 on: January 23, 2022, 07:20:54 am »
The version I am using is 2.0.10 32 Bit
All that comes from the master is good.  :)
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

balazsszekely

  • Guest
Re: [Closed] How to set mouse position fastest ?
« Reply #11 on: January 23, 2022, 09:17:36 am »
@loaded

Quote
The version I am using is 2.0.10 32 Bit
Time to upgrade.

The attached project will scan a region(TRect) of the desktop, then display the result in a VST. By default the region is the whole screen, but you can adjust that by passing a different parameter to the scan function. Few notes:
   1. The scan is threaded, the screen is split in sub regions to increase speed
   2. The list is protected with critical section to prevent data corruption
   3. The result is displayed in virtual tree, again for speed gain
   4. The application uses a pre-cached window list, to prevent slow API calls
   5. The result is displayed as follows:
        - first line(0...Screen.Width)
        - second line(0...Screen.Width)
        - etc...

At my side the entire screen is scanned/displayed in 1.1 seconds. Please note that populating the VST also take times, so the actual scan is even faster. Hopefully is enough for your purposes.  I did not test the application thoroughly, it may contain bugs.
« Last Edit: January 23, 2022, 09:21:12 am by GetMem »

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: [Closed] How to set mouse position fastest ?
« Reply #12 on: January 23, 2022, 10:56:39 am »
My esteemed Master,
Again, you showed your talents;
I say with all sincerity, every work of yours is a complete lesson.
It has been a work beyond what I expected, well done.
May your presence be everlasting.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018