Trying to make Keycounter

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Samuel1996
Posts: 2
Joined: 24 Nov 2022, 15:30

Trying to make Keycounter

Post by Samuel1996 » 24 Nov 2022, 15:36

I feel like a complete idiot for this, because I am 100% sure someone else most have figured this out already. But is it possible to have AHK register all my typed keys and save how often I press a specific key or key combo? I found some previous keycounters, like this: https://www.autohotkey.com/board/topic/122565-keystroke-counter/, which logs only H keys. I have been trying to modify this to automatically work with everything else, but I have not yet been succesful. Has anyone published something like this somewhere.

Note that I NOT looking to make a keylogger, I dont care about the order in which the buttons were pressed, I care about how often I press them.

User avatar
mikeyww
Posts: 27077
Joined: 09 Sep 2014, 18:38

Re: Trying to make Keycounter

Post by mikeyww » 24 Nov 2022, 16:35

Welcome to this AutoHotkey forum!

Code: Select all

#SingleInstance Prompt
count  := {}
dir     = %A_ScriptDir%
out     = %dir%\counts.csv
keyList = %dir%\keyList.txt
log     = F4
Loop, 0xFF
 If ("" < key := GetKeyName(Format("VK{:02X}", 256 - A_Index))) {
  Hotkey, ~*%key%, Count, On
  keys .= (keys > "" ? "`n" : "") key
 }
Loop, Parse, % "!@#$%^&*()_+<>?{}|:"""
 Hotkey, ~*%A_LoopField%, Count, On
Loop, Parse, % "Del,Down,End,PgUp,PgDn,Left,Right,Home,Ins", CSV
 Hotkey, ~*%A_LoopField%, Count, On
FileRecycle, %keyList%
FileAppend, %keys%, %keyList%
; Run, %keyList%
Return

Count:
count[A_ThisHotkey] := count[A_ThisHotkey] ? count[A_ThisHotkey] + 1 : 1
If SubStr(A_ThisHotkey, 3) != log
 Return
If FileExist(out) {
 FileRecycle, %out%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while deleting the file.`n`n%out%
  Return
 }
}
text =
For hk, n in count
 text .= (text > "" ? "`n" : "") """" StrReplace(hk, "~*") """," n
FileAppend, %text%, %out%
Run, %out%
Return

User avatar
flyingDman
Posts: 2821
Joined: 29 Sep 2013, 19:01

Re: Trying to make Keycounter

Post by flyingDman » 24 Nov 2022, 17:59

Different approach. Haven't tested it thoroughly (if I did, the turkey would burn...):

Code: Select all

log := {}
loop
{
Input, var, VL1,{Enter}{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}
if instr(el := ErrorLevel,"EndKey:")
	el := substr(el,8), (el) && log[el] := !log.HasKey(el) ? 1 : log[el] + 1
else
	log[var] := !log.HasKey(var) ? 1 : log[var] + 1
}

#\::                ; check the log
gui, destroy
gui, add, listview,vLV w150 r25,key|count
for x,y in Log
	LV_add("",(x=" "? "space" : x),y)
	LV_ModifyCol(1,85)
LV_ModifyCol(2,50)
gui, show, ,LV
return
Last edited by flyingDman on 24 Nov 2022, 18:11, edited 2 times in total.
14.3 & 1.3.7

User avatar
mikeyww
Posts: 27077
Joined: 09 Sep 2014, 18:38

Re: Trying to make Keycounter

Post by mikeyww » 24 Nov 2022, 18:05

OK, but not sure why Gosub recursively instead of a Loop. Could it lead to some fatal stack problem without any Return?
Jumps to the specified label and continues execution until Return is encountered.

User avatar
flyingDman
Posts: 2821
Joined: 29 Sep 2013, 19:01

Re: Trying to make Keycounter

Post by flyingDman » 24 Nov 2022, 18:10

Good point. Corrected above. You might also want to consider using Input, var, MVL1 instead of Input, var, VL1 to catch modified keystrokes (Ctrl a for example)
14.3 & 1.3.7

Samuel1996
Posts: 2
Joined: 24 Nov 2022, 15:30

Re: Trying to make Keycounter

Post by Samuel1996 » 25 Nov 2022, 16:17

Thank you so so much! This was exactly what I was looking for!

Post Reply

Return to “Ask for Help (v1)”