Hide / unhide cursor mouse

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Hide / unhide cursor mouse

30 Jan 2015, 06:11

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?
Everything is possible!
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Hide / unhide cursor mouse

30 Jan 2015, 06:40

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).
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Hide / unhide cursor mouse

30 Jan 2015, 08:03

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
Everything is possible!
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Hide / unhide cursor mouse

30 Jan 2015, 19:10

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
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Hide / unhide cursor mouse

30 Jan 2015, 20:19

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 )
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Hide / unhide cursor mouse

31 Jan 2015, 12:30

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.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Hide / unhide cursor mouse

05 Feb 2015, 07:22

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
Everything is possible!
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Hide / unhide cursor mouse

05 Feb 2015, 09:51

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
Everything is possible!
Drako
Posts: 30
Joined: 14 Jan 2016, 15:08

Re: Hide / unhide cursor mouse

26 Dec 2016, 15:14

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!
iPhilip
Posts: 796
Joined: 02 Oct 2013, 12:21

Re: Hide / unhide cursor mouse

26 Jan 2017, 20:24

The above code only works for system cursors. You might look at this strategy (move the cursor offscreen temporarily) instead.

Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Hide / unhide cursor mouse

27 Jan 2017, 00:27

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.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
iPhilip
Posts: 796
Joined: 02 Oct 2013, 12:21

Re: Hide / unhide cursor mouse

27 Jan 2017, 03:43

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!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Hide / unhide cursor mouse

27 Jan 2017, 05:29

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].
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Hide / unhide cursor mouse

05 Dec 2018, 07:07

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)	
	}
}
User avatar
rediffusion
Posts: 58
Joined: 15 Mar 2019, 05:16

Re: Hide / unhide cursor mouse

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
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Hide / unhide cursor mouse

22 Sep 2019, 06:08

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)	
  }
}
User avatar
rediffusion
Posts: 58
Joined: 15 Mar 2019, 05:16

Re: Hide / unhide cursor mouse

22 Sep 2019, 16:15

@Maestr0
I checked your code it's awesome. It's exactly what I want.
Many thanks, many love! :clap:
cleetz
Posts: 25
Joined: 04 Feb 2023, 15:09

Re: Hide / unhide cursor mouse

29 Feb 2024, 05:37

Code: Select all



SystemCursor(0) ; This will hide the cursor when the script starts


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



Toggle := 0 ; 0 means cursor is shown, 1 means hidden
F1:: ; F1 to pause and resume cursor visibility
    Toggle := !Toggle
    SystemCursor(Toggle)
Return

this will turn the cursor off when starting the script, then F1 Turns it back on

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, RussF, TheDewd and 137 guests