简体   繁体   English

如何根据Python中按下的时间使按钮按下产生不同的输出?

[英]How to make a button press have different outputs depending on time pressed in Python?

I am attempting to learn how to program a little. 我正在尝试学习一些编程方法。 I am making this script for my Raspberry Pi, but the concept doesn't have to be specifically for it. 我正在为我的Raspberry Pi编写此脚本,但是该概念不必专门针对它。 I want to have a button press that will execute one of two commands: 我想按下一个按钮,它将执行以下两个命令之一:

If held for one second, then run command A If held for five seconds, then run command B It is okay for command A to run multiple times while waiting for B to register. 如果按住一秒钟,然后运行命令A如果按住五秒钟,然后运行命令B在等待B注册之前,命令A可以多次运行是可以的。

Here is my script, and afterwards I will explain its purpose: 这是我的脚本,然后我将解释其目的:

import RPi.GPIO as GPIO
import uinput
inport time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)

def main():
     events = (
          uinput.KEY_ESC,
          )

     device = uinput.Device(events)

     device.emit(uinput.KEY_ESC, 1) #Press
     device.emit(uinput.KEY_ESC, 0) #Release

Then, here are the two things that I want to have added: 然后,这是我要添加的两件事:

while 1:

     if time.sleep(1)
     if GPIO.input(17)==True:
          main()
          break

while 1:

     if time.sleep(10)
     if GPIO.input(17)==True:
          os.system("sudo reboot")
          break

Essentially, this script will cause the button to have two purposes. 本质上,此脚本将导致按钮具有两个目的。 Pressing it for one second will emulate a keystroke of the key ESC. 按住一秒钟将模拟ESC键的击键。 Pressing it for ten seconds will cause it to reboot the system. 按下它十秒钟将导致它重新引导系统。 How can I have the two happen at the same time? 我怎样才能使两者同时发生? Learning Python is pretty challenging for me, but I've had no programming experience at all until now. 学习Python对我来说是一个很大的挑战,但是到目前为止,我还没有编程经验。

Though, I don't have experience with with Raspberry Pi, I have decided to answer you, since it look like you have been missing attention, from guru. 虽然,我没有使用Raspberry Pi的经验,但我还是决定回答您,因为您似乎已经失去了专家的关注。 With corresponding with this article Buttons and Switches , I think follwed code should work properly: 与本文的“ 按钮和开关”相对应,我认为下面的代码应该可以正常工作:

import os
import time
while True:
    if GPIO.input(17):
    #start counting pressed time
    pressed_time=time.monotonic()
    while GPIO.input(17): #call: is button still pressed
        # just to force process wait
        # may be possible use in this time.sleep(1) but I don't have confidence
        pass
    pressed_time=time.monotonic()-pressed_time
    if pressed_time<5:
        #send corresponding signal, you mentioned to call "main()", ok then:
        main()
    elif pressed_time>=5:
        os.system("sudo reboot")

I can not speak for your special case but in general when you want a button to be pressed and measure the time. 我不能代表您的特殊情况,但总的来说,当您想要按下按钮并测量时间时。 Maybe it is of help. 也许有帮助。

The algorithm is in the comments and I sketch the Python equivalent: 该算法在注释中,我绘制了Python等效代码:

import time
while 1:
    time.sleep(0.001) # do not use all the cpu power
    # make a loop to test for the button being pressed
    if button == pressed:
        when_pressed = time.time()
        while button == pressed:
            # wait until the button is not pressed any more
            time.sleep(0.001) # do not use all the cpu power
        # measure the time
        time_pressed = time.time() - when_pressed
        if time_pressed < too_short:
            continue # pressed too short do not use the other cases
        if 1 < time_pressed < 10:
            pass # pressed more than 1 second but less then 10
        if time_pressed > 10:
            pass # pressed more then 10 seconds 
            # a computer usually uses 6 seconds to wait for the shutdown

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Python与Tkinter结合使用,如何根据选项菜单中选择的选项,使按钮按下做不同的事情? - Using Python with Tkinter, how can I make a button press do a different thing depending on which option is selected in the option menu? 在 Python 的键盘中按下特定字符/数字时如何按下按钮? - How to press a button when a specific character/number is pressed in keyboard on Python? 第二次按下时使按钮的行为有所不同(Python) - Make a button behave differently when pressed a second time (Python) 不同的输出取决于对多个问题的回答(Python) - Different outputs depending on responses to multiple questions (Python) Python:根据按下的按钮更改标签 - Python: Changing label depending which button is pressed 如果按钮也是动态添加的,如何根据按下的按钮在不同的屏幕上动态添加小部件? - How to dynamically add widgets on a different screen depending on which button is pressed if the buttons were added dynamically as well? 如何找出上次python tkinter按下了哪个按钮 - how to find out which button was pressed last time python tkinter 在Python中按下按钮时如何自动更改为其他程序 - How automatically change to different program when a button is pressed in Python 如何使窗口状态空闲直到在python tkinter中按下按钮? - How to make state of window idle until button is pressed in python tkinter? python - 如何制作 function 来检查按钮是否按下了一次或两次? - python - how to make a function to check if button pressed once or twice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM