Solved

Application to move mouse

Posted on 2010-08-24
7
629 Views
Last Modified: 2012-06-22
I have a need to write an application in vb.net to move the mouse cursor every 4 minutes. The mouse movement can be minimal, but enough to keep screen saver from coming on. Does anyone have code to do this?
0
Comment
Question by:OB1Canobie
7 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33512191
      Windows.Forms.Cursor.Position = New System.Drawing.Point(Windows.Forms.Cursor.Position.X + 1, Windows.Forms.Cursor.Position.Y + 1)


0
 
LVL 14

Expert Comment

by:systan
ID: 33512239
Disable the screen saver instead;
Imports Microsoft.Win32





'// This goes on form_load



'// Reads and sets the registry to disable screensaver from kicking

Dim regKey As RegistryKey

Dim regVal As String

regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)

regVal = regKey.GetValue("ScreenSaveActive")

If regVal = 1 Then

regKey.SetValue("ScreenSaveActive", "0")

End If

regKey.Close()





'//This goes on app exit



'// Reads and sets the registry to re-enable screensaver

Dim regKey As RegistryKey

Dim regVal As String

regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)

regVal = regKey.GetValue("ScreenSaveActive")

If regVal = 0 Then

regKey.SetValue("ScreenSaveActive", "1")

End If

regKey.Close()





OR Do like this:

Microsoft.Win32.RegistryKey rkScreenSaver = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"  Control Panel\Desktop", true );



if ( (string)rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )

{

rkScreenSaver.SetValue( "ScreenSaveActive", "0" );

rkScreenSaver.Close( );

}

Open in new window

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 33512495
You don't need to move the mouse just inform the operating system your application is doing work with the display and it will reset the idle timer. You can use SetThreadExecutionState() with ES_CONTINUOUS | ES_DISPLAY_REQUIRED flags.
For example if your watching a DVD on your computer the application would call the above method, if they didn't the screen saver would interrupt the movie.
 

Imports System.Runtime.InteropServices



Public Class Form1

    Private Const ES_CONTINUOUS As Integer = &H80000000

    Private Const ES_DISPLAY_REQUIRED As Integer = &H2

    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _

    Private Shared Function SetThreadExecutionState(ByVal esFlags As Integer) As Integer

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SetThreadExecutionState(ES_CONTINUOUS Or ES_DISPLAY_REQUIRED)

    End Sub

End Class

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33512608
In the example above which I never mentioned you would periodically call SetThreadExecutionState() once every minute is fine. When your finished and you want it to go back to normal use SetThreadExecutionState(ES_CONTINUOUS).
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33512638
Thanks for posting that one egl1044.  Learned something new today!  =D
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33521796
No problem Idle_Mind it's very useful for background processing.  I would like to also point out is that I have always called this routine periodically but it's appears only neccessary if you use only ES_CONTINUOUS flag.
If you use both flags ES_CONTINUOUS | ES_DISPLAY_REQUIRED it locks the Idle counter all together until you remove the ES_DISPLAY_REQUIRED bit flag. That means with the two flags you only need call it once but periodically if using only ES_CONTINUOUS.
When I started using this the documentation wasn't the same from a couple years ago but they seem to have improved it over time with much better descriptions.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33521944
Whoops the above should read
If you use SetThreadExecutionState without* ES_CONTINUOUS flag.
That means with the two flags you only need call it once but periodically if using SetThreadExecutionState without* ES_CONTINUOUS flag.
<Fills up more coffee>...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now