Help with measuring pixels with PixelSpacing

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Help with measuring pixels with PixelSpacing

Post by iilabs » 23 Jun 2023, 00:16

Hi AHK world,

I have a script I'm having trouble combining the logic. What I have is a basically a ruler that measures pixels and converts it to mm using a scale factor. This works great until you zoom. Then I have to convert scale factor with a zoom factor. My problem is I measure using LBUTTON, drag and RELEASE LBUTTON to get a measurement. I also zoom with LBUTTON and RBUTTON and moving the mouse up or down and release. Since I cannot have two Hotkeys of same syntax I need to combine?

Code: Select all


Global ScaleFactor := .4379
Global ZoomFactor := 1

~LButton:: ;hold down Lbutton mouse button to start measuring
	mouseGetPos,sx,sy ;start position to measure from
	setTimer updatePos,50
return

~LButton Up:: ;release to capture measurement
	setTimer updatePos,off	
	tooltip distance measured is : %distCalibrated% units
	clipboard := distCalibrated
return

updatePos:
	mouseGetPos,x,y
	dx := x-sx
	dy := sy-y
	dist := round(  ((dx)**2 + (dy)**2) **.5  ,2)
	distCalibrated := round(dist * scaleFactor,3)  
	tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% units
return

;////////////////FOR Z SCALE FACTOR IN MEASURING
~LButton::
mouseGetPos,,zy
setTimer updatePos,50
While GetKeyState("RButton", "D") {
	Gosub, updatePosZ
    SetTimer, RemoveToolTip, -1000
}
Return

updatePosZ:
	mouseGetPos,,zz
	distz := zy-zz
	zoomfactor := round(distz,3) / 100
	tooltip, % distz
     
return
I also tried...

Code: Select all

~LButton:: ;hold down middle mouse button to start measuring
	mouseGetPos,sx,sy ;start position to measure from
    While GetKeyState("RButton", "D") {
        mouseGetPos,,zy
        setTimer updatePosZ,50
    }
	setTimer updatePos,50
return

~LButton Up:: ;release to capture measurement
	setTimer updatePos,off	
	tooltip distance measured is : %distCalibrated% units
	clipboard := distCalibrated
return

updatePos:
	mouseGetPos,x,y
	dx := x-sx
	dy := sy-y
	dist := round(  ((dx)**2 + (dy)**2) **.5  ,2)
	distCalibrated := round(dist * scaleFactor,3)  
	tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% units
return

updatePosZ:
	mouseGetPos,,zz
	distz := zy-zz
return

~RButton Up:: ;release to capture measurement
	setTimer updatePosZ,off	
    zoomfactor := round(distz,3) / 100
    tooltip, % zoomfactor
    SetTimer, RemoveToolTip, -1000
return

Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Help with measuring pixels with PixelSpacing

Post by Rohwedder » 23 Jun 2023, 01:14

Hallo,
Try:

Code: Select all

ScaleFactor := .4379

~LButton:: ;hold down Lbutton mouse button to start measuring
SoundBeep, 4000, 20
	mouseGetPos,sx,sy ;start position to measure from
	setTimer updatePos,50
	updatePos := True
return

~LButton Up:: ;release to capture measurement
	setTimer updatePos,off
	updatePos := False
	tooltip distance measured is : %distCalibrated% units
	clipboard := distCalibrated
return

updatePos:
	mouseGetPos,x,y
	dx := x-sx
	dy := sy-y
	dist := round(  ((dx)**2 + (dy)**2) **.5  ,2)
	distCalibrated := round(dist * scaleFactor,3)
	tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% units`nzoomfactor: %zoomfactor%
return

;////////////////FOR Z SCALE FACTOR IN MEASURING
#IF updatePos
RButton::
#IF
SoundBeep, 1000, 20
mouseGetPos,,zy
setTimer updatePosZ, 50
KeyWait, RButton
setTimer updatePosZ, Off
Return

updatePosZ:
	mouseGetPos,,zz
	distz := zy-zz
	zoomfactor := round(distz,3) / 100
return
Currently the zoomfactor is only displayed, not taken into account in the measurement.

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: Help with measuring pixels with PixelSpacing

Post by iilabs » 23 Jun 2023, 09:29

The logic seems much better. I may not have explained the button presses accurately. For me to zoom I have to have both Rbutton and Lbutton pressed and dragged and then get the zoomfactor. I believe now zoomfactor changes only if I press left and then right button. I tried below but not sure what to do with the keywait, RButton? Maybe more release of RButton?

Code: Select all


#IF updatePos
~LButton & Rbutton::
#IF
SoundBeep, 1000, 20
mouseGetPos,,zy
setTimer updatePosZ, 50
KeyWait, RButton
setTimer updatePosZ, Off
Return


Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Help with measuring pixels with PixelSpacing

Post by Rohwedder » 23 Jun 2023, 10:43

So you could measure (LButton Down) and/or change zoom factor (RButton Down) at will simultaneously or separately:

Code: Select all

ScaleFactor := .4379

~LButton:: ;hold down Lbutton mouse button to start measuring
SoundBeep, 4000, 20
	mouseGetPos,sx,sy ;start position to measure from
	setTimer updatePos, 50
	updatePos := True
return

~LButton Up:: ;release to capture measurement
	setTimer updatePos, off
	updatePos := False
	tooltip distance measured is : %distCalibrated% units
	clipboard := distCalibrated
return

updatePos:
	mouseGetPos,x,y
	dx := x-sx
	dy := sy-y
	dist := round(  ((dx)**2 + (dy)**2) **.5  ,2)
	distCalibrated := round(dist * scaleFactor,3)
	tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% units`nzoomfactor: %zoomfactor%
return

;////////////////FOR Z SCALE FACTOR IN MEASURING
RButton::
mouseGetPos,,zy
setTimer updatePosZ, 50
Return
RButton Up::
setTimer updatePosZ, Off
ToolTip
Return

updatePosZ:
	mouseGetPos,,zz
	distz := zy-zz, zoomfactor := round(distz,3) / 100
	IF !updatePos
		ToolTip,% "zoomfactor: " zoomfactor
return

Post Reply

Return to “Ask for Help (v1)”