Results 1 to 14 of 14

Thread: [RESOLVED] GetCursorPos API and Date problem

  1. #1

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Resolved [RESOLVED] GetCursorPos API and Date problem

    I have a module with a module level date variable.
    It also uses the GetCursorPos API.
    The problem is that every time the GetCursorPos call is made, the date variable is being reset to '11/15/2006 12:45:00 AM '

    Is this a known issue?
    Any way to fix it?

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: GetCursorPos API and Date problem

    that sounds a bit unlikely - can you post your code

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: GetCursorPos API and Date problem

    You might have declared the GetCursorPos function the wrong way, that's the only reason I can see how other memory resources are written over. Can you please post the declaration you're using for the function and for the POINTAPI structure.

  4. #4

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: GetCursorPos API and Date problem

    It's a modified version of some code I got from PSC.

    It checks to see how long a user has been idle.

    I set a watch on the g_lIdleTime variable to find were the reset was happening.

    VB Code:
    1. Option Explicit
    2. '**************************************
    3. 'Windows API/Global Declarations for :check for user activity throughout the system
    4. '**************************************
    5.  
    6. Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
    7.  
    8. Private Declare Sub GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI)
    9.  
    10. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
    11.  
    12. Private Type POINTAPI
    13.     X As Integer
    14.     Y As Integer
    15. End Type
    16.  
    17. Private posOld As POINTAPI
    18. Private posNew As POINTAPI
    19.  
    20. Private m_dStartTime As Date
    21. Public g_lIdleTime As Long 'number of seconds the user has been idle
    22. '**************************************
    23. ' Name: check for user activity througho
    24. '     ut the system
    25. ' Description:check for user activity throughout the system ,comes in handy when you want to check if the user is uses his computer or not and then take some action depending on it like showing a screensaver or do some maintenance or whatever ofcourse you should use this in combination with a timer control or function
    26. ' By: Michel Posseth
    27. '**************************************
    28. Public Sub CheckIdleTime()
    29. 'this sub needs to be called from a timer or loop
    30.     Dim lX As Long
    31.    
    32.     If m_dStartTime = "12:00:00 AM" Then
    33.         m_dStartTime = Now
    34.         'clear the CheckKeys
    35.         For lX = 32 To 126
    36.             GetAsyncKeyState Asc(Chr$(lX))
    37.         Next
    38.     End If
    39.     If InputCheck Then
    40.         g_lIdleTime = 0
    41.         m_dStartTime = Now
    42.     Else
    43.         g_lIdleTime = DateDiff("s", m_dStartTime, Now)
    44.     End If
    45. End Sub
    46.  
    47. Private Function InputCheck() As Boolean
    48.     Dim i As Integer
    49.     Dim KeyVal As Integer 'key value
    50.    
    51.     ' take mouse coordinates as they are
    52.     Call GetCursorPos(posNew)
    53.     ' compare them with the previous values
    54.     If ((posNew.X <> posOld.X) Or (posNew.Y <> posOld.Y)) Then
    55. '    Beep 50, 50
    56.         posOld = posNew
    57.         InputCheck = True
    58.         'mouse has been moved no need to check further
    59.         Exit Function
    60.     End If
    61.    
    62.     For KeyVal = 32 To 126
    63.         'check the keys, allow it to loop all keys to clear them
    64.         InputCheck = CBool(GetAsyncKeyState(Asc(Chr$(KeyVal))))
    65.     Next
    66.    
    67. End Function

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: GetCursorPos API and Date problem

    try this
    VB Code:
    1. Option Explicit
    2. '**************************************
    3. 'Windows API/Global Declarations for :check for user activity throughout the system
    4. '**************************************
    5.  
    6. Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
    7.  
    8. Private Declare Sub GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI)
    9.  
    10. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
    11.  
    12. Private Type POINTAPI
    13.     X As Integer
    14.     Y As Integer
    15. End Type
    16.  
    17. Private posOld As POINTAPI
    18. Private posNew As POINTAPI
    19.  
    20.  
    21. Public g_lIdleTime As Long 'number of seconds the user has been idle
    22. '**************************************
    23. ' Name: check for user activity througho
    24. '     ut the system
    25. ' Description:check for user activity throughout the system ,comes in handy when you want to check if the user is uses his computer or not and then take some action depending on it like showing a screensaver or do some maintenance or whatever ofcourse you should use this in combination with a timer control or function
    26. ' By: Michel Posseth
    27. '**************************************
    28. Public Sub CheckIdleTime()
    29. [B]Dim m_dStartTime As Date 'postion changed[/B]
    30. 'this sub needs to be called from a timer or loop
    31.     Dim lX As Long
    32.    
    33.     If m_dStartTime = "12:00:00 AM" Then
    34.         m_dStartTime = Now
    35.         'clear the CheckKeys
    36.         For lX = 32 To 126
    37.             GetAsyncKeyState Asc(Chr$(lX))
    38.         Next
    39.     End If
    40.     If InputCheck Then
    41.         g_lIdleTime = 0
    42.         m_dStartTime = Now
    43.     Else
    44.         g_lIdleTime = DateDiff("s", m_dStartTime, Now)
    45.     End If
    46. End Sub
    47.  
    48. Private Function InputCheck() As Boolean
    49.     Dim i As Integer
    50.     Dim KeyVal As Integer 'key value
    51.    
    52.     ' take mouse coordinates as they are
    53.     Call GetCursorPos(posNew)
    54.     ' compare them with the previous values
    55.     If ((posNew.X <> posOld.X) Or (posNew.Y <> posOld.Y)) Then
    56. '    Beep 50, 50
    57.         posOld = posNew
    58.         InputCheck = True
    59.         'mouse has been moved no need to check further
    60.         Exit Function
    61.     End If
    62.    
    63.     For KeyVal = 32 To 126
    64.         'check the keys, allow it to loop all keys to clear them
    65.         InputCheck = CBool(GetAsyncKeyState(Asc(Chr$(KeyVal))))
    66.     Next
    67.    
    68. End Function
    69.  
    70. Private Sub Command1_Click()
    71. Call CheckIdleTime
    72. End Sub
    Please mark you thread resolved using the Thread Tools as shown

  6. #6

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: GetCursorPos API and Date problem

    i mis-said something.

    It's the m_dStartTime variable that's being reset and has a watch set.

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: GetCursorPos API and Date problem

    How do you call this function?
    Please mark you thread resolved using the Thread Tools as shown

  8. #8

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: GetCursorPos API and Date problem

    Quote Originally Posted by danasegarane
    try this
    Moving the m_dStartTime variable into a sub won't work unless it's dimed as Static.
    It's value needs to be carried over time.

    I just tried it as a static but it still has the same problem.

    Also, the module needs to be called by a timer or loop.

  9. #9

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: GetCursorPos API and Date problem

    My computer had been running several days without a reboot.
    I just rebooted to see if XP was acting up, but it didn't help.

    dStartTime is still being reset at:
    Call GetCursorPos(posNew)

    In the InputCheck Function
    Last edited by longwolf; Nov 15th, 2006 at 10:03 AM.

  10. #10

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: GetCursorPos API and Date problem

    I just came up with a hack that works, but I'd still like to know what's going on with the original code

    VB Code:
    1. Private Function InputCheck() As Boolean
    2.     Dim i As Integer
    3.     Dim KeyVal As Integer 'key value
    4.     Dim dHack As Date
    5.    
    6.     ' take mouse coordinates as they are
    7.     dHack = m_dStartTime 'this shouldn't be needed but GetCursorPos is changing m_dStartTime
    8.     Call GetCursorPos(posNew)
    9.     m_dStartTime = dHack
    10.     ' compare them with the previous values
    11.     If ((posNew.X <> posOld.X) Or (posNew.Y <> posOld.Y)) Then
    12. '    Beep 50, 50
    13.         posOld = posNew
    14.         InputCheck = True
    15.         'mouse has been moved no need to check further
    16.         Exit Function
    17.     End If
    18.    
    19.     For KeyVal = 32 To 126
    20.         'check the keys, allow it to loop all keys to clear them
    21.         InputCheck = CBool(GetAsyncKeyState(Asc(Chr$(KeyVal))))
    22.     Next
    23.    
    24. End Function
    Last edited by longwolf; Nov 15th, 2006 at 10:04 AM.

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: GetCursorPos API and Date problem

    The problem is in the POINTAPI declaration, it should contain two 32-bit integers but you have assigned two 16-bit integers to it, so when you call the function it writes information to a 64-bit memory area, but your POINTAPI is only 32-bit which means that other data is overwritten, which happens to be another variable, which is why you don't get a GPF. Change the declaration to:
    VB Code:
    1. Private Type POINTAPI
    2.     X As [b]Long [/b]
    3.     Y As [b]Long [/b]
    4. End Type

  12. #12

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: GetCursorPos API and Date problem

    Thanks Joacim!
    That fixed it.

    I guess that's what I get for using cut-in-paste code without studying every line

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] GetCursorPos API and Date problem

    a couple of further comments about the code:
    VB Code:
    1. Asc(Chr$(lX))
    is pointless - you're taking a virtual KeyCode, interpretting it as a ASCII number (which it isn't) to make a String and then reading the ASCII value of the string - i.e. you end up back where you started.

    VB Code:
    1. For KeyVal = 32 To 126
    2.         'check the keys, allow it to loop all keys to clear them
    3.         InputCheck = CBool(GetAsyncKeyState(Asc(Chr$(KeyVal))))
    4.     Next
    InputCheck will only ever be equal to the last GetAsyncKeyState in the loop - i.e. InputCheck will only be true if whatever key 126 is, is depressed - you can bash away at all the other keys without the idle time ever being reset.

    VB Code:
    1. For lX = 32 To 126
    2.             GetAsyncKeyState Asc(Chr$(lX))
    3.         Next
    GetAsyncKeyState doesn't do anything when it's called - the only functionality you get from it is by capturing the return value - which you're not doing, so this is pointless too.

    All in all, this is a great example of why most of the code at PSC can be ignored - it's crap. I'd recommend going in search of some better stuff, here or at FreeVBCode for example.

  14. #14

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: [RESOLVED] GetCursorPos API and Date problem

    Yep, I thought the Asc(Chr$(lX)) looked funny.
    I hadn't gotten o the point of optimizing the code.
    I just needed to find out what was causing the date reset.

    But I removed an "Exit For" from the CBool(GetAsyncKeyState(KeyVal)) loop because I found that test was catching each key.

    I'm using a timer with a 2 second delay to call CheckIdleTime.
    Try replacing the InputCheck function with the next code and see what the Debug.Print puts out.

    VB Code:
    1. Private Function InputCheck() As Boolean
    2.     Dim KeyVal As Integer
    3.    
    4.     ' take mouse coordinates as they are
    5.     Call GetCursorPos(posNew)
    6.     ' compare them with the previous values
    7.     If ((posNew.X <> posOld.X) Or (posNew.Y <> posOld.Y)) Then
    8.         posOld = posNew
    9.         InputCheck = True
    10.         'mouse has been moved no need to check further
    11.         Exit Function
    12.     End If
    13.    
    14.     For KeyVal = 32 To 126
    15.         'check the keys, allow it to loop all keys to clear them\
    16.         InputCheck = CBool(GetAsyncKeyState(KeyVal))
    17.         If InputCheck Then
    18.             Beep '50, 50
    19.             Debug.Print KeyVal
    20.             Exit For
    21.         End If
    22.     Next
    23.    
    24. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width