Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How to get mouse position in process

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Sun Jul 04, 2021 6:41 pm    Post subject: How to get mouse position in process Reply with quote

how to get mouse pos in any application? like getMousePos(x, y) but only in application
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Sun Jul 04, 2021 10:49 pm    Post subject: Reply with quote

You can get the mouse position via:
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos

You can then translate it to a specific windows coords (if its within the windows bounds) with:
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-screentoclient

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Mon Jul 05, 2021 1:35 pm    Post subject: Reply with quote

You should probably get the Window description.
I have prepared an example for the CE Lua Engine window below.
It works with the "F8" key.
To get the window size and position or to print the mouse position.
If the size or location of the window changes, you should refresh your list with "F8" again.

Or take examples from the code that work for you and reshape it according to your wishes.

Code:
function windowCaption()
w=getWindow(getForegroundWindow(), GW_HWNDFIRST)

pid=getOpenedProcessID()

while w and (w~=0) do
  if getWindowProcessID(w)==pid then
    print(w..' - '..getWindowCaption(w)..'('..getWindowClassName(w)..')')

  end
  w=getWindow(w,GW_HWNDNEXT)
end
end

--To get the window title of the opened process, activate the following line.

--windowCaption()
----------------------------------------

if f then f.Destroy() f=nil end

f = createForm()
f.width, f.height = 280,100

lblX = createLabel(f)
lblX.left, lblX.top = 20, 5
lblX.font.size = 14

lblY = createLabel(f)
lblY.left, lblY.top = 20, 25
lblY.font.size = 14

lblX1 = createLabel(f)
lblX1.left, lblX1.top = 20, 45
lblX1.font.size = 14

lblY1 = createLabel(f)
lblY1.left, lblY1.top = 20, 65
lblY1.font.size = 14
----------------------------------------
--Lua Engine visible true
print("Hello")

local Panel = { }

function panelCoord1()
Panel.x1=findWindow(nil,"Lua Engine") --My window handle header is CE "Lua Engine"

m=createMemoryStream()
m.size=16

if executeCodeLocalEx('GetWindowRect',Panel.x1,m.Memory) then
  m.Position=0
  Panel.lft=(m.readDword())
  Panel.tp=(m.readDword())
  Panel.rgt=(m.readDword())
  Panel.btm=(m.readDword())

--If the window position is negative, let's fix it.
  if Panel.lft>4294900000 then Panel.lft=Panel.lft - 4294967296  end
  if Panel.tp>4294900000 then Panel.tp=Panel.tp - 4294967296  end
--Let's compare the values obtained and reach the result of width and height.
  Panel.wdt=tonumber(Panel.rgt) - tonumber(Panel.lft)
  Panel.hgt=tonumber(Panel.btm) - tonumber(Panel.tp)
--There may be some deviations. But the result is approximate.
  --print("left= "..Panel.lft.."\ntop= "..Panel.tp.."\nright= "..Panel.rgt.."\nbottom= "..Panel.btm.."\nheight= "..Panel.hgt.."\nwidth= "..Panel.wdt.."\n")
lblX1.caption = "P.height: "..Panel.hgt.." - P.width: "..Panel.wdt
lblY1.caption = "P.Left: "..Panel.lft.." - P.Top: "..Panel.tp
end
m.destroy()
end

if zT1 then zT1.Destroy() zT1=nil end
 zT1=createTimer()
 zT1.Interval=50
 zT1.Enabled=false

local screenFindx1=Panel.wdt
local  screenFindy1=Panel.hgt

function pnlOnEnter()
 local x,y = getMousePos()
 x2=tonumber(x) - tonumber(Panel.lft)
 y2=tonumber(y) - tonumber(Panel.tp)

 if x2<=0 then x2=0
 elseif y2<=0 then y2=0
 elseif x2>=Panel.wdt then x2=Panel.wdt
 elseif y2>=Panel.hgt then y2=Panel.hgt
 end
 lblX.caption = 'X.pos : '..tostring(x2)
 lblY.caption = 'Y.pos : '..tostring(y2)
end
 zT1.OnTimer=pnlOnEnter

if posKey1 then posKey1.Destroy() posKey1=nil end
posKey1=createHotkey(function() panelCoord1() sleep(200) if zT1.Enabled==false then zT1.Enabled=true
else
zT1.Enabled=false end end, VK_F8)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites