Sign in to follow this  
Followers 0
mahaju

making key press detector and timer in autoit

10 posts in this topic

#1 ·  Posted (edited)

HI I am new to autoit and this is my first script

Is there any finction that detects when a key has been pressed on the keyboard?

_KeyPressed() seems to check if some particular key was pressed, but I want something that gives a signal is any ley is pressed on the keyboard

I also need to make a timer, which counts the number of seconds elapsed

I want to be able to display the number of seconds elapsed in a window and reset the timer to 0 is any key is pressed on the keyboard (but should ignore mouse buttons or movement)

So far I have found the _KeyPressed() finction and the _timer functions, but they don't quite do exactly what I need

Please give me some ideas on how I should go about this

Thank you

Edited by mahaju

Share this post


Link to post
Share on other sites



Interesting idea. What's it going to be used for?


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites

What you are asking for is basically a keystroke logger. Please understand the topic of keyloggers is banned in the forum.

Now, I take it you want to do something that works on the principle of a screensaver but in your case it just resets the timer. I found a link to some code (Not AutoIt) but it should be able to give you some direction.

http://www.codeproject.com/Articles/37666/Calculate-system-idle-time-without-hooks

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Use

#include
_InputEvent_StartUp()

GUICreate("Key input captured here")
GUISetState(@SW_SHOW)

While True
Wend

Func _InputEvent_KeyUp($handle,$keyname,$keyvalue,$shift,$ctrl,$alt,$ticks)
    if $keyname = "ESC" Then
        Exit
    endif

    ToolTip(StringFormat("You pressed the %s [%s] key",$keyname,$keyvalue))
EndFunc
Edited by MouseSpotter

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks for the replies

Interesting idea. What's it going to be used for?

I want the computer to go to sleep if the keyboard is inactive

But I want to be able to use the mouse during this time though, which does not seem to be possible through Windows, as it keeps track of keyboard as well as mouse before making the computer go into sleep or hibernation mode or running any other program for that matter

Edited by mahaju

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

What you are asking for is basically a keystroke logger. Please understand the topic of keyloggers is banned in the forum.

Now, I take it you want to do something that works on the principle of a screensaver but in your case it just resets the timer. I found a link to some code (Not AutoIt) but it should be able to give you some direction.

http://www.codeproject.com/Articles/37666/Calculate-system-idle-time-without-hooks

By the way your post reminded me of something

I once had seen a screensaver that could take input from the mouse, without exiting

Perhaps it's logic is similar to what I need to do

However that screensaver started if both keyboard and Mouse were idle

In my case I just want to keep track of keyboard idle time and ignore the mouse movements to determine if the timer should be reset

I don't need it to work in any kind of stealth mode, and in fact, I need a window to be visible so that I can keep track of the current timer value

Edited by mahaju

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Ok I have achieved, somewhat, what I wanted to do

the code is very crude but I want to post it here so that some experienced users may provide some other ideas or suggestions for code improvement

#include
#include
#include

local $upper_limit, $counter, $hWnd, $i, $hDLL, $labelwidth, $labelheight, $label1_id, $label2_id
$hDLL = DllOpen("user32.dll");
$upper_limit = 5400
$counter = 0
$iElapse = 1000
$labelwidth = 100
$labelheight = 25
$hWnd = GUICreate("Show Counter")
GUISetState(@SW_SHOW)
$label1_id = GUICtrlCreateLabel($counter, -1, -1, $labelwidth, $labelheight)
$label2_id = GUICtrlCreateLabel($upper_limit, 50, 50, 100, $labelheight)
GUICtrlSetFont ($label1_id, 20, 600)
GUICtrlSetFont ($label2_id, 20, 600)
do
$i = 1
while $i <= 40
sleep(25);
if _IsPressed("20", $hDLL) OR _IsPressed("41", $hDLL) OR _IsPressed("45", $hDLL) OR _IsPressed("49", $hDLL) OR _IsPressed("4F", $hDLL) OR _IsPressed("55", $hDLL) OR _IsPressed("20", $hDLL) OR _IsPressed("53", $hDLL) then
$counter = 0;
exitloop
endif
$i = $i + 1
if GUIGetMsg() = $GUI_EVENT_CLOSE then
exit
endif
wend
GUICtrlSetData($label1_id, $counter)
$counter = $counter + 1
until $counter > $upper_limit
DllClose ( $hDLL )
Run(@SystemDir & "\shutdown.exe /f /s /t 10", "", @SW_MAXIMIZE)
exit

It shuts down the computer after 1.5 hours of keyboard inactivity, and any user input from the mouse during this time will not affect it

The thing I like most about this is that it detects input even when the script's window is not in focus

How does AutoIt do that anyway?

What would be an easier way to determine if any key on the keyboard had been used at all, without having to download anything else?

Anyway this solves what I need for now

PS: and how do I make the #include's show up between the tags here?

Edited by mahaju

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

how about not logging whatever it was that we detected?

that wouldn't technically be a keylogger would it?

Anyway, isn't there any function at all that let's you find out just if the keyboard has been used, instead of only those checking if some particular keys have been pressed?

Edited by mahaju

Share this post


Link to post
Share on other sites

mahaju,

how about not logging whatever it was that we detected?

that wouldn't technically be a keylogger would it?

Please read the announcement I posted about keyboard watching scripts. It does not matter if you do not log the return - merely checking for the keys is not allowed as it is trivial to log if the code to detect is provided. It is actually pretty trivial to detect keypresses as well - but as the site owner does not want such matters discussed on the forum that is an end to it. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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  
Followers 0