python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python键盘使用次数热力图

基于Python绘制键盘按键使用次数的热力图

作者:大鲸鱼crush

这篇文章主要为大家详细介绍了如何使用Python绘制键盘按键使用次数的热力图,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

前言

刷掘金时候偶然看到了记录下今天敲了多少下键盘,但是博主只是展示了统计出的键盘使用次数,因此想到在这个idea上小改一下,实现键盘按键的热力图展示。

思路分析

具体实现

设置环境

首先安装一些Python库。pynput库将允许我们捕获键盘事件,matplotlib将使我们能够创建热图可视化,tkinter将用于构建GUI。:

pip install pynput matplotlib tk

定义按键字典

这里我们需要预先使用字典存储键盘中的全部按键的名字。

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)。以下是如何为键盘事件设置监听器:

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()函数处理从文件中读取和写入数据:

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来生成我们的热图:

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界面。

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()

界面效果如下:

处理程序关闭

为了确保在应用程序关闭时保存数据,我们连接到窗口的关闭事件:

def on_close():
    save_data(data)
    root.destroy()

root.protocol("WM_DELETE_WINDOW", on_close

)

最终效果

最终代码汇总

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()

到此这篇关于基于Python绘制键盘按键使用次数的热力图的文章就介绍到这了,更多相关Python键盘使用次数热力图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文