Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Last active February 8, 2021 19:43
Show Gist options
  • Save pstuifzand/f8922f16905299075d9f5eb9ffd09ef1 to your computer and use it in GitHub Desktop.
Save pstuifzand/f8922f16905299075d9f5eb9ffd09ef1 to your computer and use it in GitHub Desktop.
Keyboard logger
#!/bin/bash
xinput test-xi2 --root | perl -MTime::HiRes=time -lne '
BEGIN{$"=",";
select((select(STDOUT),$|=1)[0]);
open X, "-|", "xmodmap -pke";
while (<X>) {$k{$1}=$2 if /^keycode\s+(\d+) = (\w+)/}
open X, "-|", "xmodmap -pm"; <X>;<X>;
while (<X>) {if (/^(\w+)\s+(\w*)/){($k=$2)=~s/_[LR]$//;$m[$i++]=$k||$1}}
close X;
}
$time = time();
if (/^EVENT type.*\((.*)\)/) {$e = $1}
elsif (/detail: (\d+)/) {$d=$1}
elsif (/modifiers:.*effective: (.*)/) {
$m=$1;
if ($e =~ /^Key/){
my @mods;
for (0..$#m) {push @mods, $m[$_] if (hex($m) & (1<<$_))}
print "time=$time,event=$e,detail=$d,key=$k{$d},modbits=$m,mods=@mods"
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment