簡體   English   中英

python3 linux - 檢測與輸入root一起按下的鍵盤鍵

[英]python3 linux - detect keyboard key pressed withput root

我試圖檢測鍵盤按鍵但沒有root - 我找到了庫鍵盤,但它沒用(因為它需要root)

我發現一些網站說它不需要root,但它絕對需要。

我試過這段代碼

import keyboard
def key_press(key):
    print(key.name)
keyboard.on_press(key_press)

但就像我說的 - 它需要root

...
line 174, in ensure_root
raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.

我需要沒有root,因為當然是第一個安全性,因為我稍后會添加pygame - 你不能用root運行gui

我也嘗試搜索其他庫,但是我沒有找到任何用於檢測按鍵的內容 - 有按鍵的pykeyboard,但沒有檢查按鍵是否被按下

在本模塊的已知限制下提到了它

為了避免依賴X,Linux部分讀取原始設備文件( /dev/input/input* ),但這需要root。

可以從源代碼( _nixkeyboard.py )確認。

def ensure_root():
    if os.geteuid() != 0:
        raise ImportError('You must be root to use this library on linux.')

device = None
def build_device():
    global device
    if device: return
    ensure_root()
    device = aggregate_devices('kbd')

def init():
    build_device()
    ...

def listen(callback):
    build_device()
    ...

def write_event(scan_code, is_down):
    build_device()
    ...

請注意,在做任何動作之前build_device被調用時,它調用ensure_root檢查調用進程的有效用戶ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM