Sign in to follow this  
Followers 0
primer

Recording & Outputting Key Presses

14 posts in this topic

this isnt for anything malicious or anything but is it possible to have an au3 script record how long i hold down which key and when, and then output it. Basically, i want it to simulate my exact key patterns and then duplicate them. it would have to be in the background while recording also, because i need to have focus on another window. is that possible?

Share this post


Link to post
Share on other sites



What it is going to be used for? :P

Do be aware that key loggers of any type are frowned upon and will you not get much help.


[font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font]

Share this post


Link to post
Share on other sites

for my personal use to automate a video game :P

i could use a stop-watch to record how long i pres each key and then code it from there, but this way seems much easier and more precise

Share this post


Link to post
Share on other sites

ok, this is what im trying to do. i only need to record these four keys

HotKeySet("{W}","wfunc()")
HotKeySet("{S}","sfunc()")
HotKeySet("{A}","afunc()")
HotKeySet("{D}","dfunc()")

Func wfunc()
    $begin = TimerInit()
    While _IsPressed("{w}","user32.dll")
        $dif = TimerDiff($begin)
    WEnd
    MsgBox(0, "W Key: " + $dif/1000 + " seconds.", "W")
EndFunc

Func sfunc()
    $begin = TimerInit()
    While _IsPressed("{s}","user32.dll")
        $dif = TimerDiff($begin)
    WEnd
    MsgBox(0, "S Key: " + $dif/1000 + " seconds.", "S")
EndFunc

Func afunc()
    $begin = TimerInit()
    While _IsPressed("{a}","user32.dll")
        $dif = TimerDiff($begin)
    WEnd
    MsgBox(0, "A Key: " + $dif/1000 + " seconds.", "A")
EndFunc

Func dfunc()
    $begin = TimerInit()
    While _IsPressed("{d}","user32.dll")
        $dif = TimerDiff($begin)
    WEnd
    MsgBox(0, "D Key: " + $dif/1000 + " seconds.", "D")
EndFunc

but it doesnt work

Share this post


Link to post
Share on other sites

What you're doing wrong is quite simple, however, this script could easily be turned into a keylogger by changing just a few things.

Without any permission from the administration I will refrain from contributing to your project, be it a keylogger or not.

I hope you understand this, as your post count does not add any credibility to your position.


Infinity is a floorless room without walls or ceiling.Anyone who cannot cope with mathematics is not fully human. At best he is a tolerable subhuman who has learned to wear shoes, bathe, and not make messes in the house.

Share this post


Link to post
Share on other sites

i absolutely understand, but how much could I possibly record from 4 keys?

also, i run into another problem. how would I record the time in between the key presses, when no key is pressed. i look more at the _IsPressed function and realized im doing it wrong. came up with this:

HotKeySet("{W down}","wfunc()")
HotKeySet("{S down}","sfunc()")
HotKeySet("{A down}","afunc()")
HotKeySet("{D down}","dfunc()")

HotKeySet("{W up}","wfunc2()")
HotKeySet("{S up}","sfunc2()")
HotKeySet("{A up}","afunc2()")
HotKeySet("{D up}","dfunc2()")

Func wfunc()
    $wbegin = TimerInit()
EndFunc

Func wfunc2()
    $wdif = TimerDiff($wbegin)
    MsgBox(0, "W Key: " + $wdif/1000 + " seconds.", "W")
EndFunc

Func sfunc()
    $sbegin = TimerInit()
EndFunc

Func sfunc2()
    $sdif = TimerDiff($sbegin)
    MsgBox(0, "S Key: " + $sdif/1000 + " seconds.", "S")
EndFunc

Func afunc()
    $abegin = TimerInit()
EndFunc

Func afunc2()
    $adif = TimerDiff($abegin)
    MsgBox(0, "A Key: " + $adif/1000 + " seconds.", "A")
EndFunc

Func dfunc()
    $dbegin = TimerInit()
EndFunc

Func dfunc2()
    $ddif = TimerDiff($dbegin)
    MsgBox(0, "D Key: " + $ddif/1000 + " seconds.", "D")
EndFunc

Share this post


Link to post
Share on other sites

not sure what i can do to convince you its not a keylogger. A keylogger seems like it would be more feasible because I wouldn't have to worry about timing and how long W,A,S or D are held down.

Share this post


Link to post
Share on other sites

Oh my... I don't have time to point every error in the code your showed, but here are something to look at :

Wrong : HotKeySet("{W down}","wfunc()")

Correct : HotKeySet("w","wfunc")

Wrong : MsgBox(0, "W Key: " + $wdif/1000 + " seconds.", "W")

Correct : MsgBox(0, "W Key: " & $wdif/1000 & " seconds.", "W")

Notes :

- Study the helpfile, it's comments and examples while keeping both eyes open. Don't guess.

- Try to learn the basics of the language before asking questions in a forum.

That's all I have time for...

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Wrong : HotKeySet("{W down}","wfunc()")

Correct : HotKeySet("w","wfunc")

not according to the documentation

HotKeySet ( "key" [, "function"] )

Key - The key(s) to use as the hotkey. Same format as Send().

If you mean the function calls () then yea I goofed up on that.

I understand my syntax knowledge is shaky with this language, but thats not what I'm asking for help with. I can read the documentation for that. I do not understand the... logic, i guess, to accomplishing this task in AutoIt

Edited by primer

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

not according to the documentation

HotKeySet ( "key" [, "function"] )

Key - The key(s) to use as the hotkey. Same format as Send().

If you mean the function calls () then yea I goofed up on that.

I understand my syntax knowledge is shaky with this language, but thats not what I'm asking for help with. I can read the documentation for that. I do not understand the... logic, i guess to accomplishing this task in AutoIt

Hmm, not according to the documentation?

Where do you get {W Down} from "key"? Helge is correct in your syntax being a tad more than shaky. You should learn the syntax properly before trying to address logic issues. (and do you're home work before trying to say you are correct)

Edit:

I should state about the "Same as Send format" as you are only clicking the key for HotKeySet() not holding them down in order to activate them, so it is like "Send" in that aspect, but you will only use "literal" keys.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I will help you with this, but I'm not giving you a working example. I'm using your first example of code as it was closer to the target.

First of all, in order to use the _IsPressed() Function, you must include Misc.au3.

So add

#include <misc.au3>
oÝ÷ ÚÚ2¢êÜ¡×yÊ'u.ë-¢§¶VÞ~ÞÊj{¬z+ZºÚ"µÍÌÍÙQÜ[  ][ÝÝÙÌ  ][ÝÊNÝÚXÚ]ÝHÛÜÙY]H[ÙHÙÜ[BÛÜÙJ    ÌÍÙ
B

Third: you cannot put {w} and {s} when your communicating with a DLL. They must be referenced by their corresponding hex values.

Refer to the help file under _IsPressed() for more information.

Fourth: Replace the "User32.dll" in your _IsPressed() functions to $DLL as it references the open DLL file.

Fifth: Move the $dif=TimerDiff($begin) to the outside of the While-WEnd loop. As it stands now, the user presses the key, and it enters the loop which immediately stops the timer, whence on the outside, it waits until the key is no longer pressed, and then it evaluates the difference in the timer.

These should be a couple steps to help you on your way, provided that this is NOT, and will not be used for a keylogger.

Edited by hearurscream

Infinity is a floorless room without walls or ceiling.Anyone who cannot cope with mathematics is not fully human. At best he is a tolerable subhuman who has learned to wear shoes, bathe, and not make messes in the house.

Share this post


Link to post
Share on other sites

im still a bit lost unfortunately

Share this post


Link to post
Share on other sites

Elaborate please.

"im still a bit lost unfortunately" tells us nothing.

Can you post anything that you have changed, if you have at all?


Infinity is a floorless room without walls or ceiling.Anyone who cannot cope with mathematics is not fully human. At best he is a tolerable subhuman who has learned to wear shoes, bathe, and not make messes in the house.

Share this post


Link to post
Share on other sites

#include <misc.au3>

HotKeySet('{esc}', 'quit')

While 1
    If _IsPressed(57) Then keytimer(57)
    If _IsPressed(53) Then keytimer(53)
    If _IsPressed(41) Then keytimer(41)
    If _IsPressed(44) Then keytimer(44)
    Sleep(50)
WEnd

Func keytimer($key)
    $s_timer = TimerInit()
    Do
        ;Sleep(50)  ;may want a small sleep here for cpu cycles
    Until Not _IsPressed($key)
    ConsoleWrite(@CRLF & 'key ' & $key & ' held for ' & StringFormat('%.2f', TimerDiff($s_timer) / 1000) & ' secs')
EndFunc

Func quit()
    MsgBox(0, '', 'Exiting...', 1)
    Sleep(1000)
    Exit
EndFunc

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