Page 1 of 1

Hide / unhide cursor mouse

Posted: 30 Jan 2015, 06:11
by empardopo
I've just found this

Code: Select all

SystemCursor(OnOff=1)   ; INIT = "I","Init"; OFF = 0,"Off"; TOGGLE = -1,"T","Toggle"; ON = others
{
    static AndMask, XorMask, $, h_cursor
        ,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13 ; system cursors
        , b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13   ; blank cursors
        , h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13   ; handles of default cursors
    if (OnOff = "Init" or OnOff = "I" or $ = "")       ; init when requested or at first call
    {
        $ = h                                          ; active default cursors
        VarSetCapacity( h_cursor,4444, 1 )
        VarSetCapacity( AndMask, 32*4, 0xFF )
        VarSetCapacity( XorMask, 32*4, 0 )
        system_cursors = 32512,32513,32514,32515,32516,32642,32643,32644,32645,32646,32648,32649,32650
        StringSplit c, system_cursors, `,
        Loop %c0%
        {
            h_cursor   := DllCall( "LoadCursor", "Ptr",0, "Ptr",c%A_Index% )
            h%A_Index% := DllCall( "CopyImage", "Ptr",h_cursor, "UInt",2, "Int",0, "Int",0, "UInt",0 )
            b%A_Index% := DllCall( "CreateCursor", "Ptr",0, "Int",0, "Int",0
                , "Int",32, "Int",32, "Ptr",&AndMask, "Ptr",&XorMask )
        }
    }
    if (OnOff = 0 or OnOff = "Off" or $ = "h" and (OnOff < 0 or OnOff = "Toggle" or OnOff = "T"))
        $ = b  ; use blank cursors
    else
        $ = h  ; use the saved cursors

    Loop %c0%
    {
        h_cursor := DllCall( "CopyImage", "Ptr",%$%%A_Index%, "UInt",2, "Int",0, "Int",0, "UInt",0 )
        DllCall( "SetSystemCursor", "Ptr",h_cursor, "UInt",c%A_Index% )
    }
}
I use the next code in a script

Code: Select all

SystemCursor(0)
and it hides the cursor. My problem is how can I unhide my cursor?
In another script I put the next line code

Code: Select all

SystemCursor(1)
but the cursor is still hidden!
Any idea?

Re: Hide / unhide cursor mouse

Posted: 30 Jan 2015, 06:40
by just me
Because the function relies on overwriting the system cursors and keeping the originals within local static variables, you have to call SystemCursor(1) in the same script you called SystemCursor(0).

Re: Hide / unhide cursor mouse

Posted: 30 Jan 2015, 08:03
by empardopo
just me wrote:Because the function relies on overwriting the system cursors and keeping the originals within local static variables, you have to call SystemCursor(1) in the same script you called SystemCursor(0).
Ok thanks! But if I'm wrong, how then retrieve the cursor?
Thanks

Re: Hide / unhide cursor mouse

Posted: 30 Jan 2015, 19:10
by MJs
MAKE BOTH SCRIPTS COMMUNICATE WITH EACH OTHER, MAKE THAT LOCAL STATIC VARIABLES FIND THEIR WAY TO THE OTHER SCRIPT.
in other words for simplicity sake only:
just save the content of those local static variables to a file after you run your function SystemCursor(0), and let the second script read it, instead of creating it all over again.
let me know how that pans out!

Re: Hide / unhide cursor mouse

Posted: 30 Jan 2015, 20:19
by MJs
after some looking around:
http://www.autohotkey.com/board/topic/3 ... em-cursor/

Code: Select all

SPI_SETCURSORS := 0x57
DllCall( "SystemParametersInfo", UInt,SPI_SETCURSORS, UInt,0, UInt,0, UInt,0 )

Re: Hide / unhide cursor mouse

Posted: 31 Jan 2015, 12:30
by Masonjar13
I recently made an alternative to hide the cursor, as that function breaks my animated cursors. It's designed to keep the cursor in place, but you can get rid of that and make the gui the size of the screen instead, if that's what you want.

Code: Select all

detectHiddenWindows, On
gui +hwndgHwnd
gui,show,hide w1 h1
winset,transparent,1,ahk_id %gHwnd%
gui +alwaysOnTop +toolWindow -caption +0x80000000
return

ScrollLock::
while(a_timeidlephysical>300000) ; 5-minutes
    sleep 100
tog:=!tog
if(tog){
    blockinput,mousemove
    dllcall("ShowCursor","uint",0)
    mousegetpos,mx,my,active
    gui +Owner%active%
    gui,show,x%mx% y%my% noactivate
} else {
    blockinput,mousemoveoff
    gui,cancel
    dllcall("ShowCursor","uint",1)
}
return
For anyone who asks, I set the ShowCursor on and off every time so I could properly use the tray menu.

Re: Hide / unhide cursor mouse

Posted: 05 Feb 2015, 07:22
by empardopo
MJs wrote:MAKE BOTH SCRIPTS COMMUNICATE WITH EACH OTHER, MAKE THAT LOCAL STATIC VARIABLES FIND THEIR WAY TO THE OTHER SCRIPT.
in other words for simplicity sake only:
just save the content of those local static variables to a file after you run your function SystemCursor(0), and let the second script read it, instead of creating it all over again.
let me know how that pans out!
MJs, I have only a script

Code: Select all

SystemCursor(Init)
SystemCursor(Off)

return

Esc::
SystemCursor(On)
ExitApp
return
This script doesn't work fine! When I press ESC my cursor is hide.

I've added your new lines

Code: Select all

SystemCursor(Init)
SystemCursor(Off)

return

Esc::
;SystemCursor(On)
SPI_SETCURSORS := 0x57
DllCall( "SystemParametersInfo", UInt,SPI_SETCURSORS, UInt,0, UInt,0, UInt,0 )
ExitApp
return
And I get the same result.

I don't kno why?

Thanks

Re: Hide / unhide cursor mouse

Posted: 05 Feb 2015, 09:51
by empardopo
Sorry MJs. I've just read fully your before link and It works fine now. Thanks.

I've used this two functions

Code: Select all

SetSystemCursor( Cursor = "", cx = 0, cy = 0 )
{
	BlankCursor := 0, SystemCursor := 0, FileCursor := 0 ; init
	
	SystemCursors = 32512IDC_ARROW,32513IDC_IBEAM,32514IDC_WAIT,32515IDC_CROSS
	,32516IDC_UPARROW,32640IDC_SIZE,32641IDC_ICON,32642IDC_SIZENWSE
	,32643IDC_SIZENESW,32644IDC_SIZEWE,32645IDC_SIZENS,32646IDC_SIZEALL
	,32648IDC_NO,32649IDC_HAND,32650IDC_APPSTARTING,32651IDC_HELP
	
	If Cursor = ; empty, so create blank cursor 
	{
		VarSetCapacity( AndMask, 32*4, 0xFF ), VarSetCapacity( XorMask, 32*4, 0 )
		BlankCursor = 1 ; flag for later
	}
	Else If SubStr( Cursor,1,4 ) = "IDC_" ; load system cursor
	{
		Loop, Parse, SystemCursors, `,
		{
			CursorName := SubStr( A_Loopfield, 6, 15 ) ; get the cursor name, no trailing space with substr
			CursorID := SubStr( A_Loopfield, 1, 5 ) ; get the cursor id
			SystemCursor = 1
			If ( CursorName = Cursor )
			{
				CursorHandle := DllCall( "LoadCursor", Uint,0, Int,CursorID )	
				Break					
			}
		}	
		If CursorHandle = ; invalid cursor name given
		{
			Msgbox,, SetCursor, Error: Invalid cursor name
			CursorHandle = Error
		}
	}	
	Else If FileExist( Cursor )
	{
		SplitPath, Cursor,,, Ext ; auto-detect type
		If Ext = ico 
			uType := 0x1	
		Else If Ext in cur,ani
			uType := 0x2		
		Else ; invalid file ext
		{
			Msgbox,, SetCursor, Error: Invalid file type
			CursorHandle = Error
		}		
		FileCursor = 1
	}
	Else
	{	
		Msgbox,, SetCursor, Error: Invalid file path or cursor name
		CursorHandle = Error ; raise for later
	}
	If CursorHandle != Error 
	{
		Loop, Parse, SystemCursors, `,
		{
			If BlankCursor = 1 
			{
				Type = BlankCursor
				%Type%%A_Index% := DllCall( "CreateCursor"
				, Uint,0, Int,0, Int,0, Int,32, Int,32, Uint,&AndMask, Uint,&XorMask )
				CursorHandle := DllCall( "CopyImage", Uint,%Type%%A_Index%, Uint,0x2, Int,0, Int,0, Int,0 )
				DllCall( "SetSystemCursor", Uint,CursorHandle, Int,SubStr( A_Loopfield, 1, 5 ) )
			}			
			Else If SystemCursor = 1
			{
				Type = SystemCursor
				CursorHandle := DllCall( "LoadCursor", Uint,0, Int,CursorID )	
				%Type%%A_Index% := DllCall( "CopyImage"
				, Uint,CursorHandle, Uint,0x2, Int,cx, Int,cy, Uint,0 )		
				CursorHandle := DllCall( "CopyImage", Uint,%Type%%A_Index%, Uint,0x2, Int,0, Int,0, Int,0 )
				DllCall( "SetSystemCursor", Uint,CursorHandle, Int,SubStr( A_Loopfield, 1, 5 ) )
			}
			Else If FileCursor = 1
			{
				Type = FileCursor
				%Type%%A_Index% := DllCall( "LoadImageA"
				, UInt,0, Str,Cursor, UInt,uType, Int,cx, Int,cy, UInt,0x10 ) 
				DllCall( "SetSystemCursor", Uint,%Type%%A_Index%, Int,SubStr( A_Loopfield, 1, 5 ) )			
			}          
		}
	}	
}

RestoreCursors()
{
	SPI_SETCURSORS := 0x57
	DllCall( "SystemParametersInfo", UInt,SPI_SETCURSORS, UInt,0, UInt,0, UInt,0 )
}
Thanks very much

Re: Hide / unhide cursor mouse

Posted: 26 Dec 2016, 15:14
by Drako
HI! I'm using this last code from empardopo and it works fine, with my normal cursor. I would like to use it for Acrobat, meaning that I'm trying to make my cursor disappear while I go through the pages of a PDF with the keyboard, the problem is that the above code does not seem to work with the actobat's cursors, the hand for example.
Does anyone know how to make it work also with these cursors? Im sure it's a very simple change but I'm not up to that yet... :oops:
Thank you!

Re: Hide / unhide cursor mouse

Posted: 26 Jan 2017, 20:24
by iPhilip
The above code only works for system cursors. You might look at this strategy (move the cursor offscreen temporarily) instead.

Cheers!

Re: Hide / unhide cursor mouse

Posted: 27 Jan 2017, 00:27
by Masonjar13
iPhilip wrote:The above code only works for system cursors. You might look at this strategy (move the cursor offscreen temporarily) instead.

Cheers!
I can only assume this was made on Windows XP, because on 7, that's where the 'Show Desktop' button is located. By default, hovering over it will make all windows transparent. This is the same for 8.1, but I do not know about Windows 10. This makes it a not-so-great option, unless you've removed that button. Even then, the time would take its place and display its tooltip.
Drako wrote:HI! I'm using this last code from empardopo and it works fine, with my normal cursor. I would like to use it for Acrobat, meaning that I'm trying to make my cursor disappear while I go through the pages of a PDF with the keyboard, the problem is that the above code does not seem to work with the actobat's cursors, the hand for example.
Does anyone know how to make it work also with these cursors? Im sure it's a very simple change but I'm not up to that yet... :oops:
Thank you!
My posted script should work just fine for you.

Re: Hide / unhide cursor mouse

Posted: 27 Jan 2017, 03:43
by iPhilip
Hi Masonjar13,

Thank you. I appreciate you pointing out your script. I learned from it. Here is a simplified version that works for me (see signature block for OS):

Code: Select all

ScrollLock::
if (flag := !flag) {
   MouseGetPos, , , hwnd
   Gui Cursor:+Owner%hwnd%
   BlockInput MouseMove
   DllCall("ShowCursor", Int,0)
} else {
   BlockInput MouseMoveOff
   DllCall("ShowCursor", Int,1)
}
Return
Cheers!

Re: Hide / unhide cursor mouse

Posted: 27 Jan 2017, 05:29
by Masonjar13
That was originally what I had considered. However, if another window pops up, your cursor then becomes visible. You may alleviate that issue by setting a timer to constantly, but it will still flicker.

Code: Select all

ScrollLock::
if (flag := !flag) {
   setTimer,hideCursor,10
} else {
   setTimer,hideCursor,off
   BlockInput MouseMoveOff
   DllCall("ShowCursor", Int,1)
}
Return

hideCursor:
MouseGetPos, , , hwnd
Gui Cursor:+Owner%hwnd%
BlockInput MouseMove
DllCall("ShowCursor", Int,0)
return
You can test by having multiple windows open and switching using winkey+[number].

Re: Hide / unhide cursor mouse

Posted: 05 Dec 2018, 07:07
by Maestr0
or, alternatively:

Code: Select all

SystemCursor(0)
tooltip cursor is hidden for 3 seconds
sleep 3000
SystemCursor(1)
tooltip

SystemCursor(OnOff=1) {
	if (OnOff == 1)
	{
	   BlockInput MouseMoveOff
	   DllCall("ShowCursor", Int,1)	
	} 
	else 
	{
	   MouseGetPos, , , hwnd
	   Gui Cursor:+Owner%hwnd%
	   BlockInput MouseMove
	   DllCall("ShowCursor", Int,0)	
	}
}

Re: Hide / unhide cursor mouse

Posted: 22 Sep 2019, 05:24
by rediffusion
Hi Party People! :dance:

I looked at the documentation, but I can't find a solution!
How to do this:

Hide cursor

Code: Select all

	CoordMode, Mouse, Screen
	MouseClick, left, 1596, 1050
	MouseClick, left, 1420, 565
Show cursor

Re: Hide / unhide cursor mouse

Posted: 22 Sep 2019, 06:08
by Maestr0
rediffusion wrote:
22 Sep 2019, 05:24
Hi Party People! :dance:

I looked at the documentation, but I can't find a solution!
How to do this:

Hide cursor

Code: Select all

	CoordMode, Mouse, Screen
	MouseClick, left, 1596, 1050
	MouseClick, left, 1420, 565
Show cursor
Is this list correct? (in order):
1. hide the mouse cursor
2. click two specific places
3. show the mouse cursor

if so, try this:

Code: Select all

SystemCursor(0)
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos ; get current position
MouseClick, left, 1596, 1050,,0
MouseClick, left, 1420, 565,,0
MouseMove, xpos, ypos, 0
SystemCursor(1)

SystemCursor(OnOff:=1) {
  if (OnOff == 1)
  {
     BlockInput MouseMoveOff
     DllCall("ShowCursor", Int,1)	
  } 
  else 
  {
     MouseGetPos, , , hwnd
     Gui Cursor:+Owner%hwnd%
     BlockInput MouseMove
     DllCall("ShowCursor", Int,0)	
  }
}

Re: Hide / unhide cursor mouse

Posted: 22 Sep 2019, 16:15
by rediffusion
@Maestr0
I checked your code it's awesome. It's exactly what I want.
Many thanks, many love! :clap: