Jump to content
Sign in to follow this  
Sedoc94

How to check for Cursor change?

Recommended Posts

Sedoc94

Hello,

How would I go check with AutoIt if my cursor has been

changed to cursor x?

It should check if the cursor has changed to cursor x

(a cursor thats not installed with windows)

x = should be the custom installed cursor

Thanks a bunch!

- Sedoc94

Share this post


Link to post
Share on other sites
Sedoc94

Yeah, but as the cursors are all custom cursors it would always return id 0 (unkown), right? :mellow:

Share this post


Link to post
Share on other sites
monoscout999

inside your GUI or in another window?

Share this post


Link to post
Share on other sites
Sedoc94

My script doesn't have a GUI...

So in another window, yeah...

Share this post


Link to post
Share on other sites
wakillon

Sorry, MouseGetCursor doesn't recognize external cursor !


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
monoscout999

but your idea is monitoring contantly the cursor and know when it change?

Share this post


Link to post
Share on other sites
monoscout999

#Include <WinAPI.au3>
local $hCursor, $hOldCursor = 0
While True
    $hCursor = _WinAPI_GetCursorInfo()
    If $hOldCursor <> $hCursor[2] Then
        $hOldCursor = $hCursor[2]
        consolewrite("The Cursor has changed the handle to the new cursor is "&$hCursor[2]&@CRLF)
    EndIf
    Sleep(500)
WEnd

This is an easy way to do it.

Share this post


Link to post
Share on other sites
wakillon

@monoscout999

That doesn't permit him to identify the custom installed cursor.


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
Sedoc94

@monoscout999

That doesn't permit him to identify the custom installed cursor.

Yeah...

So I guess with AutoIt that is impossible..

  • Like 1

Share this post


Link to post
Share on other sites
jvanegmond

Yeah...

So I guess with AutoIt that is impossible..

Did you try the example or are you just saying "Yeah.." because you are too lazy to try it?

I don't think wakillon is right in this case. Because $aCursor (return from function) element 2 is a handle to the cursor, which is a HCURSOR. A quote from MSDN:

A cursor handle is a unique value of the HCURSOR type that identifies a standard or custom cursor.

Share this post


Link to post
Share on other sites
UEZ

Will this help you?

$old = _WinAPI_GetCursor ()
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $old = ' & $old & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
_WinAPI_SetCursor(5)
If _WinAPI_GetCursor() <> $old Then MsgBox(0, "Test", "Cursor has changed")
_WinAPI_SetCursor($old)


; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GetCursor
; Description....: Retrieves a handle to the current cursor.
; Syntax.........: _WinAPI_GetCursor ( )
; Parameters.....: None
; Return values..: Success - Handle to the current cursor. If there is no cursor, the return value is 0.
;                  Failure - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ GetCursor
; Example........: Yes
; ===============================================================================================================================
Func _WinAPI_GetCursor()
    Local $Ret = DllCall('user32.dll', 'ptr', 'GetCursor')
    If @error Then Return SetError(1, 0, 0)
    Return $Ret[0]
EndFunc   ;==>_WinAPI_GetCursor

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_SetCursor
; Description ...: Establishes the cursor shape
; Syntax.........: _WinAPI_SetCursor($hCursor)
; Parameters ....: $hCursor     - Identifies the cursor
; Return values .: Success      - The handle of the previous cursor, if there was one.  If there  was  no  previous  cursor,  the
;                  +return value is 0.
; Author ........: Paul Campbell (PaulIA)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ SetCursor
; Example .......:
; ===============================================================================================================================
Func _WinAPI_SetCursor($hCursor)
    Local $aResult = DllCall("user32.dll", "handle", "SetCursor", "handle", $hCursor)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_SetCursor

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
wakillon

Did you try the example or are you just saying "Yeah.." because you are too lazy to try it?

I don't think wakillon is right in this case. Because $aCursor (return from function) element 2 is a handle to the cursor, which is a HCURSOR. A quote from MSDN:

Ok for the handle, but that doesn't permit to identify ( find his name or path ) the custom ( or external ) installed cursor Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
monoscout999

Ok for the handle, but that doesn't permit to identify ( find his name or path ) the custom ( or external ) installed cursor Posted Image

Yes I know what you mean, I have done this another example.

Global Const $aCheckCursor[15][2] = [["IDC_APPSTARTING", 32650], _
        ["IDC_HAND", 32649], _
        ["IDC_ARROW", 32512], _
        ["IDC_CROSS", 32515], _
        ["IDC_IBEAM", 32513], _
        ["IDC_ICON", 32641], _
        ["IDC_NO", 32648], _
        ["IDC_SIZE", 32640], _
        ["IDC_SIZEALL", 32646], _
        ["IDC_SIZENESW", 32643], _
        ["IDC_SIZENS", 32645], _
        ["IDC_SIZENWSE", 32642], _
        ["IDC_SIZEWE", 32644], _
        ["IDC_UPARROW", 32516], _
        ["IDC_WAIT", 32514]]
#include <WinAPI.au3>
Local $hCursor, $hOldCursor = 0
While True
    $hCursor = _WinAPI_GetCursorInfo()
    If $hOldCursor <> $hCursor[2] Then
        $hOldCursor = $hCursor[2]
        For $i = 0 To UBound($aCheckCursor) - 1
            If _WinAPI_LoadCursor(0, $aCheckCursor[$i][1]) = $hCursor[2] Then
                ConsoleWrite("The Cursor has changed the handle to the new cursor is " & $hCursor[2] & @CRLF & "And the name of the cursor resource is " & $aCheckCursor[$i][0] & @CRLF)
            EndIf
        Next
    EndIf
    Sleep(500)
WEnd
; _WinAPI_LoadCursor() Also you can find it in the WinAPIEx.au3 UDF
; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_LoadCursor
; Description....: Loads the specified cursor resource from the executable (.EXE) file.
; Syntax.........: _WinAPI_LoadCursor ( $hInstance, $sName )
; Parameters.....: $hInstance - Handle to an instance of the module whose executable file contains the cursor to be loaded.
;                  $sName     - The name of the cursor resource or resource identifier to be loaded. To use one of the predefined
;                               cursors, the application must set the $hInstance parameter to 0 and the $sName parameter to one
;                               of the following values.
;
;                               $IDC_APPSTARTING
;                               $IDC_HAND
;                               $IDC_ARROW
;                               $IDC_CROSS
;                               $IDC_IBEAM
;                               $IDC_ICON
;                               $IDC_NO
;                               $IDC_SIZE
;                               $IDC_SIZEALL
;                               $IDC_SIZENESW
;                               $IDC_SIZENS
;                               $IDC_SIZENWSE
;                               $IDC_SIZEWE
;                               $IDC_UPARROW
;                               $IDC_WAIT
;
; Return values..: Success    - Handle to the newly loaded cursor.
;                  Failure    - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: This function loads the cursor resource only if it has not been loaded; otherwise, it retrieves the handle to
;                  the existing resource.
; Related........:
; Link...........: @@MsdnLink@@ LoadCursor
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_LoadCursor($hInstance, $sName)

    Local $TypeOfName = 'int'

    If IsString($sName) Then
        $TypeOfName = 'wstr'
    EndIf

    Local $Ret = DllCall('user32.dll', 'ptr', 'LoadCursorW', 'ptr', $hInstance, $TypeOfName, $sName)

    If (@error) Or (Not $Ret[0]) Then
        Return SetError(1, 0, 0)
    EndIf
    Return $Ret[0]
EndFunc   ;==>_WinAPI_LoadCursor

This also retrieve the cursor name form the resource and compare the handles to mach what handle is being used. If you are using external cursors maybe this will be more complicated.

Edited by monoscout999

Share this post


Link to post
Share on other sites
wakillon

@monoscout999

I have tried your solution and it doesn't work with an external cursor ( .cur )

It returns IDC_IBEAM for all cursor tried.

It seems to be valid only for a cursor installed with windows.

Sedoc94 wants to know when his external cursor has been choose.


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
monoscout999

If Sedoc94 knows what .cur files he have, maybe he can do a sctipt that saves all the cursors handles into a variable and later make the comparison, like i did with the default windows cursors.

he need to load all the cursors using the function _WinAPI_LoadCursorFromFile() this function returns cursor handles, save all the handles in an array and compare these handles with the one returned by _WinAPI_GetCursorInfo()

Edited by monoscout999

Share this post


Link to post
Share on other sites
monoscout999

Why do I think this is for a game?

I don`t know. Why do you think that? Edited by monoscout999

Share this post


Link to post
Share on other sites
Yashied
monoscout999

You can try to use the GetIconInfoEx() function. See szModName and szResName members from the ICONINFOEX structure.

Thanks @yashired.

Most of it is extracted fromthe help file of the _WinAPI_GetIconInfoEx() function located in the

The only member of the struct that is not retrieving is the "ResName". i don`t have any idea why not.

#include <WinAPIex.au3>
Local $hCursor, $hOldCursor = 0
While True
    $hCursor = _WinAPI_GetCursorInfo()
    If $hOldCursor <> $hCursor[2] Then
        $hOldCursor = $hCursor[2]
        $tIIEX = _WinAPI_GetIconInfoEx($hCursor[2])
        ConsoleWrite('Handle: ' & $hCursor[2] & @CR)
        ConsoleWrite('Path:   ' & DllStructGetData($tIIEX, 'ModName') & @CR)
        ConsoleWrite('ID:     ' & DllStructGetData($tIIEX, 'ResID') & @CR)
        ConsoleWrite('Name:   ' & DllStructGetData($tIIEX, 'ResName') & @CR)
    EndIf
    Sleep(500)
WEnd

Can you try this?

EDIt: For me is not working with custom cursors, i do a test with a game and it gets the handle of the cursor but not the any other info.

Edited by monoscout999

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.