前言
刷掘金时候偶然看到了记录下今天敲了多少下键盘,但是博主只是展示了统计出的键盘使用次数,因此想到在这个idea上小改一下,实现键盘按键的热力图展示。
思路分析
- 按键记录 这里可以使用 pynput 来侦听键盘事件并记录每次按键的计数。
- 数据存储 使用一个字典来存储按键次数,其中键代表键盘键,值代表按下的次数。
- 可视化 使用 matplotlib 和 seaborn 库可视化热力图。 您将把字典中的计数映射到色标,该色标将显示在代表 108 键键盘的布局上。
- 实时显示 使用 tkinter 库实时更新热图
具体实现
设置环境
首先安装一些Python库。pynput
库将允许我们捕获键盘事件,matplotlib
将使我们能够创建热图可视化,tkinter
将用于构建GUI。:
bash
pip install pynput matplotlib tk
定义按键字典
这里我们需要预先使用字典存储键盘中的全部按键的名字。
python
key_layout = [
['Esc', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'PrtSc', 'Scroll', 'Pause'],
['`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'Backsp', '', ''],
['Tab', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', '\', '', ''],
['Caps', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', "'", 'Enter', '', '', ''],
['Shift', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 'Shift', '', '', '', ''],
['Ctrl', 'Win', 'Alt', 'Space', 'Alt', 'Fn', 'Menu', 'Ctrl', '', '', '', '', '', '', '', '']
]
捕获键盘输入
pynput
可以以非阻塞方式监听键盘事件。保证程序可以在监听按键时继续进行其他任务(如更新GUI)。以下是如何为键盘事件设置监听器:
python
from pynput.keyboard import Listener
def on_press(key):
try:
key_name = key.char.lower() if hasattr(key, 'char') and key.char is not None else key.name.lower()
if key_name in key_counts:
key_counts[key_name] += 1
update_heatmap()
except AttributeError:
pass
listener = Listener(on_press=on_press)
listener.start()
存储和加载数据
这里我增加了个数据存储功能,在每次关闭的时候将数据存储到本地json文件中。load_data()
和save_data()
函数处理从文件中读取和写入数据:
python
import json
import os
def load_data():
if os.path.exists('keyboard_data.json'):
with open('keyboard_data.json', 'r') as file:
return json.load(file)
else:
return {}
def save_data(data):
with open('keyboard_data.json', 'w') as file:
json.dump(data, file, indent=4)
使用Matplotlib创建热图
使用matplotlib
来生成我们的热图:
python
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
heatmap = ax.imshow(np.zeros((6, 16)), cmap='hot', interpolation='nearest')
for y, row in enumerate(key_layout):
for x, key in enumerate(row):
ax.text(x, y, key, ha='center', va='center', color='white')
使用Tkinter构建GUI
这里使用了tkinter
来构建一个简单的UI界面。
python
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root = tk.Tk()
root.title("Keyboard Heatmap")
canvas = FigureCanvasTkAgg(fig, master=root)
canvas_widget = canvas.get_tk_widget()
canvas_widget.pack()
root.mainloop()
界面效果如下:
处理程序关闭
为了确保在应用程序关闭时保存数据,我们连接到窗口的关闭事件:
python
def on_close():
save_data(data)
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_close
)
最终效果
最终代码汇总
python
import tkinter as tk
from pynput.keyboard import Listener
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import json
from datetime import datetime
import os
key_layout = [
['Esc', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'PrtSc', 'Scroll', 'Pause'],
['`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'Backsp', '', ''],
['Tab', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', '\', '', ''],
['Caps', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', "'", 'Enter', '', '', ''],
['Shift', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 'Shift', '', '', '', ''],
['Ctrl', 'Win', 'Alt', 'Space', 'Alt', 'Fn', 'Menu', 'Ctrl', '', '', '', '', '', '', '', '']
]
# Initialize key counts, ignoring empty strings
key_counts = {key.lower(): 0 for row in key_layout for key in row if key}
# File for storing the data
data_file = 'keyboard_data.json'
# Load historical data
def load_data():
if os.path.exists(data_file):
with open(data_file, 'r') as f:
return json.load(f)
else:
return {}
# Save data
def save_data(data):
with open(data_file, 'w') as f:
json.dump(data, f, indent=4)
# Current date
current_date = datetime.now().strftime("%Y-%m-%d")
data = load_data()
if current_date not in data:
data[current_date] = {key.lower(): 0 for row in key_layout for key in row if key}
# Setup the matplotlib figure and axis
fig, ax = plt.subplots(figsize=(12, 3))
heatmap = ax.imshow(np.zeros((6, 16)), cmap='hot', interpolation='nearest', vmin=0, vmax=10)
ax.set_xticks([])
ax.set_yticks([])
# Adding key labels to the heatmap
for y, row in enumerate(key_layout):
for x, key in enumerate(row):
if key:
ax.text(x, y, key, ha='center', va='center', color='white', fontsize=8)
# Function to update the heatmap
def update_heatmap():
data_array = np.array([[data[current_date].get(key.lower(), 0) for key in row] for row in key_layout])
heatmap.set_data(data_array)
canvas.draw_idle()
# Key press callback
def on_press(key):
try:
key_name = key.char.lower() if hasattr(key, 'char') and key.char is not None else key.name.lower()
if key_name in data[current_date]:
data[current_date][key_name] += 1
root.after(1, update_heatmap)
except AttributeError:
pass
# Setup the tkinter window
root = tk.Tk()
root.title("Keyboard Heatmap")
# Embed the matplotlib figure in the tkinter window
canvas = FigureCanvasTkAgg(fig, master=root)
canvas_widget = canvas.get_tk_widget()
canvas_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# Start listening to the keyboard in the background
listener = Listener(on_press=on_press)
listener.start()
# Shutdown hook to save data when the program is closed
def on_close():
save_data(data)
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_close)
# Start the tkinter main loop
root.mainloop()