Jump to content

HELP Cursor jumps on showCursor


Recommended Posts

Hi,

When I use showCursor(true) to open a panel, the cursor appears in the center of the screen for a short moment, then instantly jumps to where it was before I closed the panel.

I tried saving the last position using getCursorPosition() and restoring it with setCursorPosition(), but it doesn't help.

Any idea how to prevent the cursor jumping from the center?

Link to comment
cursorX, cursorY = getCursorPosition()
-- showCursor(false) <-- Use Before Then

 ...

-- showCursor(true) <-- Use After Then
screenX, screenY = guiGetScreenSize()
setCursorPosition(cursorX*screenX, cursorY*screenY)

 

  • Like 1
Link to comment
2 hours ago, CastiaL said:
cursorX, cursorY = getCursorPosition()
-- showCursor(false) <-- Use Before Then

 ...

-- showCursor(true) <-- Use After Then
screenX, screenY = guiGetScreenSize()
setCursorPosition(cursorX*screenX, cursorY*screenY)

 

As I mentioned, I already tried getCursorPosition and setCursorPosition, but it doesn't work — the cursor still starts from the center of the screen..
 

local function togglePanel()
	toggle = not toggle
   
	if toggle then 		
		if savedCursorX and savedCursorY then
			local screenX, screenY = guiGetScreenSize()
			setCursorPosition(savedCursorX * screenX, savedCursorY * screenY)
		end
		
		showCursor(true)
	else 
		savedCursorX, savedCursorY = getCursorPosition()
		showCursor(false)
	end
end
bindKey("k", "down", togglePanel)

Doesnt work this way

Link to comment
local toggle = false
local savedCursorX, savedCursorY

local function restoreCursorPos()
	if savedCursorX and savedCursorY then
		local screenX, screenY = guiGetScreenSize()
		setCursorPosition(savedCursorX * screenX, savedCursorY * screenY)
	end
end

local function togglePanel()
	toggle = not toggle

	if toggle then
		showCursor(true)

		for i = 1, 5 do
			setTimer(restoreCursorPos, i * 50, 1)
		end
	else
		savedCursorX, savedCursorY = getCursorPosition()
		showCursor(false)
	end
end

bindKey("k", "down", togglePanel)

Can you just try this? ı added a timer for it.

  • Like 1
Link to comment
14 minutes ago, efex said:
local toggle = false
local savedCursorX, savedCursorY

local function restoreCursorPos()
	if savedCursorX and savedCursorY then
		local screenX, screenY = guiGetScreenSize()
		setCursorPosition(savedCursorX * screenX, savedCursorY * screenY)
	end
end

local function togglePanel()
	toggle = not toggle

	if toggle then
		showCursor(true)

		for i = 1, 5 do
			setTimer(restoreCursorPos, i * 50, 1)
		end
	else
		savedCursorX, savedCursorY = getCursorPosition()
		showCursor(false)
	end
end

bindKey("k", "down", togglePanel)

Can you just try this? ı added a timer for it.

Thank you, but same :~.. xd doesnt help

Link to comment

There are two kinds of methods, one with setTimer and the other with the alternative onClientRender


setTimer :

The cursor always appears in the center for one frame after showCursor(true).

function openPanel()
    showCursor(true)
    -- Save the last cursor position (values between 0 and 1)
    local x, y = getCursorPosition()
    -- Get the screen resolution
    local sx, sy = guiGetScreenSize()
    -- After 50ms, set the cursor back to its previous position
    setTimer(function()
        setCursorPosition(x * sx, y * sy)
    end, 50, 1)
end 

onClientRender:

Use a timer or onClientRender to set the position after showing the cursor.

function panelAc()
    showCursor(true)
    local x, y = getCursorPosition()
    local sx, sy = guiGetScreenSize()
    local function imlecDuzelt()
        setCursorPosition(x * sx, y * sy)
        removeEventHandler("onClientRender", root, imlecDuzelt)
    end
    addEventHandler("onClientRender", root, imlecDuzelt)
end

 

your fixed code :

 

local toggle = false
local savedCursorX, savedCursorY

local function togglePanel()
    toggle = not toggle

    if toggle then
        showCursor(true)
        if savedCursorX and savedCursorY then
            local screenX, screenY = guiGetScreenSize()
            -- Wait one frame before setting the cursor position
            setTimer(function()
                setCursorPosition(savedCursorX * screenX, savedCursorY * screenY)
            end, 50, 1)
        end
    else
        savedCursorX, savedCursorY = getCursorPosition()
        showCursor(false)
    end
end
bindKey("k", "down", togglePanel)

 

I think you understand, if you have a different problem please post it here or create a new topic and tag me

  • Like 1
Link to comment
2 hours ago, Shady1 said:

There are two kinds of methods, one with setTimer and the other with the alternative onClientRender


setTimer :

The cursor always appears in the center for one frame after showCursor(true).

function openPanel()
    showCursor(true)
    -- Save the last cursor position (values between 0 and 1)
    local x, y = getCursorPosition()
    -- Get the screen resolution
    local sx, sy = guiGetScreenSize()
    -- After 50ms, set the cursor back to its previous position
    setTimer(function()
        setCursorPosition(x * sx, y * sy)
    end, 50, 1)
end 

onClientRender:

Use a timer or onClientRender to set the position after showing the cursor.

function panelAc()
    showCursor(true)
    local x, y = getCursorPosition()
    local sx, sy = guiGetScreenSize()
    local function imlecDuzelt()
        setCursorPosition(x * sx, y * sy)
        removeEventHandler("onClientRender", root, imlecDuzelt)
    end
    addEventHandler("onClientRender", root, imlecDuzelt)
end

 

your fixed code :

 

local toggle = false
local savedCursorX, savedCursorY

local function togglePanel()
    toggle = not toggle

    if toggle then
        showCursor(true)
        if savedCursorX and savedCursorY then
            local screenX, screenY = guiGetScreenSize()
            -- Wait one frame before setting the cursor position
            setTimer(function()
                setCursorPosition(savedCursorX * screenX, savedCursorY * screenY)
            end, 50, 1)
        end
    else
        savedCursorX, savedCursorY = getCursorPosition()
        showCursor(false)
    end
end
bindKey("k", "down", togglePanel)

 

I think you understand, if you have a different problem please post it here or create a new topic and tag me

Needed an extra showCursore(false) before the timer
 

showCursor(true)
		
if savedCursorX and savedCursorY then
	showCursor(false) -- here

	local screenX, screenY = guiGetScreenSize()
	setTimer(function()
		setCursorPosition(savedCursorX * screenX, savedCursorY * screenY)
		showCursor(true)
	end, 50, 1)
end

So now it looks good :D thank you guys

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...