Solved

Detect No Mouse Move & No Keyboard Pressed

Posted on 2000-02-24
18
414 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:nozzle99
  • 6
  • 4
  • 2
  • +5
18 Comments
 
LVL 1

Expert Comment

by:fulvio_brasil
Comment Utility
why you can't use the screen saver mechanism ?
0
 

Author Comment

by:nozzle99
Comment Utility
I don't use screen saver because I don't want this program to run when user hasn't login yet.
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
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.
0
 

Author Comment

by:nozzle99
Comment Utility
I don't use screen saver because I don't want this program to run when user hasn't login yet.
0
 

Author Comment

by:nozzle99
Comment Utility
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).
0
 

Expert Comment

by:Gurkan
Comment Utility
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;
0
 

Expert Comment

by:Gurkan
Comment Utility
by the way u can adjust the timeout interval with the WinAPI function:
SystemParametersInfo with the SPI_SETSCREENSAVETIMEOUT parameter.
0
 
LVL 5

Expert Comment

by:TheNeil
Comment Utility
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
0
 

Author Comment

by:nozzle99
Comment Utility
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:TheNeil
Comment Utility
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
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
>> 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.
0
 

Expert Comment

by:Gurkan
Comment Utility
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.

0
 

Expert Comment

by:Gurkan
Comment Utility
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.

0
 

Author Comment

by:nozzle99
Comment Utility
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.
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 200 total points
Comment Utility
I think you have to work with a keyboard/mouse hook.

here is some explenation and source code :

http://www.preview.org/q/q2108.shtml

same goes for mouse trapping

Regards, Zif.

0
 
LVL 6

Expert Comment

by:Jaymol
Comment Utility
Nozzle99 : Just one small point.  If the user has not logged in, your program will not be running. :-)

John.
0
 

Author Comment

by:nozzle99
Comment Utility
Hi ZifNab, could you show me how to use that library, cause I haven't use any library before? Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now