Raspbian 睡觉时检测键盘输入

Raspbian detect keyboard input while sleeping

我在 Raspbian 中有一个 python 脚本,它无限循环并在主函数执行之间休眠 15 分钟。 Main 是线程化的,但通常需要 3 秒才能 运行。我不希望任何代码中断,及时进行下一次调用。在睡眠期间,我想检测按键 'r' 以选择性地启动另一个功能,也可能是线程化的。

我尝试了 pynput 模块,但出现了奇怪的暂停,这似乎与我需要的线程和使用 VNC 签入有关。我还尝试了线程内的常规旧输入,但没有用户输入就无法结束线程。

键盘模块在 Windows 中运行良好,但在 Raspbian 中未检测到按键。我正在使用 sudo "sudo python3 scriptname.py" 运行ning 脚本我真的不在乎是否通过使用线程即时进行密钥检测。如果需要,我可以中断我的睡眠周期,在一分钟左右后调用该函数。我就是停不下来。

import time
import keyboard
import threading

def mainFunc():
    print('does stuff')

def keyFunc():
    print('do key detect stuff')

while True:
    t1 = threading.Thread(target=mainFunc)
    t1.start()
    time.sleep(60)
    t1.join()
    keyboard.on_press_key("r", lambda _:keyFunc())
    for _ in range(14):
        time.sleep(60)
    keyboard.unhook_all()

我认为可能有用的一种方法是从日期时间对象创建一个时间跨度持续时间并将其用于您的主循环。

这是我创建的示例。希望对你的 pi raspbian 脚本有帮助。

from datetime import *
import time
import keyboard
import threading

#every 3 second function
x_start = datetime.now()
xSleep=3
def mainFunc():
    newTime = datetime.now()
    if(getTimeDiff(x_start,newTime)>xSleep):
        setXStart(datetime.now())
        return True
#every 15 minute function
y_start = datetime.now()
ySleep=900
def fifteenMinuteFunction():
    newTime = datetime.now()
    if(getTimeDiff(y_start,newTime)>ySleep):
        setYStart(datetime.now())
        return True 
#key press function (from pressing p)
def keyFunc():
    print("do keystuff")
#calculate time difference since last call
def getTimeDiff(start, end):
    span = end - start
    #convert to string
    spanStr = str(span)[0:7]
    hours   = spanStr[0:1]
    minutes = spanStr[2:4]
    seconds = spanStr[5:7]
    #convert to rounded int (seconds)
    seconds = int(seconds) + (int(minutes) * 60) + (int(hours) * 60 * 60)
    return int(seconds)
#set new start time for 3 second function
def setXStart(newTime):
    global x_start 
    x_start = newTime
#set new start tiem for 15 minute function
def setYStart(newTime):
    global y_start 
    y_start = newTime

#bind keyboard keys
keyboard.on_press_key("p", lambda _:keyFunc())                  # p do stuff function

while True:
    if keyboard.is_pressed('x'):                                # exit keybind to x
        exit()
    elif mainFunc():
        print("Doing thingy every "+str(xSleep)+" seconds")     # 3 seconds
    elif fifteenMinuteFunction():
        print("Doing thingy every "+str(ySleep)+" seconds")     # 15 mins
    else:
        time.sleep(.1)                                          

输出:

do keystuff
doing thingy every 3 seconds
do keystuff
doing thingy every 3 seconds
doing thingy every 3 seconds
....
doing thingy every 900 seconds
...
def inputDaemon():
    while True:
        x = input('r to reload')
        if x == 'r': doTheFunc()

inpD = threading.Thread(target=inputDaemon)
inpD.daemon = True
inpD.start()

我最终使用的是什么,它运行良好。