記錄每次擊鍵並儲存在檔案中

記錄每次擊鍵並儲存在檔案中

我需要記錄每一次擊鍵店鋪在使用者目錄中的一個檔案中〜,當使用我的帳戶時,我不是sudoer,並且無法安裝程式(例如紀錄鍵) 以任何方式。我怎麼能使用終端來做到這一點?

筆記:這個問題不是另一個提到的問題的重複;在這個問題中,我詢問每次擊鍵,而在另一個問題中,詢問者詢問終端會話中的擊鍵。

答案1

xinput test可以將所有鍵盤事件回報給X伺服器。在 GNU 系統上:

xinput list |
  grep -Po 'id=\K\d+(?=.*slave\s*keyboard)' |
  xargs -P0 -n1 xinput test

如果您想從按鍵代碼中取得按鍵名稱,您可以使用以下命令對該輸出進行後處理:

awk 'BEGIN{while (("xmodmap -pke" | getline) > 0) k[$2]=$4}
     {print $0 "[" k[$NF] "]"}'

新增> file.log以儲存在日誌檔案中。或| tee file.log同時記錄並查看。

xinput查詢 X 伺服器的 XinputExtension。這與您將要獲得的標準(我不知道有任何涵蓋 X 實用程式的標準)或通用命令一樣接近。這也不需要root權限。

如果 X 伺服器和 xinput 支援 XinputExtension 的版本 2,您可以使用whichtest-xi2test提供更多信息,特別是修飾符的狀態(shift、ctrl、alt...)。例子:

$ xinput test-xi2 --root
EVENT type 2 (KeyPress)
    device: 11 (11)
    detail: 54
    flags:
    root: 846.80/451.83
    event: 846.80/451.83
    buttons:
    modifiers: locked 0 latched 0 base 0x4 effective: 0x4
    group: locked 0 latched 0 base 0 effective: 0
    valuators:
    windows: root 0x26c event 0x26c child 0x10006e6

detail您可以在再次的幫助下將鍵碼(在 中)轉換為鍵符號xmodmap -pke,並effective在 的幫助下將修飾符位遮罩轉換為更有用的內容xmodmap -pm。例如:

xinput test-xi2 --root | perl -lne '
  BEGIN{$"=",";
    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;
  }
  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 "$e $d [$k{$d}] $m [@mods]"
    }
  }'

會輸出:

KeyPress 24 [q] 0x19 [Shift,Alt,Num_Lock]

當我在數字鎖定開啟時按 Shift+Alt+q 時。

請注意,您不需要具有超級使用者權限即可安裝一個程序。如果您對檔案系統上授予執行權限的某個位置(您的主目錄,...)具有寫入存取權限,/tmp那麼/var/tmp您可以xinput從那裡的相容系統複製命令並執行它。

答案2

您是否考慮過使用script命令?

相關內容