Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to simulate user action to prevent system being considered idle

I know this question must have been asked hundreds of times before and so, maybe, I'm just doing something wrong. But I have a WinForms program I'm writing to try and keep the system appearing active so as to stop it as registering idle.

I figured that having a Timer on my form and doing something as simple as either moving the mouse via System.Windows.Forms.Cursor.Position or using the SendKeys.Send method would be enough to register user interaction, but it's not registering as user action and still letting the system appear as inactive after a set amount of time.

My code is fairly straightforward... Either:

Private Sub Updater_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updater.Tick
    SendKeys.Send("+")
End Sub

Or doing something along the lines of:

Private Sub Updater_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updater.Tick
    Dim MyMousePosition As Point
    MyMousePosition = Windows.Forms.Cursor.Position

    Windows.Forms.Cursor.Position = New Point(MyMousePosition.X - 10, MyMousePosition.Y)
End Sub

But neither is doing the trick... How can I get this to work AND preferably in a way that would be of least inconvnience to a user if they are actually using the system? (Meaning that I don't want to send a bunch of keys that may mess up the user if they're actually being active or move the mouse clear across the screen)

I know this coce is in VB, but I'm good with VB / C# solutions.

Thanks!!!


EDIT

As an addition to this question, I used the GetLastInputInfo from the User32.dll to check on the system activity.

Even with my mouse / keyboard events linked to the Timer_Tick event, GetLastInputInfo only gets reset if I physically move the mouse / perform some action on the computer...

I guess my question is What events can I add to my Timer_Tick event that will reset the GetLastInputInfo - In other words, have windows believe the user actually did something on the machine??

Thanks!!!

like image 334
John Bustos Avatar asked Aug 13 '13 16:08

John Bustos


2 Answers

After a lot of playing around, I came up with a solution that worked and I'm answering this question so as to help anyone out there that may be looking for the same information.

(Coded in VB):

In Declarations Section:

Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Integer, ByRef pInputs As INPUT_TYPE, ByVal cbSize As Integer) As Integer
Const INPUT_MOUSE = 0
Const MOUSEEVENTF_MOVE = &H1

Public Structure MOUSEINPUT
    Public dx As Integer
    Public dy As Integer
    Public mouseData As Integer
    Public dwFlags As Integer
    Public dwtime As Integer
    Public dwExtraInfo As Integer
End Structure

Public Structure INPUT_TYPE
    Public dwType As Integer
    Public xi As MOUSEINPUT
End Structure

The Sub to move the mouse:

Public Sub MoveMouse(ByVal x As Integer, ByVal y As Integer, ByVal flag As Integer)

    Dim inputEvents As INPUT_TYPE
    Dim xi As New MOUSEINPUT

    xi.dx = x
    xi.dy = y
    xi.mouseData = 0
    xi.dwtime = 0
    xi.dwFlags = flag
    xi.dwExtraInfo = 0

    inputEvents.dwType = INPUT_MOUSE
    inputEvents.xi = xi

    SendInput(1, inputEvents, Len(inputEvents))
End Sub

The Code in the Timer_Tick Event:

Private Sub Updater_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updater.Tick

    ' Move the mouse (relatively) 1 pixel away and then back
    MoveMouse(1, 1, MOUSEEVENTF_MOVE)
    MoveMouse(-1, -1, MOUSEEVENTF_MOVE)

End Sub

I tested this using the GetLastInputInfo API function and it works!!

... The most helpful link I found was this one: http://homeofcox-cs.blogspot.com/2008/07/c-simulate-mouse-and-keyboard-events.html

Thank you all for all your help and I hope this helps other people too!!

like image 66
John Bustos Avatar answered Sep 20 '22 02:09

John Bustos


when we needed the system not to go idle (in WM 6.0 when we ha long logic and the device went idle the logic stopped working) we used

  [DllImport("CoreDll.dll")]
  public static extern void SystemIdleTimerReset();

it reset the idle timer, not interfering with the user's actions and it worked like charm

like image 44
No Idea For Name Avatar answered Sep 20 '22 02:09

No Idea For Name