[Solved] How to set mouse position fastest ? lazarus / 34-Windows ---------------------------------------------------------------------- loaded · 2022-01-21 17:48 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  crono:cardinal;  x,y:integer;begin   crono:=GetTickCount;   for x:=0 to screen.Width{mee to 1920} do for y:=0 to screen.Height {1080} do  SetCursorPos(x,y); //uses add windows   TButton(sender).Caption:=floattostr((GetTickCount-crono)/1000);  //  approximate time 11.28 secondsend; ---------------------------------------------------------------------- winni · 2022-01-21 19:37 Hi! I don't know if it is faster, but give it a try:  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Mouse.CursorPos := Point(x,y); Winni ---------------------------------------------------------------------- loaded · 2022-01-21 19:45 Thank you very much for your answer winni It works, but the speed is the same. ---------------------------------------------------------------------- balazsszekely · 2022-01-21 20:41 @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 · 2022-01-22 05:00 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. Quote from: GetMem 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. 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.. ---------------------------------------------------------------------- balazsszekely · 2022-01-22 09:01 @loaded Quotemy 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses BGRABitmap, BGRABitmapTypes; procedure InspectColors(AWidth, AHeight: Integer);var  Bmp: TBGRABitmap;  X, Y: integer;  p: PBGRAPixel;begin  Bmp := TBGRABitmap.Create(AWidth, AHeight);  try    Bmp.TakeScreenshot(TRect.Create(0, 0, AWidth, AHeight));    for Y := 0 to AHeight - 1 do    begin      P := Bmp.Scanline[Y];      for X := 0 to AWidth - 1 do      begin        //do something with  P^.Red, P^.Green, P^.Blue, P^.Alpha        Inc(P);      end;    end;  finally    Bmp.Free;  end;end; procedure TForm1.Button1Click(Sender: TObject);begin  InspectColors(Screen.Width, Screen.Height); //or BrowserWindow width and heigthend; 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 · 2022-01-22 10:13 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. ---------------------------------------------------------------------- winni · 2022-01-22 12:09 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Bmp := TBGRABitmap.Create;  try    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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i P := bmp.Data; for i := 0 to bmp.NbPixels -1 do    begin    ....    inc(p);    end;    // if you made some writing operations you have to invalidate:    bmp.InvalidateBitmap; Winni ---------------------------------------------------------------------- loaded · 2022-01-22 13:40 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. ---------------------------------------------------------------------- balazsszekely · 2022-01-22 21:11 @loaded What is your Lazarus version? I have a surprise for you. ---------------------------------------------------------------------- loaded · 2022-01-23 06:20 The version I am using is 2.0.10 32 Bit All that comes from the master is good. ---------------------------------------------------------------------- balazsszekely · 2022-01-23 08:17 @loaded QuoteThe version I am using is 2.0.10 32 BitTime 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. ---------------------------------------------------------------------- loaded · 2022-01-23 09:56 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.