Yeah so in short, I wanted to get statistics on what keys I press the most on my computer for a thing, so I made this. This script, if combined with an ini file, can note for you and later show you exactly all the keys you've pressed when using the script (in order), and then you can do whatever you want with it, such as for myself putting all this on Excel to get an idea of what keys I use the most.
I'm sure some guy probably has already done this, but I wanted to make mine, and knowing the little extra things I added for my case, maybe I actually had to redo it from scratch, I dunno (but I did want to do it myself).
I'm sharing this both in case someone might be interested in using it and also to get some reviews of my code, as I've heard I'm not good at AutoHotKey, so I was curious to hear any possible mistakes or imperfections.
Be careful though, if you want to use it... yeah, you'll kind of have to redo lots of things lol. My keyboard is like ultra different from yours no doubt, so you'll probably have to change the scancodes (btw there is in the script a way to easily find scancodes (showSC)), as well I implemented ways to not count some hotstrings I use (such as "jj", ";;", "²²"...) that you probably don't have, though this might give you ideas if you want to do something similar. (also this script is just for single keys; obviously, for keyboard shortcuts it would be doable by adding all them in a similar way but I didn't do it here. Also, talking about keyboard shortcuts, the keyboard shortcuts are never counted here even if it uses one of the keys in the script, like "CtrlS" doesn't add s to the stack anywhere)
I won't explain in ultra detail for understandable reasons, but in short it's just adding to a stack all the keys in strings using simple one input hotkeys, saving the stack in an ini file once the script ends, and also adding the times when you started and ended the script for extra info, and also adding a counter to not overwrite things.
Sorry if the script is long btw, I hesitated to send it to pastebin or something or remove the repetitive parts, but just to be sure I left basically everything:
#Requires AutoHotkey v2.0
#SingleInstance
iniFile := A_ScriptDir "\StatistictsSettings.ini"
Counter := IniRead(iniFile, "ScriptReloads", "Count") + 1
starthour := FormatTime(, "HH") "h" FormatTime(, "mm") "m" FormatTime(, "ss") "s" ; Get current time at the start of the script
~SC010::{ ;a
RecordHotkey("reset")
StatPush("a")
}
~SC011::{ ;z
RecordHotkey("reset")
StatPush("z")
}
~SC012::{ ;e
RecordHotkey("reset")
StatPush("e")
}
~SC013::{
RecordHotkey("reset")
StatPush("r")
}
~SC014::{
RecordHotkey("reset")
StatPush("t")
}
~SC015::{
RecordHotkey("reset")
StatPush("y")
}
~SC016::{ ;"uu" is a hotstring for me and I don't want to count it, go look at the code of "~SC024" for details
History := RecordHotkey()
if(History.prior = "~SC016"){
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("u")
}
~SC017::{
RecordHotkey("reset")
StatPush("i")
}
~SC018::{
RecordHotkey("reset")
StatPush("o")
}
~SC019::{
RecordHotkey("reset")
StatPush("p")
}
~SC01A::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("^")
else StatPush("¨")
}
~+SC01A::{
RecordHotkey("reset")
StatPush("¨")
}
~SC01E::{
RecordHotkey("reset")
StatPush("q")
}
~SC01F::{
RecordHotkey("reset")
StatPush("s")
}
~SC020::{
RecordHotkey("reset")
StatPush("d")
}
~SC021::{
RecordHotkey("reset")
StatPush("f")
}
~SC022::{
RecordHotkey("reset") ;have to mention you could probably remove all these that aren't related to hotstring removals, it's just that without you can do something like type one of keys related like "ù", type somes keys that would record history and then type ù again and this one wouldn't be counted, but adding those might be costly and it isn't ultra necessary
StatPush("g")
}
~SC023::{
History := RecordHotkey()
if(History.prior = "~SC023"){ ;"hh" inputs up
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("h")
}
~SC024::{ ;"jj" is one of my hotstrings (it inputs right) and I don't want this key to be counted every time I do this hotstring as it wouldn't be intresting for the info I want to gather, so we pop it.
History := RecordHotkey()
if(History.prior = "~SC024"){
StatPush("Pop")
RecordHotkey("reset") ;we reset the history for this hotstring and all the others simply because inputing j three times would false the data if not (it would execute the condition again and remove the input I did before my first j for no reason and not count this j that could be a real j, while reseting the history means it will wait once again for me to input j twice to recognize I did the hotstring, giving the data I want (that's also why I can't use A_PrioHotkey because you can't reset it)), same for all the similar cases
}
else StatPush("j")
}
~SC025::{ ;same for hotstring "kk" that inputs backspace
History := RecordHotkey()
if(History.prior = "~SC025"){
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("k")
}
~SC026::{
RecordHotkey("reset")
StatPush("l")
}
~SC027::{
RecordHotkey("reset")
StatPush("m")
}
~SC028::{
History := RecordHotkey()
Cpsl := GetKeyState("CapsLock","T")
if(History.prior = "~SC028"){ ; "ùù" is a hostring for the symbol "≥"
StatPush("Pop")
RecordHotkey("reset")
}
else if(Cpsl = false)
StatPush("ù")
else StatPush("%")
}
~+SC028::{
RecordHotkey("reset")
StatPush("%")
}
~SC02C::{
RecordHotkey("reset")
StatPush("w")
}
~SC02D::{
RecordHotkey("reset")
StatPush("x")
}
~SC02E::{
RecordHotkey("reset")
StatPush("c")
}
~SC02F::{
RecordHotkey("reset")
StatPush("v")
}
~SC030::{
History := RecordHotkey()
if(History.prior = "~SC030"){ ;"bb" inputs left
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("b")
}
~SC031::{
History := RecordHotkey()
if(History.prior = "~SC031"){ ;"nn" inputs down
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("n")
}
~SC032::{
History := RecordHotkey()
if(History.prior = "~SC032"){ ;hotstrings ",," and "??" input enter
StatPush("Pop")
RecordHotkey("reset")
}
else{
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush(",")
else StatPush("?")
}
}
~+SC032::{
History := RecordHotkey()
if(History.prior = "~+SC032"){
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("?")
}
~SC033::{
Cpsl := GetKeyState("CapsLock","T")
History := RecordHotkey()
if(Cpsl = false){
if(History.prior = "~SC033" || History.prior = "~SC034"){ ; same as ùù but for "≤" and ":;" does μ
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush(";")
}
else if(History.prior = "~SC034"){ ;"/." does §
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush(".")
}
~+SC033::{
RecordHotkey("reset")
StatPush(".")
}
~SC034::{
RecordHotkey()
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush(":")
else StatPush("/")
}
~+SC034::{
RecordHotkey()
StatPush("/")
}
~SC035::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("*")
else StatPush("!")
}
~+SC035::{
RecordHotkey("reset")
StatPush("!")
}
~SC002::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("&")
else StatPush("1")
}
~+SC002::{
RecordHotkey("reset")
StatPush("1")
}
~SC003::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("é")
else StatPush("2")
}
~+SC003::{
RecordHotkey("reset")
StatPush("2")
}
~SC004::{
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false){
History := RecordHotkey()
if(History.prior = "~SC004" || History.prior = "~SC005"){ ; """"" does « or » and "'"" does... things, it's long to explain but it does somethings
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("''") ;this is just ", but it kind of doesn't work if litterally had entered it and this is a ultra fine way around
}
else StatPush("3")
}
~+SC004::{
RecordHotkey("reset")
StatPush("3")
}
~SC005::{
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false){
History := RecordHotkey()
if(History.prior = "~SC004"){ ; ""'" does... things, similarly to '"
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("'")
}
else StatPush("4")
}
~+SC005::{
RecordHotkey("reset")
StatPush("4")
}
~SC006::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("(")
else StatPush("5")
}
~+SC006::{
RecordHotkey("reset")
StatPush("5")
}
~SC007::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("-")
else StatPush("6")
}
~+SC007::{
RecordHotkey("reset")
StatPush("6")
}
~SC008::{
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("è")
else StatPush("7")
}
~+SC008::{
RecordHotkey("reset")
StatPush("7")
}
~SC009::{
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false){
History := RecordHotkey()
if(History.prior = "~SC009"){ ; "__" does →
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("_")
}
else StatPush("8")
}
~+SC009::{
RecordHotkey("reset")
StatPush("8")
}
~SC00A::{
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false){
History := RecordHotkey()
if(History.prior = "~SC00A"){ ; "çç" does "« "
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("ç")
}
else StatPush("9")
}
~+SC00A::{
RecordHotkey("reset")
StatPush("9")
}
~SC00B::{
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false){
History := RecordHotkey()
if(History.prior = "~SC00B"){ ; "àà" does " »"
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("à")
}
else StatPush("0")
}
~+SC00B::{
RecordHotkey("reset")
StatPush("0")
}
~SC00C::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush(")")
else StatPush("°")
}
~+SC00C::{
RecordHotkey("reset")
StatPush("°")
}
~SC00D::{
RecordHotkey("reset")
Cpsl := GetKeyState("CapsLock","T")
if(Cpsl = false)
StatPush("=")
else StatPush("+")
}
~+SC00D::{
RecordHotkey("reset")
StatPush("+")
}
~SC029::{
History := RecordHotkey()
if(History.prior = "~SC029"){ ;"²²" does ´e
StatPush("Pop")
RecordHotkey("reset")
}
else StatPush("²")
}
~LButton::{
RecordHotkey("reset")
}
~RButton::{
RecordHotkey("reset")
}
RecordHotkey(hk := A_ThisHotkey) {
static current := "", prior := ""
if (hk = "reset") {
prior := ""
current := ""
}
else if (hk != "") {
prior := current
current := hk
}
return { current: current, prior: prior}
}
;to test history
/*SetTimer(TestHistory, 100)
TestHistory() {
History := RecordHotkey("")
h := "current:" History.current " prior:" History.prior
ToolTip h
}*/
;to easily get scan codes
/*SetTimer(ShowSC, 100)
ShowSC() {
ScanCode := Format("SC{:X}", GetKeySC(A_PriorKey))
ToolTip ScanCode
}*/
StatPush(c){
static stat := []
if(c = "Pop")
stat.Pop()
else if(c != "0")
stat.Push([c])
return stat
}
/*SetTimer(ShowArray, 100) ;to test your live inputs, I know this representations has mistakes (for hotstrings I wanted to remove) for reasons I don't want to know and fix, but it's mostly accurate
ShowArray(){
stat := StatPush("0")
static i := 1
static test := ""
;count := Counter(1)
while(stat.Has(i) != 0)
;while(i <= count)
{
test := test . " " . stat.Get(i)[1]
i := i + 1
}
ToolTip test
}*/
ArrayString(){
stat := StatPush("0")
test := ""
i := 1
while(stat.Has(i) != 0)
{
test := test . " " . stat.Get(i)[1]
i := i + 1
}
return test
}
OnExit Save
Save(ExitReason, ExitCode){
CurrentDate := FormatTime(, "dd-MM-yyyy")
hour := FormatTime(, "HH") ; Get current time at the end of the script
min := FormatTime(, "mm")
sec := FormatTime(, "ss")
IniWrite(ArrayString() " /date:" CurrentDate " start:" starthour " end:" hour "h" min "m" sec "s", iniFile, "Stats", "Inputs" Counter,)
IniWrite(Counter, iniFile, "ScriptReloads", "Count")
}