Displaying Mouse Coordinates

Share your advanced PureBasic knowledge/code with the community.
Slyvnr
User
User
Posts: 58
Joined: Wed Jun 27, 2007 10:10 pm
Location: USA
Contact:

Displaying Mouse Coordinates

Post by Slyvnr »

I have had need for a quick program to display the coordinates of the mouse cursor for writing macro programs.

I came up with this little program. Unfortunately it uses Windows API calls. And I do not have Mac or Linux systems to test for the other two op systems anyway.

Code: Select all

;-----------------------------------------------------
;
;    MouseLoc.pb
;    June 10 2009
;    Version 1.1
;
;    V 1.1 06/10/2009
;    Added lines to make window sticky to stay on top
;
;    V 1.0 06/09/2009
;    simply tell us where the mouse cursor is on the
;    desktop
;-----------------------------------------------------
;Declare structures
Structure tagPOINT
  x.l
  y.l
EndStructure 
Global POINT.tagPOINT

;--------------------
;Main program
;--------------------
; Get the resolution of the desktop
ExamineDesktops()
DT_Width=DesktopWidth(0)
DT_Height=DesktopHeight(0)

; Figure out where to place window to display mouse coordinates
Win_Width=90
Win_Height=20
Pos_x=DT_Width-Win_Width-22
Pos_y=DT_Height-Win_Height-52

; create window in which we will display the mouse coordinates
If OpenWindow(0, Pos_x, Pos_y, Win_Width, Win_Height, "Mouse Loc", #PB_Window_SystemMenu )
  txt_titlex=TextGadget(#PB_Any,5,5,10,20,"X:")
  txt_MouseX=TextGadget(#PB_Any,20,5,40,20,"0.00")
  txt_titley=TextGadget(#PB_Any,60,5,10,20,"Y:")
  txt_MouseY=TextGadget(#PB_Any,75,5,40,20,"0.00")
EndIf

;Keep window on top of all other windows
StickyWindow(0,1)

; setup a variable to check for program close
CLOSEME=0

Repeat
     ;keep window on top 
      
    ; check to see if window X has been pressed
    e=WindowEvent()
    If e=#PB_Event_CloseWindow
     CLOSEME=1  ; if window X has been pressed set variable to end loop
    EndIf
    
    ; Get Cursor Position
    GetCursorPos_(@POINT)
    MouseX$=Str(POINT\x)
    MouseY$=Str(POINT\y)
    
    ;Update Cursor Position in Output window
    SetGadgetText(txt_MouseX,MouseX$)
    SetGadgetText(txt_MouseY,MouseY$)
    
;   ;smoother update of mouse coordinates and also release CPU to other processes
    ;basically helps prevents system resource hogging
    Sleep_(70)  
            
Until CLOSEME  ; if CLOSEME then lets exit and end the program

End

So far it has worked very well.

Has made life easier for making macros that is for sure.

Here is a nifty little icon file for it if you want to make an .exe file and have to use from the desktop.
http://www.yasdc.com/downloads/Where.ico

And for those who want it pre-compiled
http://www.yasdc.com/downloads/MouseLoc.exe

=====================
By the way there is a typo in the program. But if I correct the typo, the program crashes (will not compile/run). If I leave the typo in it works fine. See if you can spot the typo and then see if you can explain the behavior of the program.

(FIXED NO LONGER IN ABOVE CODE)
=====================

Slyvnr
Last edited by Slyvnr on Thu Jun 11, 2009 1:44 pm, edited 1 time in total.
BillNee
User
User
Posts: 93
Joined: Thu Jan 29, 2004 6:01 am
Location: Homosassa, FL

Post by BillNee »

Hi - I believe stickywindow needs the window number - 0,1, etc. Since you're using window 0, and "muwindow" is not set to a value it is 0 so stickywindow works. Think mywindow is a handle to the window, not the window number so it wouldn't work.
Bill
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

First of all you typed Mu instead of My for the sticky windows,
this is why using EnableExplicit is a "good thing" :P

Secondly, this is from the PureBasic manual :twisted:

Code: Select all

  If OpenWindow(0, 0, 0, 300, 30, "Desktop mouse monitor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0, 10, 6, 200, 20, "")
    
    Repeat
      Event = WindowEvent()
      
      If Event = 0 ; No events are in queue anymore, so halt the process for a few milliseconds for multitasking
        SetGadgetText(0, "Desktop mouse position: "+Str(DesktopMouseX())+","+Str(DesktopMouseY()))
        Delay(20)
      EndIf
       
    Until Event = #PB_Event_CloseWindow
  EndIf
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Pwned! :lol:
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Slyvnr
User
User
Posts: 58
Joined: Wed Jun 27, 2007 10:10 pm
Location: USA
Contact:

LMAO

Post by Slyvnr »

:lol:

Well you can tell I do not read manuals. LMAO



Slyvnr
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Hey! Typing mistakes we all do.
Forgetting the manual just because you "read it once" is a bad thing,
so you better remember the F1 key or Fred will come and spank you :P

The only thing I remembered was that it did have a desktop mouse thing,
but I could have sworn it was tied to the ExamineDesktop etc. Either it was different in the past or I remembered wrong.

The only downside here (in any case) is the delay, if you are too fast on moving the mouse it might miss coordinates.
You could go as low as 1ms on Delay().
But you'd have to use TimeBeginPeriod_(1) at the start of the program and TimeEndPeriod_(1) at the end of the program to be able to reach 1ms timing on Windows 2000 (and NT?) and later. Win9x, Linux and Mac already have 1ms timing.

Then just make sure you update the text only each, 200ms should be plenty? Going much below 100ms would use more CPU on the text update than necessary.

What speed does your macros run at anyway? 100ms ?
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Rescator wrote:The only downside here (in any case) is the delay, if you are too fast on moving the mouse it might miss coordinates.
It's not possible to catch all coordinates due to mouse acceleration.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Fluid Byte wrote:
Rescator wrote:The only downside here (in any case) is the delay, if you are too fast on moving the mouse it might miss coordinates.
It's not possible to catch all coordinates due to mouse acceleration.
That's true, what I meant was precision. If the macros are played back at 100ms speed, then sampling at least with 50ms intervals is advised. (kinda like the Nyquist theory but inverted)
Slyvnr
User
User
Posts: 58
Joined: Wed Jun 27, 2007 10:10 pm
Location: USA
Contact:

Mouse Precision

Post by Slyvnr »

Actually, the program was written so I could determine the location of buttons in other programs and click on them. I just position the mouse where I need it wait a moment, copy the coordinates down then plug the numbers into my macro program.

For example. I have written a program that automatically gathers data from one system (old dos based console program) and scrapes the screen (CTRL-C copy) and then pull up a new windows based program, position the mouse in the correct field, click to enter input mode, paste the scraped data into a new windows based program repeat for next field until all fields are copied. At the end of the copy process the macro program then moves the mouse to the "Save" button, clicks the "Save" button, then press enter to accept the "OK" on the Are you Sure? pop-up. In this manner I can "manually" transfer data from a really really old program to a newer program by a different company.

The old program uses a proprietary database engine that encrypts all data so was quicker and easier to do this then to do file manipulation.

Run back speed is real-time with out any delaying other than at specific moments where the program is doing disk writes. I run the program overnight. Comeback the next morning and the data is converted for my client. I then make a backup of the database to CD, overnight the data to them, they do a restore and are ready to start using the system.

Is actually a sweet little setup.

Slyvnr
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Haha, not exactly what Fred had in mind when he created PureBasic but stuff like this probably put a smile on his face.

Is there a "Unusual uses for PureBasic" thread on these forums anywhere?
If not why not make one in General or something.
I at least would love to hear about ingenious solutions like this using PureBasic, and I'm sure the rest here would love it too :)

Might make for some killer quotes for the website "PR" blurb too :P
Post Reply