Determine Keystrokes or Mouse movement

Posted on 1999-01-19
Last Modified: 2008-02-20
I'm trying to write an app to determine system inactivity and am having trouble determining keyboard input on a system-wide basis. I think I'm on the right track by using the Win32API function MsgWaitForMultipleObjects.  The function has five parameters passed to it.

They are:
nCount - The number of handles in the list.
pHandles - The first element in an array of object handles
fWaitAll - True, wait until all objects signaled. False,
           wait until any one object is signaled.
dwMilliseconds - number of milliseconds to wait
dwWakeMask - constant indicating type of message whose arrival can cause the function to return. (QS_INPUT)

The last three parameters are fairly straightforward.  It's the first two I cannot determine.
I cannot get this function to work. Can anybody show me a code sample with the proper way to call this function, OR, can you provide me with a way to check for system-wide inactivity.

please help.
Question by:zantac
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
  • 5
  • 3

Accepted Solution

groone earned 100 total points
ID: 1469549
The example below shows how you can monitor if the printscreen key is pressed.  Naturally there are many more keys and you just need to declare them in the bas module according to there numeric value.  I went ahead and listed all the keycode constants for you.  Because of length I did not list all the key syncs....I'm sure you can figure it out. As you can see, you do not have to have a bunch of loops and such...just a timer and this code will monitor all key activity.

'Paste in a form
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Sub Form_Load()
     SetKeyboardState kbArray
     kbArray.kbByte(vk_snapshot) = 0
End Sub

Private Sub Timer1_Timer()
     If (GetAsyncKeyState(vk_snapshot)) < 0 Then Text1.Text = Text1 & "PrintScreen Pressed" & vbCrLf
     SetKeyboardState kbArray
     kbArray.kbByte(vk_snapshot) = 0
End Sub

'Paste in a Bas module
Option Explicit

Public Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Public Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Type KeyboardBytes
     kbByte(0 To 255) As Byte
End Type

Public kbArray As KeyboardBytes
Public Const vk_snapshot = &H2C
Public Const vk_alt = &H12
Public Const vk_backspace = &H8
Public Const vk_tab = &H9
Public Const vk_clear = &HC
Public Const vk_enter = &HD
Public Const vk_Shift = &H10
Public Const vk_ctrl = &H11
Public Const vk_pause = &H13
Public Const vk_capslock = &H14
Public Const vk_esc = &H1B
Public Const vk_space = &H20
Public Const vk_pgup = &H21
Public Const vk_pgdwn = &H22
Public Const vk_end = &H23
Public Const vk_home = &H24
Public Const vk_lftarrow = &H25
Public Const vk_Uparrow = &H26
Public Const vk_rghtarrow = &H27
Public Const vk_dwnarrow = &H28
Public Const vk_printscrn = &H2A
Public Const vk_insert = &H2D
Public Const vk_del = &H2E
Public Const vk_numlock = &H90

Public Const vk_A = 65
Public Const vk_B = 66
Public Const vk_C = 67
Public Const vk_D = 68
Public Const vk_E = 69
Public Const vk_F = 70
Public Const vk_G = 71
Public Const vk_H = 72
Public Const vk_I = 73
Public Const vk_J = 74
Public Const vk_K = 75
Public Const vk_L = 76
Public Const vk_M = 77
Public Const vk_N = 78
Public Const vk_O = 79
Public Const vk_P = 80
Public Const vk_Q = 81
Public Const vk_R = 82
Public Const vk_S = 83
Public Const vk_T = 84
Public Const vk_U = 85
Public Const vk_V = 86
Public Const vk_W = 87
Public Const vk_X = 88
Public Const vk_Y = 89
Public Const vk_Z = 90

Public Const vk_0 = 48
Public Const vk_1 = 49
Public Const vk_2 = 50
Public Const vk_3 = 51
Public Const vk_4 = 52
Public Const vk_5 = 53
Public Const vk_6 = 54
Public Const vk_7 = 55
Public Const vk_8 = 56
Public Const vk_9 = 57

Public Const vk_Numpad0 = &H60
Public Const vk_Numpad1 = &H61
Public Const vk_Numpad2 = &H62
Public Const vk_Numpad3 = &H63
Public Const vk_Numpad4 = &H64
Public Const vk_Numpad5 = &H65
Public Const vk_Numpad6 = &H66
Public Const vk_Numpad7 = &H67
Public Const vk_Numpad8 = &H68
Public Const vk_Numpad9 = &H69
Public Const vk_Multiply = &H6A '    MULTIPLICATION SIGN (*) key
Public Const vk_Add = &H6B   '   PLUS SIGN (+) key
Public Const vk_NumpadEnter = &H6C '    ENTER (keypad) key
Public Const vk_Subtract = &H6D  '    MINUS SIGN (-) key
Public Const vk_Decimal = &H6E     '   DECIMAL POINT(.) key
Public Const vk_Divide = &H6F '    DIVISION SIGN (/) key

Public Const vk_F1 = &H70 '    F1 key
Public Const vk_F2 = &H71 '    F2 key
Public Const vk_F3 = &H72 '    F3 key
Public Const vk_F4 = &H73 '    F4 key
Public Const vk_F5 = &H74 '    F5 key
Public Const vk_F6 = &H75 '    F6 key
Public Const vk_F7 = &H76 '    F7 key
Public Const vk_F8 = &H77 '    F8 key
Public Const vk_F9 = &H78 '    F9 key
Public Const vk_F10 = &H79  '    F10 key
Public Const vk_F11 = &H7A  '    F11 key
Public Const vk_F12 = &H7B  '    F12 key

Author Comment

ID: 1469550
I thank you for your answer; but, do you think that the solution you proposed would work on a system-wide basis and not just when my app is current?  I need to check for activity on all active treads.

Expert Comment

ID: 1469551
yes, this is for a system wide key check that is why i presented it to you.  Try it out.
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.


Expert Comment

ID: 1469552
put your timer interval to 1

Expert Comment

ID: 1469553
for clarity...when you say system are you implying a single computer system, LAN, or WAN?  This is only for one single computer.  If you are referring to a intranet and monitoring work stations then this code will not work.  The code for that is very long and tedious.

Author Comment

ID: 1469554
It's for a single computer system.  The code seems to work fine.  Thank you for the extended example you presented to me.

Expert Comment

ID: 1469555
Do you need mouse info too?  I just found a good snippet of code for detecting mouse info.  Open another question and it's yours ;)

Author Comment

ID: 1469556
No Thanks, I can determine mouse movement by comparing positions (GetCursorPos). Thanks Anyway.

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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
Course of the Month10 days, 1 hour left to enroll

623 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