You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派5 Bookworm系统Python应用适配触摸屏虚拟键盘问题求助

Python应用集成虚拟键盘问题记录(树莓派5 + Raspberry Pi OS Bookworm)

matchbox-keyboard

  • 全屏模式下存在Bug:必须用鼠标点击一次虚拟键盘才能正常使用
  • 弹窗弹出时,触摸屏无法点击ok按钮,但可以关闭弹窗

复现Bug的简易脚本:

import tkinter as tk
import subprocess
import psutil

def open_virtual_keyboard(event):
    global keyboard_process
    if keyboard_process is None or keyboard_process.poll() is not None:
        keyboard_process = subprocess.Popen(['matchbox-keyboard'])

        root.after(100, lambda: event.widget.focus_force())
        print("Opening a virtual keyboard")


def close_virtual_keyboard(event=None):
    global keyboard_process
    root.after(100, check_focus)
    print("Closing the virtual keyboard")

def check_focus():
    global keyboard_process
    focused_widget = root.focus_get()
    if not isinstance(focused_widget, tk.Entry):
        if keyboard_process is not None:
            keyboard_process.terminate()
            keyboard_process = None

def on_closing():
    close_virtual_keyboard()
    for proc in psutil.process_iter(['name']):
        if proc.info['name'] == 'matchbox-keyboard':
            proc.terminate()
    root.destroy()

def handle_click(event):
    if not isinstance(event.widget, tk.Entry):
        close_virtual_keyboard()

keyboard_process = None

root = tk.Tk()
root.title("Virtual keyboard App")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

# BUG
root.attributes("-fullscreen", True)
root.geometry(f"{screen_width}x{screen_height}")
root.update()
# BUG

entry1 = tk.Entry(root)
entry1.pack(padx=10, pady=10)

entry2 = tk.Entry(root)
entry2.pack(padx=10, pady=10)

for entry in (entry1, entry2):
    entry.bind("<FocusIn>", open_virtual_keyboard)
    entry.bind("<FocusOut>", close_virtual_keyboard)

root.bind("<Button-1>", handle_click)

root.protocol("WM_DELETE_WINDOW", on_closing)

root.mainloop()

wvkbd-mobintl

应用处于全屏模式时,无法将虚拟键盘置于前端,尝试xdotool等工具均无效。

onboard

无法控制键盘的显示与隐藏,尝试使用dbus-send也未能解决。

最终解决方案与Bug原因

最终选择继续使用matchbox-keyboard,运行以下配置脚本完成触摸屏设置:

xinput list
read -p "Enter the device ID of your touchscreen: " device_id

xinput set-prop $device_id "libinput Tapping Enabled" 1

sudo mkdir -p /etc/X11/xorg.conf.d
sudo tee /etc/X11/xorg.conf.d/40-libinput.conf > /dev/null <<EOF
Section "InputClass"
    Identifier "libinput touchscreen catchall"
    MatchIsTouchscreen "on"
    MatchDevicePath "/dev/input/event*"
    Driver "libinput"
    Option "Tapping" "on"
EndSection
EOF

xinput_calibrator --device $device_id

Bug触发原因:同时连接鼠标和触摸屏时会出现问题,断开鼠标仅使用触摸屏即可解决。


内容的提问来源于stack exchange,提问作者Noy.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 16:04:53