Jim Lawless' Blog


Preventing Windows Screen-Saver Activation

Originally published on: Tue, 20 Oct 2009 00:54:54 +0000

Some time ago, I wanted to see if the simulated mouse moves generated via calls to the WIN32 API mouse_event function would prevent the screen-saver from triggering.

In the current version of XP ( and some older versions of Windows ) these events do indeed keep the screen-saver from activating.

Please refer to the source below.

busy_mouse.c


// Generate mouse events to prevent the screen-saver from
// activating.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

// check the state of the Scroll Lock or Caps Lock keys. Return a
// 1 if the requested key is active/on.

#include <windows.h>
#include <stdio.h>
#include <string.h>

#pragma comment(lib,"user32.lib")

void process_messages(void);

int main(int argc,char **argv) {
   int i;
   printf("Busy Mouse v1.0 by Jim Lawlessn");
   printf("If CAPS LOCK is on, the mouse cursor will move around to prevent any screen-savers from activating.n");
   for(;;) {
      while(GetKeyState(VK_CAPITAL)&1) {
         for(i=0;i<50;i+=2) {
            mouse_event( MOUSEEVENTF_MOVE,2,0,0,0 );
            process_messages();
         }
         Sleep(200);
      
         for(i=0;i<50;i+=2) {
            mouse_event( MOUSEEVENTF_MOVE,0,2,0,0 );
            process_messages();
         }
         Sleep(200);
      
         for(i=0;i<50;i+=2) {
            mouse_event( MOUSEEVENTF_MOVE,-2,0,0,0 );
            process_messages();
         }
         Sleep(200);
      
         for(i=0;i<50;i+=2) {
            mouse_event( MOUSEEVENTF_MOVE,0,-2,0,0 );
            process_messages();
         }
         Sleep(200);
      }
   }
   processMessages();
}

void process_messages(void) {
MSG m;
while( PeekMessage(&m,0,0,0,PM_REMOVE)) {
    TranslateMessage(&m);
DispatchMessage(&m);
}
}

When you run busy_mouse.exe the program will loop indefinitely. The program checks the CAPS LOCK state. If CAPS LOCK is enabled, it will then move the mouse in a rectangular pattern via mouse_event().

When CAPS LOCK is off, the simulated mouse events halt.

I've used this to keep the screen visible so that I can watch for emails or processes that run long while I'm away from the mouse and keyboard.

The source and executable file for busy_mouse can be downloaded in a single archive at: http://www.mailsend-online.com/wp/busy_mouse.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: Thwarting HTTP Referer Trackbacks
Next post:A TCP Command Line Interface in Rhino JavaScript


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Locking a Windows Session

An Embedded Mini-Interpreter

Obfuscated Perl

A Simple Media Control Interface Script Processor

Hiding Batch File Console Windows

Auto Save Images from the Clipboard

A Lightweight Alternative to Windows Shortcuts

A DSL in JavaScript

Pi Day Meets the HTML5 Canvas

A Simple Associative Array Library in C


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer