Link to home
Start Free TrialLog in
Avatar of nozzle99
nozzle99

asked on

Detect No Mouse Move & No Keyboard Pressed

I'm currently making program which run when windows start and stay in the background. This program will act as a "screen saver". It'll run another program if users don't move their mouse or press any key for certain amount of time. The problem is: how can I detect that the mouse isn't moving and the keyboard isn't pressed at X time (my program is inactive but still run at background) ??? I need to see working program or component.
Avatar of fulvio_brasil
fulvio_brasil

why you can't use the screen saver mechanism ?
Avatar of nozzle99

ASKER

I don't use screen saver because I don't want this program to run when user hasn't login yet.
Hi you can do something like this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
   procedure AppMessage(var Msg: TMsg; var Handled: Boolean);

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
if (Msg.Message=WM_KEYDOWN) or //if key was pressed
   (Msg.message=WM_MOUSEMOVE) then //or if mouse was moved
    begin
     Timer1.enabled:=False;
     Timer1.enabled := true;
      Handled := True;
    end;
  end;


procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Interval := 2000;
Application.OnMessage := AppMessage;
Timer1.enabled := true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
showmessage('no activity for 2 seconds.');
end;

end.
I don't use screen saver because I don't want this program to run when user hasn't login yet.
Sorry inthe, but your code only works when the mouse is on the top of the form. My program doesn't use any forms at all (I hide them).
Hi nozzle99,

Windows traces users activity, if the system is idle for the screen saver interval launches a message, this message is sent to the topmost window continuously every period of idleness even if the screen saver is running.

the wm_SysCommand message with sc_ScreenSave as lParam is sent to the topmost window when inactivity times out. by returning zero as Result you tell the system, that a screensaver (your program in this case) is currently running and not to start another one. so you can do your own "screen saving" or whatever you want. Obviously this works only when Form1 is the active window.

procedure wmSysCommand(var Msg:TwmSysCommand); message wm_SysCommand;

procedure TForm1.wmSysCommand(var Msg:TwmSysCommand);
begin
  if Msg.CmdType=sc_ScreenSave then begin
    {do your stuff}
    Msg.Result:=0; //handled so ignore (do not start the real screensaver or another instance)
  end;
end;
by the way u can adjust the timeout interval with the WinAPI function:
SystemParametersInfo with the SPI_SETSCREENSAVETIMEOUT parameter.
To detect whether the mouse moves ANYWHERE (regardless of whether it's over your form, your form is deactivated or whatever) add a timer and check the Mouse.CursorPos value. Don't know about the keyboard but that'll solve the mouse side of things for you.

The Neil
Thanks for your comment Gurkan, but I don't want to make my program become active every time.

Perhaps I should modify the question.

- How can I detect that screen saver is running? My program (not the screen saver) is running but not active.

- Let say that my program is the screen saver; so when the screen saver is active and the user hasn't login yet, I tell my program to terminate. How can the program know that the user hasn't login (using win98 or winnt) yet?

If you can give solution for one of all questions above (include the real one), you'll get the point.
To detect if a screen saver is running or not, do this

VAR
  bRunning : BOOLEAN;
BEGIN
  SystemParametersInfo(SPI_GETSCREENSAVEACTIVE,
                          0,
                          @bRunning,
                          0);
END;

The Neil
>> How can I detect that screen saver is running?

Sorry, TheNeil, your code only checks whether the screensaver is ENABLED, not whether it is running...   :-(

type TPBoolean = ^boolean;

function FindScreenSaverFunc(window: cardinal; found: TPBoolean) : bool; stdcall;
var s1 : string;
begin
  SetLength(s1,MAX_PATH+1);
  SetLength(s1,GetClassName(window,pchar(s1),MAX_PATH));
  found^:=s1='WindowsScreenSaverClass';
  result:=not found^;
end;

function IsScreenSaverRunning : boolean;
var c1 : cardinal;
begin
  if OS.winNt then begin
    result:=false;
    c1:=OpenDesktop('Screen-saver',0,false,MAXIMUM_ALLOWED);
    if c1<>0 then
      try
        EnumDesktopWindows(c1,@FindScreenSaverFunc,integer(@result));
      finally CloseDesktop(c1) end;
  end else result:=windows.FindWindow('WindowsScreenSaverClass',nil)<>0;
end;

Regards, Madshi.
Hi again nozzle99,
First, I should say TheNeil's answer gives whether a screen saver is set or not, not gives whether itis running or not.

I found one solution but it is a bit long. U should first write a screen saver (u know they are executables only their extensions are scr) then set that ssaver as the current ssaver. Then when the windows understand that it is idle your dummy ssaver will be activated and then u can do what u want.
I donot know your programming level but If u need more details just drop a message.

Hi again nozzle99,
First, I should say TheNeil's answer gives whether a screen saver is set or not, not gives whether itis running or not.

I found one solution but it is a bit long. U should first write a screen saver (u know they are executables only their extensions are scr) then set that ssaver as the current ssaver. Then when the windows understand that it is idle your dummy ssaver will be activated and then u can do what u want.
I donot know your programming level but If u need more details just drop a message.

Hi Madshi, it seems that your solution work on WinNT machine after I change the "OS" thing, but I don't know why it doesn't work on win95 machine.

And for Gurkan, thanks for your solution. I also have similiar idea with yours. I use dummy ssaver which do nothing and a program which run since user login and catch the ssaver event.
Well, it depends on the screen saver. Normally the screen saver main window should have the class name 'WindowsScreenSaverClass'. If it has, my function works, otherwise not. It works at least for the standard screen savers.
ASKER CERTIFIED SOLUTION
Avatar of ZifNab
ZifNab

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nozzle99 : Just one small point.  If the user has not logged in, your program will not be running. :-)

John.
Hi ZifNab, could you show me how to use that library, cause I haven't use any library before? Thanks