Solved

Application to move mouse

Posted on 2010-08-24
7
642 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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