Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to detect the ideal time of the machine under a VB6 program

Posted on 1999-07-13
Medium Priority
Last Modified: 2006-11-17
I am using VB6 to develop a system. If possible, I want to add a timeout function such like a screen saver. If the user did not press any key or move the mouse for a certain period of time after logged in the system, then he will be AUTOMATICALLY logged out. (My system is a one with multiple MDI child form)
Question by:chrischris
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3

Expert Comment

ID: 1524502
 The idea is simple, run a kind of daemon that checks the state of the keyboard and the mouse at a given interval. This can be implemented using a Timer control, but for it to work fine the interval property should be set at 300 more or less.

Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer

Sub Timer1_Timer()
  Dim lEscape As Long, lEnter As Long, lSpace As Long
  Dim lLButton As Long, lMButton As Long, lRButton As Long


  If lEnter<>0 Then
    'everything OK
    'Enter key pressed
  End If

  If lEscape<>0 Then
    'everything OK
    'Escape key pressed
  End If

  'and so on for every keyboard you may want

  'now the mouse

End Sub

  To check the mouse:


  With these three constants you have the mouse's buttons state; and I think there are some 'more important keys' in the keyboard like Enter, Tab, BackSpace, Escape, Space, etc; that if you detect that they are not pressed it's high probable that nobody is using the keyboard.

  You can get all the constants named above from the API viewer, check all that begin like VK_????.

  Good luck!

Expert Comment

ID: 1524503
 After re-reading your question have a doubt, you want:

1) that after, let's say 2 minutes, the user has logged into the system and doesn't use the keyboard nor the mouse, logged him out, or;

2) to check ALL THE TIME the user is logged, to detect if he stops using the keyboard or the mouse?

  If your answer is 2) my comment will apply, if is 2) then you need a second Timer control to carry on the timeout.

  Timer1: (I've said 2 minutes) 120000
  Timer2: 300 and post the code above.


Author Comment

ID: 1524504
Thanks, Maquiavelo. Do you mean that I have to set two timers where one for updating the KeyPress information at 300ms and the other for say 10minutes to detect the last time-stamp?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1524505
Sorry, I reply so late. The meaning of my question is your first case.
LVL 10

Expert Comment

ID: 1524506
There is a much easier way, which doesn't require any API calls.

Declare a global variable of type Date.

In every relevant control event (e.g. Click for buttons, Change for text boxes and so on) the first thing you do is set this variable to Now

Then you have one timer with the maximum delay which is about 60000 (1 minute). In the _Timer procedure, you compare Now with the value of the global variable, and if the difference is more than your required amount of minutes, log the user out.

Author Comment

ID: 1524507
Sorry! I totally agreed with your dummy idea. But, it's quite impossible for me to add a procedure into thousands of controls (about 50 controls in over 60 forms). Moreover, it's a huge job to do the maintenance if this method is being used.
LVL 10

Expert Comment

ID: 1524508
Fair enough!

Expert Comment

ID: 1524509
Can you not use caraf_g's idea but on a form level with the MouseMove form event and the keypress for event (you need to set the form's keypreview property to true.)

LVL 10

Expert Comment

ID: 1524510
:) Not a bad idea, simonbennett!

Accepted Solution

Smout earned 600 total points
ID: 1524511

Author Comment

ID: 1524512

I briefly tested the component and it seems Okay. But, could you tell me how much resources that it will take up from the system? Could you also tell me the home page of its producer so that I can collect more information about it?

Anyway, thanks.

Expert Comment

ID: 8943354
The link to your accepted aswer on this is dead.
Can you tell me how you solved your problem?
I'm trying to write a VB app that can tell if another app (not VB)
has had mouse or keyboard activity in a certain time period.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

721 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