dcsimg
Results 1 to 30 of 30

Thread: How to detect mouse inactivity in VB.Net ?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    How to detect mouse inactivity in VB.Net ?

    Hi friends, I have a code and a problem, I would appreciate it if you help. You know that some sites do not process the mouse after a certain period of time to logout the site. I want to prevent a user from typing in a program to refresh the site when the computer is not at the logout .

    If the site is not processed for 5 minutes, check out the site. We create a counter and start it from the last time the mouse was active. 5 minutes before the end of the main page refresh. Then we reboot the meter. These operations continue in a loop. But whenever the mouse moves, we always reset the counter.

    Thank you very much for your help.

    Downloads: https://www.mediafire.com/file/ki6tp...topup.zip/file


    Or codes;

    Code:
    Option Strict On
    Option Explicit On
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")> Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
        End Function
    
        <StructLayout(LayoutKind.Sequential)> Structure LASTINPUTINFO
            <MarshalAs(UnmanagedType.U4)> Public cbSize As Integer
            <MarshalAs(UnmanagedType.U4)> Public dwTime As Integer
        End Structure
    
        Dim lastInputInf As New LASTINPUTINFO()
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 1000
            Timer1.Start()
            WebBrowser1.Navigate("https://etopup.vodafone.com.tr/ETOPUPGUI/")
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            WebBrowser1.Document.GetElementById("userLoginForm:userName").SetAttribute("value", My.Settings.username)
            WebBrowser1.Document.GetElementById("userLoginForm:userPassword").SetAttribute("value", My.Settings.password)
    
        End Sub
    
        Private Sub KullanıcıAdıVeŞifreKaydetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KullanıcıAdıVeŞifreKaydetToolStripMenuItem.Click
            Form2.ShowDialog()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
            lastInputInf.dwTime = 0
            GetLastInputInfo(lastInputInf)
            'ToolStripStatusLabel1.Text = "Inactivity Time: " & CInt((Environment.TickCount - lastInputInf.dwTime) / 1000).ToString
            If CInt((Environment.TickCount - lastInputInf.dwTime) / 10000) > 10 Then 'check if it has been 60 seconds
                'Do whatever commands here that you want to happen
                'if no user input is detected in last 60 seconds.
                WebBrowser1.Document.All("j_id5:mainPage_image1").InvokeMember("click")
    
                'ToolStripStatusLabel1.Text = CStr(CInt((Environment.TickCount - lastInputInf.dwTime)))
                'ToolStripStatusLabel1.Text = CStr(Marshal.SizeOf(lastInputInf))
    
            End If
            Timer1.Start()
        End Sub
    End Class
    Last edited by Enigmatic; Jul 30th, 2019 at 09:53 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    For application wide user interaction inactivity...

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        ''' <summary>
        ''' GetLastInputInfo API function
        ''' </summary>
        ''' <param name="plii">structure passed byref</param>
        ''' <returns>true if successful, else false</returns>
        ''' <remarks></remarks>
        Private Declare Function GetLastInputInfo Lib "user32.dll" _
            (ByRef plii As PLASTINPUTINFO) As Boolean
    
        ''' <summary>
        ''' PLASTINPUTINFO structure
        ''' </summary>
        ''' <remarks>1st member contains size of the instance of the structure.
        ''' 2nd member is pc idle time in ms returned (byref)</remarks>
        Private Structure PLASTINPUTINFO
            Dim cbSize As Integer
            Dim dwTime As Integer
        End Structure
    
        Private Declare Function SystemParametersInfo Lib "user32" _
                                 Alias "SystemParametersInfoA" (ByVal uAction As Integer, _
                                 ByVal uParam As Integer, ByRef lpvParam As Integer, _
                                 ByVal fuWinIni As Integer) As Integer
        Private Const SPI_GETSCREENSAVETIMEOUT As Integer = 14
    
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            
            Dim oPLII As PLASTINPUTINFO
            oPLII.cbSize = Marshal.SizeOf(oPLII)
    
            GetLastInputInfo(oPLII)
    
            TextBox1.Text = CType((Environment.TickCount - oPLII.dwTime) / 1000, String)
    
            'If (Environment.TickCount - oPLII.dwTime) / 1000 >= 300 Then
            '    'system inactive for 5 minutes
            'End If
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Timer1.Interval = 60 * 1000 '1 minute
            'Timer1.Start()
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    I'm sorry but it doesn't work the way I want. I've uploaded my own project files to the first message, and if you download them and try them out, you'll know exactly what I mean. The problem is, the counter does not reset when the web page is refreshed. Therefore, the page continues to refresh every second. After the page is refreshed, I want the counter to be reset and the time to start over. Imagine that this program is your windows screen saver. At the end of the set time, the screen saver is activated, and when you move the mouse, it is deactivated. I want to refresh a web page at the end of the set time. This is the only difference between the screen saver.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    How do I reset the time after the web page is refreshed? Is it too hard?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    Try this...

    Code:
    Option Strict On
    Option Explicit On
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")> Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
        End Function
    
        <StructLayout(LayoutKind.Sequential)> Structure LASTINPUTINFO
            <MarshalAs(UnmanagedType.U4)> Public cbSize As Integer
            <MarshalAs(UnmanagedType.U4)> Public dwTime As Integer
        End Structure
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 1000
            Timer1.Start()
            WebBrowser1.Navigate("https://etopup.vodafone.com.tr/ETOPUPGUI/")
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            WebBrowser1.Document.GetElementById("userLoginForm:userName").SetAttribute("value", My.Settings.username)
            WebBrowser1.Document.GetElementById("userLoginForm:userPassword").SetAttribute("value", My.Settings.password)
    
        End Sub
    
        Private Sub KullanıcıAdıVeŞifreKaydetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KullanıcıAdıVeŞifreKaydetToolStripMenuItem.Click
            Form2.ShowDialog()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim lastInputInf As New LASTINPUTINFO()
            lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
    
            GetLastInputInfo(lastInputInf)
            ToolStripStatusLabel1.Text = "Inactivity Time: " & CInt((Environment.TickCount - lastInputInf.dwTime) / 1000).ToString
            If CInt((Environment.TickCount - lastInputInf.dwTime) / 10000) > 10 Then 'check if it has been 60 seconds
                'Do whatever commands here that you want to happen
                'if no user input is detected in last 60 seconds.
                WebBrowser1.Document.All("j_id5:mainPage_image1").InvokeMember("click")
    
                'ToolStripStatusLabel1.Text = CStr(CInt((Environment.TickCount - lastInputInf.dwTime)))
                'ToolStripStatusLabel1.Text = CStr(Marshal.SizeOf(lastInputInf))
    
            End If
            'Timer1.Start()
        End Sub
    End Class

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    I'm not sure exactly what you're asking, but the method you're using only tracks USER activity. If the page automatically refreshes, that isn't user activity and you'll need to catch the refresh a different way, then programmatically trigger some user activity if that's what you're looking for...

  7. #7

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Dear Paul, all I want is ...

    When the web page is refreshed, I want the time to be reset and restarted. Is it too hard? Like an example of a screen saver. I'll just refresh the web page instead of running scr.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    What time? As soon as the mousepointer enters the page, the counter resets to zero with my modifications...

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    if that's not how you want it to work, that is exactly how GetLastInputInfo works. Any user activity mousemove, click, keypress, they're all user activity...

  10. #10

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    The mouse activity resets the counter, but the important thing is to refresh the web page when the time reaches the specified time and restart the counter from zero. The page is refreshed every second because the time is not refreshed when the time expires.

    The time is reset when the mouse or keyboard moves, but the time needs to be reset when the web page is refreshed. Because the time is not reset, it refreshes every second.

    Typing again and again is boring, but when you enter the windows screen saver settings, you set the wait time, and when you release the mouse, the screen saver opens and resets the time, and when you touch the mouse or keyboard, the time is reset.

    But if the code I give does not move the mouse or keyboard, the time starts, the web page refreshes after the specified time, but the refresh process continues every second and does not stop, I want to reset the inactive time and restart the web page. Clicking the mouse and keyboard resets the time, there is no problem with it.

  11. #11

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Okay, let me ask you, in the simplest way with this code, how do I make a screen saver that will be activated after 5 seconds. That's what I want ...

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    So...

    If [the count] = 60 then
    refresh webbrowser
    button.performclick 'hidden button on page, counts as user activity if i'm not mistaken
    . 'Any user input resets the count
    . 'Also look at mouse_event API
    End if

  13. #13

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Look, this code is working right now. The mouse and keyboard gestures are reset and restarted. But when the web page is refreshed, the time needs to be reset, since the time is not reset, it refreshes every second. The only problem in the code is that the web page refreshes every second after the time expires.

  14. #14

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Like this;

    Code:
    idle time = 5 sec
    
    if when system inactive > 5 sec
    
    webbrowser refresh
    
    reset idle time
    
    end if

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    What do you mean resets every second? The API works on user input. Any user input resets the count to zero. That's how it works. Sounds like you just need a timer with a 60 second interval. Your zipped project doesn't load the webpage. You really need to help us to help you.

  16. #16

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Well, I changed the site and reloaded the code, the site doesn't matter, it can be any site, you can change it yourself, but you need to change WebBrowser1.Document.All to your site, or webbrowser1.Document.All where webbrowser1.navigate = " Type https://www.google.com "doesn't matter. The important thing is that after the page is refreshed, the time is reset and the time starts again. Let the cycle continue. Do not refresh the page every second.

    Download Link: https://www.mediafire.com/file/d4p2m...topup.zip/file

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    So don't use a one second timer. If you want to check for 60 seconds or slightly more of user inactivity, use a 60 second timer???

  18. #18

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    There is no second timer anyway. Only have one.

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    Use a timer with the correct interval
    Check the idle time
    Refresh webpage if necessary
    Then - the only way to reset the idle time programmatically is to simulate user activity
    How to simulate user activity is your next task
    That is why i recommended the mouse_event API

    That are your exact steps to get your app running how you're expecting

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    An alternative is to use the Mod operator on the idle time...

    20 mod 20 = 0
    15 mod 20 = 15
    95 mod 20 = 15

    Etc

  21. #21

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Dear Paul, thank you very much for your help and effort. But I want to continue with this code. It already has a little trick. Only when the Web browser is refreshed, the inactivity time will be reset.

    I'm waiting for the help of friends with knowledge.

    Updated project files:https://www.mediafire.com/file/d4p2m...topup.zip/file

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    I gave you knowledge. You didn't take it...

  23. #23

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Thank you Paul. I'm not a complete professional, so I want to continue with the code with me. If I continue with another code, it can make me go back to the beginning. Already in the code there is only one problem. The counter does not reset only after the web page is refreshed. A little problem for someone who knows.

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    Your question title is how to detect mouse inactivity. I've told you several times how to do that. Your OP says you want to prevent input after a certain amount of inactivity. That's a completely different question and there's no evidence in your code that shows you've made any progress with that.

    So really, for anyone to help in any way, you need to bring us up to speed and explain which parts you have working and which parts you currently need help with.

    I don't have a lot of time to spend helping with this, but someone else might be able to help...

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    If you want a counter that only resets under some conditions, you need to use a variable for your counter. I've explained how the idle time works and what you want it to do isn't possible

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    Quote Originally Posted by Enigmatic View Post
    Look, this code is working right now. The mouse and keyboard gestures are reset and restarted. But when the web page is refreshed, the time needs to be reset, since the time is not reset, it refreshes every second. The only problem in the code is that the web page refreshes every second after the time expires.
    Ok take from there then. As i keep telling you, the only way to reset the idle time is to move the mouse or press a key. That's not going to happen. You could simulate user activity, which would reset the idle time.

    Or...

    Code:
    If CInt(Math.Floor((Environment.TickCount - lastInputInf.dwTime) / 1000)) mod 60 = 0 Then 'check if it has been 60 seconds
    That's my two penny-worth, now i'm unsubscribing from this thread
    Last edited by .paul.; Aug 3rd, 2019 at 04:42 PM.

  27. #27

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    Yes, dear paul, now it works the way I want it, but I used your code differently, and if I didn't, it would refresh every time I moved the mouse. Thank you very much for your help. I've exhausted you.

    Code:
            If CInt((Environment.TickCount - lastInputInf.dwTime) / 1000) > 60 Then
                If CInt(Math.Floor((Environment.TickCount - lastInputInf.dwTime) / 1000)) Mod 60 = 0 Then 'check if it has been 60 seconds
                    WebBrowser1.Document.All("homelink").InvokeMember("click")
                End If
            End If

  28. #28
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,073

    Re: How to detect mouse inactivity in VB.Net ?

    This was very painful to read....

    Paul you must get a medal for forbearance
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  29. #29

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: How to detect mouse inactivity in VB.Net ?

    @schoemr u re right

    Name:  images (3).jpeg
Views: 43
Size:  12.3 KB

  30. #30
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,415

    Re: How to detect mouse inactivity in VB.Net ?

    Quote Originally Posted by Enigmatic View Post
    Yes, dear paul, now it works the way I want it, but I used your code differently, and if I didn't, it would refresh every time I moved the mouse. Thank you very much for your help. I've exhausted you.

    Code:
            If CInt((Environment.TickCount - lastInputInf.dwTime) / 1000) > 60 Then
                If CInt(Math.Floor((Environment.TickCount - lastInputInf.dwTime) / 1000)) Mod 60 = 0 Then 'check if it has been 60 seconds
                    WebBrowser1.Document.All("homelink").InvokeMember("click")
                End If
            End If
    That will refresh after 2 minutes, then every minute thereafter. To refresh after 1 minute, then every minute thereafter...

    Code:
    If CInt((Environment.TickCount - lastInputInf.dwTime) / 1000) >= 60 Then

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width