13
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonでキーロガー

Last updated at Posted at 2018-04-01

キーロガー

キーロガー(keylogger)というのはキーボードの入力を記録するソフトで
一般的にはウィルスです。
悪用しないでください。

コード

環境: Python3.6.4

keylogger.py
from pynput.keyboard import Key, Listener

FILE_NAME = 'text.txt'
out = []

def on_press(key):
    try:
        char = key.char
    except:
        #char = key
        char = ''
    finally:
        out.append(char)
def on_release(key):
    pass

def shuffle(out):
    import random
    random.shuffle(out)

if __name__ == '__main__':
    try:
        with Listener(on_press=on_press, on_release=on_release) as listener:
            listener.join()
    except:
        pass
    shuffle(out)

    with open(FILE_NAME, mode='a') as f:
        f.write(''.join(out))

注釈

  • 一般のキー入力を取得するためにはroot権限が必要
  • on_press関数内はtryで一般キー取得、exceptで修飾キー取得。今回修飾キーの情報は不要なのでコメントアウトしてる
  • 終了するときはCtrl+cとかで

きっかけ

キー入力の頻度を測ってキーボード配列をカスタマイズしたかった。
一般に出回るDvorakやColemakだとvimでのhjkl移動が困難なので。

~今後の流れ~
使用頻度の高いキーを記録
→既存のキー配列で自分に合ったものがないか確認
http://patorjk.com/keyboard-layout-analyzer/#/main
→なければ作る

13
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?