Jump to content
Sign in to follow this  

Detect keyboard use . . .A discussion of how it's done

Recommended Posts


I need a way to tell if a user is active so that a database is updated with their last action time

I am thinking that the keyboard or mouse use could help with this.

maybe MouseGetPos() then time till it changes. If more than 60 seconds unchanged = N

Then keyboard use if more than 60 seconds = N

If keyboard = N and mouse = N then active = False

But false would have to be the most recient time of activity for the mouse or keyboard (whichever is more recient)

True would also me the most recient, but since it would be sooner, it would fit within the 60 second thing and could be considered active when added to the database.

Can anyone help me with a demo of any part of this? Even how to store time since keyboard or mouse movement?

Edited by Hatcheda

Share this post

Link to post
Share on other sites

Ok, I've worked out the mouse part, can anyone help me with the keyboard?

In the example below the edit box will update when the mouse is still. It will not update again until you move the mouse and then the mouse is still again.

#include <GUIConstants.au3>

#Include <Date.au3>

Opt("GuiOnEventMode", 1)

Global $Mouse1, $Mouse2, $hParent, $Edit1, $Time

$hParent = GUICreate("Time Mouse Movement", 716, 561, 155, 134)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit", $hParent)

$Edit1 = GUICtrlCreateEdit("Increase count when mouse not active for X time", 10, 30, 230, 60, $ES_WANTRETURN)


$n = 0 ; Counter

While 1

_CheckMouse1() ; Goto get mouse pos first time


$n = $n + 1 ; Adds 1 to the counter every two seconds

_CheckMouse2() ; Goto func Get mouse pos second time


Func _CheckMouse1()

$pos1 = MouseGetPos()

$Mouse1 = $pos1[0] + $pos1[1]

EndFunc ;==>_CheckMouse1

Func _CheckMouse2()

$pos2 = MouseGetPos()

$Mouse2 = $pos2[0] + $pos2[1]

If $Mouse1 <> $Mouse2 Then

$n = 0

ElseIf $Mouse1 = $Mouse2 And $n = 2 Then



EndFunc ;==>_CheckMouse2

Func _Quit()


EndFunc ;==>_Quit

Func _UserNotActive()

GUICtrlSetData($Edit1, _NowTime (5))

EndFunc ;==>_UserNotActive

_NowTime (5)

Share this post

Link to post
Share on other sites

i would say use _ispressed to see if the keyboard is active. If you want an already typed list of _ispressed and the keys just pm and ill send it to you.

Share this post

Link to post
Share on other sites

I didn't come up with this, but its pretty clever,

Returns 1 if any key is pressed, otherwise return 0

Func _IsAnyKeyPressed($start = 0, $end = 255)
    For $i = $start to $end
        if _IsPressed(Hex($i,2)) Then return 1
        Return 0

Share this post

Link to post
Share on other sites

I just wrote a simple UDF for checking mouse movement earlier today:

; Description:      Check if the mouse is moving.
; Parameter(s):     None
; Requirement(s):   A mouse to check, obviously
; Return Value(s):  If mouse is moving: return True
;                   If mouse is not moving: return False
; Author(s):        Me
; Note(s):          None
Func _MouseIsMoving()
    $pos = MouseGetPos()
    $pos2 = MouseGetPos()
    If $pos[0] <> $pos2[0] Or $pos[1] <> $pos2[1] Then
        Return True
        Return False

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.