Skip to content

Instantly share code, notes, and snippets.

@r-k-b
Last active July 5, 2023 17:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-k-b/783ecb4d4bb14a1fadc6f2ce95af0bae to your computer and use it in GitHub Desktop.
Save r-k-b/783ecb4d4bb14a1fadc6f2ce95af0bae to your computer and use it in GitHub Desktop.
AutoHotKey: Hide Mouse Cursor When Idle
; Saved from https://autohotkey.com/board/topic/42561-hide-mouse-cursor-when-idle/?p=391177
; see also: https://superuser.com/a/68302/144573
; see also: https://github.com/r-k-b/autohotkeys/blob/master/windows/hide-idle-mouse.ahk
Last_Move := A_TickCount
State := True
Loop
{
MouseGetPos, Mouse_X, Mouse_Y
If(Mouse_X != Last_X || Mouse_Y != Last_Y)
{
If(State == False)
SystemCursor("On")
State := True
Last_Move := A_TickCount
}
If(A_TickCount > Last_Move + 3000 && State == True) ;Change the 3000 to the desired time...
{
SystemCursor("Off")
State := False
}
Last_X := Mouse_X, Last_Y := Mouse_Y
}
;### This function taken from the tutorial ###
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", "uint",0, "uint",c%A_Index% )
h%A_Index% := DllCall( "CopyImage", "uint",h_cursor, "uint",2, "int",0, "int",0, "uint",0 )
b%A_Index% := DllCall("CreateCursor","uint",0, "int",0, "int",0
, "int",32, "int",32, "uint",&AndMask, "uint",&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", "uint",%$%%A_Index%, "uint",2, "int",0, "int",0, "uint",0 )
DllCall( "SetSystemCursor", "uint",h_cursor, "uint",c%A_Index% )
}
}
@xcloudx01
Copy link

Thank you so much! This is exactly what I was looking for. It's working well 💖

Copy link

ghost commented Jul 5, 2023

Thanks but you should probably include something like "Sleep 100" inside the loop, this is wasting a lot of CPU cycles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment