🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.nativeapi.win32
4 messages
4 total messages Started by A Nixon Thu, 17 Jul 2008 22:36
Application close on timeout
#114
Author: A Nixon
Date: Thu, 17 Jul 2008 22:36
11 lines
1132 bytes
Hi all,

Is there a way to detect if there have been no user events generated in an 
application (on a global level) for a period of time and close it if none 
were detected.

Trying to stop people walking off leaving the application opened.

regards

Tony 
Re: Application close on timeout
#115
Author: David Cornelius
Date: Thu, 17 Jul 2008 23:19
72 lines
3040 bytes
> Is there a way to detect if there have been no user events generated in an 
> application (on a global level) for a period of time and close it if none 
> were detected.
>
> Trying to stop people walking off leaving the application opened.


One way is hook into a couple of Windows functions that trap the keyboard 
and mouse input.  But first you need to create a couple of variables to hold 
the current value of the Windows hooks:

var
  KeyboardHook, MouseHook: HHook;


function TrapKeyboardInput(nCode: Integer; wp: wParam; lp: lParam): LResult; 
stdcall;
begin
  if nCode < 0 then
    Result := CallNextHookEx(KeyBoardHook, nCode, wp, lp)
  else begin
    Result := 0;
    if nCode = HC_ACTION then
      IdleTime := 0;
  end;
end;

function TrapMouseInput(nCode: Integer; wp: wParam; lp: lParam): LResult; 
stdcall;
begin
  if nCode < 0 then
    Result := CallNextHookEx(MouseHook, nCode, wp, lp)
  else begin
    if nCode = HC_ACTION then begin
      case wp of
        WM_LBUTTONDOWN,
        WM_RBUTTONDOWN,
        WM_MBUTTONDOWN,
        WM_LBUTTONDBLCLK,
        WM_RBUTTONDBLCLK,
        WM_MBUTTONDBLCLK:
          IdleTime := 0;
      end;
    end;
    Result := 0;
  end;
end;

Then, in your application's OnCreate event, hook into the Windows functions:

  MouseHook := SetWindowsHookEx(WH_MOUSE, TrapMouseInput, 0, 
GetCurrentThreadID);
  KeyboardHook := SetWindowsHookEx(WH_KEYBOARD, TrapKeyboardInput, 0, 
GetCurrentThreadID);

And start a timer.  Of course, your timer's OnTimer even can do anything it 
needs to shut your application down, like save and close datasets, write a 
log file event, etc.


Be sure to free the Windows hooks in the OnDestroy event:

  UnhookWindowsHookEx(MouseHook);
  UnHookWindowsHookEx(KeyboardHook);



Hope this helps!

-- 
David Cornelius
CorneliusConcepts.com 
Re: Application close on timeout
#116
Author: Remy Lebeau (TeamB)
Date: Fri, 18 Jul 2008 01:49
12 lines
1249 bytes
"A Nixon" <pac@pac.asn.au> wrote in message 
news:487fc97d@newsgroups.borland.com...

> Is there a way to detect if there have been no user events generated
> in an application (on a global level) for a period of time and close
> it if none were detected.

Look at the Win32 API GetLastInputInfo() function.


Gambit
Re: Application close on timeout
#120
Author: Peter Below (TeamB)
Date: Fri, 18 Jul 2008 17:49
55 lines
2460 bytes
A Nixon wrote:

> Hi all,
> 
> Is there a way to detect if there have been no user events generated
> in an application (on a global level) for a period of time and close
> it if none were detected.
> 
> Trying to stop people walking off leaving the application opened.
> 

Making an app close automatically after a certain period of inactivity

> the users will start the app and then just minimize it during the day.
> I would like to have a way of shutting the application down after so
many
> minutes of idle time.

You need three pieces of equipment here:

 a) a "time of last activity" variable, field of your main form
 
 FLastActive: TDateTime;
 
 b) a timer that regularly checks the FLastActive variable against the 
    current time. Set it to an interval of, say 60000, and set its
Active
    property to true at design-time. The OnTimer event handler would be 
    something like this (timeout after 15 minutes):
    
   If (FLastActive + EncodeTime( 0, 15, 0, 0 )) < Now Then
     Close;
     
 c) a handler for the Application.OnMessage event that updates the 
    FLastActive variable on each key or mouse message. The handler would
    do something like this:
    
    Case msg.Message of
      WM_KEYFIRST..WM_KEYLAST, WM_MOUSEFIRST..WM_MOUSELAST:
         if msg.Message <> WM_MOUSEMOVE then
           FLastActive := Now;
    End;    

The OS will send mouse move messages to the active process at intervals
even if the mouse does not move, that's why that  message is filtered
out above.
    
That's it.    

-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads