Idea 作為一個每天有一半時間都在電腦旁的人,無時無刻不在敲擊著鍵盤,點擊著滑鼠。有一天,我突然很想知道在一天的時間內,在我輕盈的指法下麵,鍵盤被我狂敲了多少下,滑鼠又被我點擊了多少次。甚至更具體一些,鍵盤上哪些鍵挨的敲擊次數更多呢?想想也覺得挺有意思的。 <! more Learing 有了想法 ...
Idea
作為一個每天有一半時間都在電腦旁的人,無時無刻不在敲擊著鍵盤,點擊著滑鼠。有一天,我突然很想知道在一天的時間內,在我輕盈的指法下麵,鍵盤被我狂敲了多少下,滑鼠又被我點擊了多少次。甚至更具體一些,鍵盤上哪些鍵挨的敲擊次數更多呢?想想也覺得挺有意思的。
Learing
有了想法,接著就是上(尋)網(找)學(代)習(碼)了。既然要記錄鍵盤敲擊和滑鼠點擊的次數,那就得監聽鍵盤和滑鼠的事件。在搜索過程中,瞭解到了鉤子函數這個概念。想要學習的同學可以看下這篇博客
畢竟我只是想實現一下功能,也就沒太深入學習。對比了一些各種語言對於監聽事件的實現代碼,發現Python簡直不能再簡潔了,果斷使用Python。
Coding(Copying)
現在想法、概念知識和所用工具都具備了,剩下的就是用代碼去實現。Python環境自然不用多說,除此之外還需要幾個第三方的庫。主要參考這篇博客,自己的想法其實別人早就已經實現了。
當然別人的代碼並不完全符合自己的需求,我不需要記錄鍵盤、滑鼠點擊事件過於詳細的信息,我需要記錄的僅僅是次數。在前人的代碼基礎上修改一番即可。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pythoncom
import pyHook
import time
import win32api
"""
def onMouseEvent(event):
"處理滑鼠事件"
fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n')
fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
fobj.writelines("MessageName:%s\n" % str(event.MessageName))
fobj.writelines("Message:%d\n" % event.Message)
fobj.writelines("Time_sec:%d\n" % event.Time)
fobj.writelines("Window:%s\n" % str(event.Window))
fobj.writelines("WindowName:%s\n" % str(event.WindowName))
fobj.writelines("Position:%s\n" % str(event.Position))
fobj.writelines('-' * 20 + 'MouseEvent End' + '-' * 20 + '\n')
return True
"""
def onMouse_rightup(event):
# 監聽滑鼠左鍵按下事件 次數加1
global right_up_num
right_up_num += 1
return True
def onMouse_leftup(event):
# 監聽滑鼠左鍵彈起事件 次數加1
global left_up_num
left_up_num += 1
return True
def onKeyboardEvent(event):
# 處理鍵盤事件 定義各個鍵位全局變數,記錄敲擊次數
global key_a_num
global key_b_num
global key_c_num
global key_d_num
global key_e_num
global key_f_num
global key_g_num
global key_h_num
global key_i_num
global key_j_num
global key_k_num
global key_l_num
global key_m_num
global key_n_num
global key_o_num
global key_p_num
global key_q_num
global key_r_num
global key_s_num
global key_t_num
global key_u_num
global key_v_num
global key_w_num
global key_x_num
global key_y_num
global key_z_num
global key_enter_num
global key_back_num
global key_1_num
global key_2_num
global key_3_num
global key_4_num
global key_5_num
global key_6_num
global key_7_num
global key_8_num
global key_9_num
global key_0_num
global key_oem_3_num #~
global key_oem_minus_num #-
global key_oem_plus_num #+
global key_tab_num #tab
global key_cap_num #caps
global key_lshift_num #left shift
global key_lctrl_num #left ctrl
global key_lwin_num
global key_lalt_num
global key_space_num
global key_rshift_num #left shift
global key_rctrl_num #left ctrl
global key_rwin_num
global key_ralt_num
global key_oem_comma_num #,
global key_oem_period_num #.
global key_oem_2_num #/
global key_oem_1_num #;
global key_oem_7_num #'
global key_oem_4_num #[
global key_oem_5_num #]
global key_oem_6_num #\
global key_left_num #左
global key_right_num #右
global key_up_num #上
global key_down_num #下
global key_delete_num #delete
global key_end_num #end
global key_next_num #page down
global key_insert_num #insert
global key_home_num #home
global key_prior_num #page up
global key_snapshot_num #print screen
global key_scroll_num #scroll lock
global key_pause_num #pause
global key_escape_num #esc
global key_f1_num
global key_f2_num
global key_f3_num
global key_f4_num
global key_f5_num
global key_f6_num
global key_f7_num
global key_f8_num
global key_f9_num
global key_f10_num
global key_f11_num
global key_f12_num
global key_numpad0_num
global key_numpad1_num
global key_numpad2_num
global key_numpad3_num
global key_numpad4_num
global key_numpad5_num
global key_numpad6_num
global key_numpad7_num
global key_numpad8_num
global key_numpad9_num
global key_numlock_num
global key_divide_num
global key_multiply_num
global key_subtract_num
global key_add_num
global key_decimal_num
global key_total_num #總敲擊次數
fobj.write("%s" % str(event.Key)+" ")
s=str(event.Key)
if s=='A':
key_a_num+=1
key_total_num+=1
elif s=='B':
key_b_num+=1
key_total_num+=1
elif s=='C':
key_c_num+=1
key_total_num+=1
elif s=='D':
key_d_num+=1
key_total_num+=1
elif s=='E':
key_e_num+=1
key_total_num+=1
elif s=='F':
key_f_num+=1
key_total_num+=1
elif s=='G':
key_g_num+=1
key_total_num+=1
elif s=='H':
key_h_num+=1
key_total_num+=1
elif s=='I':
key_i_num+=1
key_total_num+=1
elif s=='J':
key_j_num+=1
key_total_num+=1
elif s=='K':
key_k_num+=1
key_total_num+=1
elif s=='L':
key_l_num+=1
key_total_num+=1
elif s=='M':
key_m_num+=1
key_total_num+=1
elif s=='N':
key_n_num+=1
key_total_num+=1
elif s=='O':
key_o_num+=1
key_total_num+=1
elif s=='P':
key_p_num+=1
key_total_num+=1
elif s=='Q':
key_q_num+=1
key_total_num+=1
elif s=='R':
key_r_num+=1
key_total_num+=1
elif s=='S':
key_s_num+=1
key_total_num+=1
elif s=='T':
key_t_num+=1
key_total_num+=1
elif s=='U':
key_u_num+=1
key_total_num+=1
elif s=='V':
key_v_num+=1
key_total_num+=1
elif s=='W':
key_w_num+=1
key_total_num+=1
elif s=='X':
key_x_num+=1
key_total_num+=1
elif s=='Y':
key_y_num+=1
key_total_num+=1
elif s=='Z':
key_z_num+=1
key_total_num+=1
elif s=='Return':
key_enter_num+=1
key_total_num+=1
elif s=='Back':
key_back_num+=1
key_total_num+=1
elif s=='1':
key_1_num+=1
key_total_num+=1
elif s=='2':
key_2_num+=1
key_total_num+=1
elif s=='3':
key_3_num+=1
key_total_num+=1
elif s=='4':
key_4_num+=1
key_total_num+=1
elif s=='5':
key_5_num+=1
key_total_num+=1
elif s=='6':
key_6_num+=1
key_total_num+=1
elif s=='7':
key_7_num+=1
key_total_num+=1
elif s=='8':
key_8_num+=1
key_total_num+=1
elif s=='9':
key_9_num+=1
key_total_num+=1
elif s=='0':
key_0_num+=1
key_total_num+=1
elif s=='Oem_3':
key_oem_3_num+=1
key_total_num+=1
elif s=='Oem_Minus':
key_oem_minus_num+=1
key_total_num+=1
elif s=='Oem_Plus':
key_oem_plus_num+=1
key_total_num+=1
elif s=='Tab':
key_tab_num+=1
key_total_num+=1
elif s=='Capital':
key_cap_num+=1
key_total_num+=1
elif s=='Lshift':
key_lshift_num+=1
key_total_num+=1
elif s=='Lcontrol':
key_lctrl_num+=1
key_total_num+=1
elif s=='Lwin':
key_lwin_num+=1
key_total_num+=1
elif s=='Lmenu':
key_lalt_num+=1
key_total_num+=1
elif s=='Space':
key_space_num+=1
key_total_num+=1
elif s=='Rshift':
key_rshift_num+=1
key_total_num+=1
elif s=='Rcontrol':
key_rctrl_num+=1
key_total_num+=1
elif s=='Rwin':
key_rwin_num+=1
key_total_num+=1
elif s=='Rmenu':
key_ralt_num+=1
key_total_num+=1
elif s=='Oem_Comma':
key_oem_comma_num+=1
key_total_num+=1
elif s=='Oem_Period':
key_oem_period_num+=1
key_total_num+=1
elif s=='Oem_2':
key_oem_2_num+=1
key_total_num+=1
elif s=='Oem_1':
key_oem_1_num+=1
key_total_num+=1
elif s=='Oem_7':
key_oem_7_num+=1
key_total_num+=1
elif s=='Oem_4':
key_oem_4_num+=1
key_total_num+=1
elif s=='Oem_5':
key_oem_5_num+=1
key_total_num+=1
elif s=='Oem_6':
key_oem_6_num+=1
key_total_num+=1
elif s=='Left':
key_left_num+=1
key_total_num+=1
elif s=='Right':
key_right_num+=1
key_total_num+=1
elif s=='Up':
key_up_num+=1
key_total_num+=1
elif s=='Down':
key_down_num+=1
key_total_num+=1
elif s=='Delete':
key_delete_num+=1
key_total_num+=1
elif s=='End':
key_end_num+=1
key_total_num+=1
elif s=='Next':
key_next_num+=1
key_total_num+=1
elif s=='Insert':
key_insert_num+=1
key_total_num+=1
elif s=='Home':
key_home_num+=1
key_total_num+=1
elif s=='Prior':
key_prior_num+=1
key_total_num+=1
elif s=='Snapshot':
key_snapshot_num+=1
key_total_num+=1
elif s=='Scroll':
key_scroll_num+=1
key_total_num+=1
elif s=='Pause':
key_pause_num+=1
key_total_num+=1
elif s=='Escape':
key_escape_num+=1
key_total_num+=1
elif s=='F1':
key_f1_num+=1
key_total_num+=1
elif s=='F2':
key_f2_num+=1
key_total_num+=1
elif s=='F3':
key_f3_num+=1
key_total_num+=1
elif s=='F4':
key_f4_num+=1
key_total_num+=1
elif s=='F5':
key_f5_num+=1
key_total_num+=1
elif s=='F6':
key_f6_num+=1
key_total_num+=1
elif s=='F7':
key_f7_num+=1
key_total_num+=1
elif s=='F8':
key_f8_num+=1
key_total_num+=1
elif s=='F9':
key_f9_num+=1
key_total_num+=1
elif s=='F10':
key_f10_num+=1
key_total_num+=1
elif s=='F11':
key_f11_num+=1
key_total_num+=1
elif s=='F12':
key_f12_num+=1
key_total_num+=1
elif s=='Numpad0':
key_numpad0_num+=1
key_total_num+=1
elif s=='Numpad1':
key_numpad1_num+=1
key_total_num+=1
elif s=='Numpad2':
key_numpad2_num+=1
key_total_num+=1
elif s=='Numpad3':
key_numpad3_num+=1
key_total_num+=1
elif s=='Numpad4':
key_numpad4_num+=1
key_total_num+=1
elif s=='Numpad5':
key_numpad5_num+=1
key_total_num+=1
elif s=='Numpad6':
key_numpad6_num+=1
key_total_num+=1
elif s=='Numpad7':
key_numpad7_num+=1
key_total_num+=1
elif s=='Numpad8':
key_numpad8_num+=1
key_total_num+=1
elif s=='Numpad9':
key_numpad9_num+=1
key_total_num+=1
elif s=='Numlock':
key_numlock_num+=1
key_total_num+=1
elif s=='Divide':
key_divide_num+=1
key_total_num+=1
elif s=='Multiply':
key_multiply_num+=1
key_total_num+=1
elif s=='Subtract':
key_subtract_num+=1
key_total_num+=1
elif s=='Add':
key_add_num+=1
key_total_num+=1
elif s=='Decimal':
key_decimal_num+=1
key_total_num+=1
#定義一個退出的按鍵,並將記錄的信息寫入txt
if str(event.Key)=='Pause':
fobj.write("\n")
fobj.write("滑鼠右鍵點擊量: %d\n" % right_up_num)
fobj.write("滑鼠左鍵點擊量: %d\n" % left_up_num)
fobj.write("鍵盤總點擊量: %d\n" % key_total_num)
fobj.write("a: %d\n" % key_a_num)
fobj.write("b: %d\n" % key_b_num)
fobj.write("c: %d\n" % key_c_num)
fobj.write("d: %d\n" % key_d_num)
fobj.write("e: %d\n" % key_e_num)
fobj.write("f: %d\n" % key_f_num)
fobj.write("g: %d\n" % key_g_num)
fobj.write("h: %d\n" % key_h_num)
fobj.write("i: %d\n" % key_i_num)
fobj.write("j: %d\n" % key_j_num)
fobj.write("k: %d\n" % key_k_num)
fobj.write("l: %d\n" % key_l_num)
fobj.write("m: %d\n" % key_m_num)
fobj.write("n: %d\n" % key_n_num)
fobj.write("o: %d\n" % key_o_num)
fobj.write("p: %d\n" % key_p_num)
fobj.write("q: %d\n" % key_q_num)
fobj.write("r: %d\n" % key_r_num)
fobj.write("s: %d\n" % key_s_num)
fobj.write("t: %d\n" % key_t_num)
fobj.write("u: %d\n" % key_u_num)
fobj.write("v: %d\n" % key_v_num)
fobj.write("w: %d\n" % key_w_num)
fobj.write("x: %d\n" % key_x_num)
fobj.write("y: %d\n" % key_y_num)
fobj.write("z: %d\n" % key_z_num)
fobj.write("enter: %d\n" % key_enter_num)
fobj.write("back: %d\n" % key_back_num)
fobj.write("0: %d\n" % key_0_num)
fobj.write("1: %d\n" % key_1_num)
fobj.write("2: %d\n" % key_2_num)
fobj.write("3: %d\n" % key_3_num)
fobj.write("4: %d\n" % key_4_num)
fobj.write("5: %d\n" % key_5_num)
fobj.write("6: %d\n" % key_6_num)
fobj.write("7: %d\n" % key_7_num)
fobj.write("8: %d\n" % key_8_num)
fobj.write("9: %d\n" % key_9_num)
fobj.write("~: %d\n" % key_oem_3_num)
fobj.write("r-: %d\n" % key_oem_minus_num)
fobj.write("r+: %d\n" % key_oem_plus_num)
fobj.write("tab: %d\n" % key_tab_num)
fobj.write("caps: %d\n" % key_cap_num)
fobj.write("Lshift: %d\n" % key_lshift_num)
fobj.write("Lctrl: %d\n" % key_lctrl_num)
fobj.write("Lwin: %d\n" % key_lwin_num)
fobj.write("Lalt: %d\n" % key_lalt_num)
fobj.write("space: %d\n" % key_space_num)
fobj.write("Rshift: %d\n" % key_rshift_num)
fobj.write("Rctrl: %d\n" % key_rctrl_num)
fobj.write("Rwin: %d\n" % key_rwin_num)
fobj.write("Ralt: %d\n" % key_ralt_num)
fobj.write(",<: %d\n" % key_oem_comma_num)
fobj.write(".>: %d\n" % key_oem_period_num)
fobj.write("/?: %d\n" % key_oem_2_num)
fobj.write(";: %d\n" % key_oem_1_num)
fobj.write("雙引號: %d\n" % key_oem_7_num)
fobj.write("[{: %d\n" % key_oem_4_num)
fobj.write("]}: %d\n" % key_oem_5_num)
fobj.write("\|: %d\n" % key_oem_6_num)
fobj.write("左: %d\n" % key_left_num)
fobj.write("右: %d\n" % key_right_num)
fobj.write("上: %d\n" % key_up_num)
fobj.write("下: %d\n" % key_right_num)
fobj.write("delete: %d\n" % key_delete_num)
fobj.write("end: %d\n" % key_end_num)
fobj.write("pagedown: %d\n" % key_next_num)
fobj.write("insert: %d\n" % key_insert_num)
fobj.write("pageup: %d\n" % key_prior_num)
fobj.write("home: %d\n" % key_home_num)
fobj.write("print screen: %d\n" % key_snapshot_num)
fobj.write("scroll lock: %d\n" % key_scroll_num)
fobj.write("pause: %d\n" % key_pause_num)
fobj.write("esc: %d\n" % key_escape_num)
fobj.write("f1: %d\n" % key_f1_num)
fobj.write("f2: %d\n" % key_f2_num)
fobj.write("f3: %d\n" % key_f3_num)
fobj.write("f4: %d\n" % key_f4_num)
fobj.write("f5: %d\n" % key_f5_num)
fobj.write("f6: %d\n" % key_f6_num)
fobj.write("f7: %d\n" % key_f7_num)
fobj.write("f8: %d\n" % key_f8_num)
fobj.write("f9: %d\n" % key_f9_num)
fobj.write("f10: %d\n" % key_f10_num)
fobj.write("f11: %d\n" % key_f11_num)
fobj.write("f12: %d\n" % key_f12_num)
fobj.write("numpad0: %d\n" % key_numpad0_num)
fobj.write("numpad1: %d\n" % key_numpad1_num)
fobj.write("numpad2: %d\n" % key_numpad2_num)
fobj.write("numpad3: %d\n" % key_numpad3_num)
fobj.write("numpad4: %d\n" % key_numpad4_num)
fobj.write("numpad5: %d\n" % key_numpad5_num)
fobj.write("numpad6: %d\n" % key_numpad6_num)
fobj.write("numpad7: %d\n" % key_numpad7_num)
fobj.write("numpad8: %d\n" % key_numpad8_num)
fobj.write("numpad9: %d\n" % key_numpad9_num)
fobj.write("numlock: %d\n" % key_numlock_num)
fobj.write("divide: %d\n" % key_divide_num)
fobj.write("multiply: %d\n" % key_multiply_num)
fobj.write("subtract: %d\n" % key_subtract_num)
fobj.write("add: %d\n" % key_add_num)
fobj.write("decimal: %d\n" % key_decimal_num)
fobj.close()
win32api.PostQuitMessage()
return True
#主函數
if __name__ == "__main__":
key_a_num=0
key_b_num=0
key_c_num=0
key_d_num=0
key_e_num=0
key_f_num=0
key_g_num=0
key_h_num=0
key_i_num=0
key_j_num=0
key_k_num=0
key_l_num=0
key_m_num=0
key_n_num=0
key_o_num=0
key_p_num=0
key_q_num=0
key_r_num=0
key_s_num=0
key_t_num=0
key_u_num=0
key_v_num=0
key_w_num=0
key_x_num=0
key_y_num=0
key_z_num=0
key_enter_num=0
key_back_num=0
key_1_num=0
key_2_num=0
key_3_num=0
key_4_num=0
key_5_num=0
key_6_num=0
key_7_num=0
key_8_num=0
key_9_num=0
key_0_num=0
key_oem_3_num=0
key_oem_minus_num=0
key_oem_plus_num=0
key_tab_num=0
key_cap_num=0
key_lshift_num=0
key_lctrl_num=0
key_lwin_num=0
key_lalt_num=0
key_space_num=0
key_rshift_num=0
key_rctrl_num=0
key_rwin_num=0
key_ralt_num=0
key_oem_comma_num=0
key_oem_period_num=0
key_oem_2_num=0
key_oem_1_num=0
key_oem_7_num=0
key_oem_4_num=0
key_oem_5_num=0
key_oem_6_num=0
key_left_num=0
key_right_num=0
key_up_num=0
key_down_num=0
key_delete_num=0 #delete
key_end_num=0 #end
key_next_num=0 #page down
key_insert_num=0 #insert
key_home_num=0 #home
key_prior_num=0 #page up
key_snapshot_num=0 #print screen
key_scroll_num=0 #scroll lock
key_pause_num=0 #pause
key_escape_num=0
key_f1_num=0
key_f2_num=0
key_f3_num=0
key_f4_num=0
key_f5_num=0
key_f6_num=0
key_f7_num=0
key_f8_num=0
key_f9_num=0
key_f10_num=0
key_f11_num=0
key_f12_num=0
key_numpad0_num=0
key_numpad1_num=0
key_numpad2_num=0
key_numpad3_num=0
key_numpad4_num=0
key_numpad5_num=0
key_numpad6_num=0
key_numpad7_num=0
key_numpad8_num=0
key_numpad9_num=0
key_numlock_num=0
key_divide_num=0
key_multiply_num=0
key_subtract_num=0
key_add_num=0
key_decimal_num=0
key_total_num=0
right_up_num=0
left_up_num=0
#打開日誌文件
file_name = "D:\\keyboard_recoder\\2018-01-26.txt"
fobj = open(file_name, 'w')
#創建hook句柄
hm = pyHook.HookManager()
#監控滑鼠
hm.MouseRightUp=onMouse_rightup
hm.MouseLeftUp=onMouse_leftup
hm.HookMouse()
#監控鍵盤
hm.KeyUp = onKeyboardEvent
hm.HookKeyboard()
#迴圈獲取消息
pythoncom.PumpMessages()
#關閉日誌文件
fobj.close()
上述代碼記錄了鍵盤上的每一個按鈕敲擊的次數,當然不同的鍵盤所包含的鍵數不同,還得根據實際情況進行改進。將這段代碼保存為.py文件,這樣每天在打開電腦後雙擊運行。在結束使用電腦後按下Pause鍵退出,即可看到一天的鍵盤和滑鼠點擊次數。
data visualization
利用上述程式,我記錄了20天滑鼠和鍵盤的敲擊的次數,並對數據進行稍微的處理,得到一個Excel表格。
20天內滑鼠左鍵點擊了44035下,右鍵點擊了1228下,鍵盤總共敲擊了91960次。Excel表格數據看起來不是很直觀,為了更加直觀地展示數據,採用Echarts對數據進行簡單的可視化處理,即用圖表來表示數據。這裡選擇層疊柱狀圖來展現20天內每個鍵位點擊的數量,同時也能展現總體的數量。
最後利用熱力圖最直觀地展現敲擊次數的鍵位。
熱力圖的製作首先從網上找到高清的鍵盤圖作為熱力圖的地圖,然後用像素橫縱坐標定義每個鍵位大概的位置,但是最後結果不是特別好。這裡推薦一個網址,可以線上根據文本實時生成鍵盤鍵點陣圖,但是似乎只能記錄能在屏幕上能打出符號的鍵位,空格、換行等都無法記錄,不過效果的確不錯。代碼是開源的,大家可以去GitHub下載。實現鍵盤熱力圖的大神還有許多有意思的項目,有興趣的同學可以研究下。
- https://www.patrick-wied.at/
我的Backspace敲擊次數排在第三位,看來準確率有些低呀。大家快去記錄一下自己每天要狂敲多少次鍵盤吧。