Solved

Detect No Mouse Move & No Keyboard Pressed

Posted on 2000-02-24
18
419 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
ID: 2556555
why you can't use the screen saver mechanism ?
0
 

Author Comment

by:nozzle99
ID: 2556616
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
ID: 2556722
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:nozzle99
ID: 2556729
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
ID: 2556754
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
ID: 2556873
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
ID: 2556885
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
ID: 2557025
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
ID: 2557066
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2557099
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
ID: 2557128
>> 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
ID: 2557140
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
ID: 2557155
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
ID: 2557359
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
ID: 2557366
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
ID: 2557486
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
ID: 2557648
Nozzle99 : Just one small point.  If the user has not logged in, your program will not be running. :-)

John.
0
 

Author Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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