做爲一個程序員,忽然好奇本身天天敲擊多少次鍵盤,因而研究了一下,寫了一個python程序來記錄。這裏分享給你們。
其實實現很簡單,給python安裝一個package pynput。用其中自帶的keyboard listener. 在輸出統計數據的時候,有兩種選擇,一種是實時輸出,一種是給用某一個特殊的鍵作爲信號告訴代碼輸出統計數據。我選擇的是後者。我用了page_down這個我歷來不用的鍵作輸出。這個script跑起來後,就不要管它,在用完電腦後按下特殊鍵,就能夠輸出統計數據了。
同時我也附上了個人某一天的統計圖。
這一天我總共敲擊鍵盤7836次。一個典型的英文單詞長度是6個字母,至關於我敲了1300個單詞。單純的打字速度是100次每分鐘,因此我一天只敲了13分鐘的鍵盤。。。python
#!/usr/bin/env python from pynput import keyboard def get_key_name(key): if isinstance(key, keyboard.KeyCode): return key.char else: return str(key) def on_press(key): key_name = get_key_name(key) def on_release(key): key_name = get_key_name(key) if key_name in keys: key_stat[key_name] = key_stat[key_name] + 1 if key_name == 'Key.page_down': fp=open('key_stat.xyz','w') for key in keys: fp.write('%s: %i\n' %(key, key_stat[key])) return False key_stat = {} keys = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0','`','Key.esc','Key.tab','Key.caps_lock','Key.shift','Key.ctrl','Key.alt','Key.cmd','Key.space','Key.alt_r','Key.cmd_r','Key.enter','Key.shift_r','Key.backspace','Key.up','Key.down','Key.left','Key.right','Key.ctrl_r',',','.','/',';','\'','\\',']','[','=','-'] for key in keys: key_stat[key] = 0 with keyboard.Listener( on_press = on_press, on_release = on_release) as listener: listener.join()
畫圖的代碼以下程序員
#!/usr/bin/python import numpy as np import matplotlib.pyplot as plt fp = open('key_stat.xyz') keys = [] stats = [] total = 0 for line in fp: x, y = line.split() x = x.replace('Key.','').replace(':','') keys.append(x) stats.append(int(y)) total += int(y) print(total) plt.bar(keys,stats) plt.xticks(keys,rotation='45') plt.show()