我想运行一个命令,直到放开钥匙

发布于 2025-02-06 16:28:04 字数 332 浏览 1 评论 0 原文

我已经在RC汽车上编码了一些电动机,并复制了使用诅咒模块的一些代码。在我控制汽车时,它运行了相同的命令,例如向前或离开,直到我给它另一个命令,例如右侧。我希望它在释放钥匙时停止移动。我该怎么做?

try:
shell = curses.initscr()
shell.nodelay(False)
curses.noecho()


while True:
    key = shell.getch()
    
    if key ==  65:
        right()
        time.sleep(0.1)

这就是我一直使用的代码,因此对任何帮助都将不胜感激。

I have coded some motors on an RC car and copied some code which uses the curses module. At the moment when I control the car it runs the same command such as forwards or left until I give it another such as right. I want it to stop moving when I release the key. How would I do that?

try:
shell = curses.initscr()
shell.nodelay(False)
curses.noecho()


while True:
    key = shell.getch()
    
    if key ==  65:
        right()
        time.sleep(0.1)

That's the code I've been using so any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2025-02-13 16:28:04

您描述它的方式是您正在寻找一种确定“不按密钥”的方法。
AS 真实。

之后,您可以将“ -1”案例分配给停止命令(无论您的情况如何)。

shell = curses.initscr()
shell.nodelay(True)  # <- make getch non-blocking
curses.noecho()


while True:
    key = shell.getch()
    
    if key == -1:  # <- no key pressed
        stop()  # <- appropriate stop functionality
        time.sleep(0.1)

The way you describe it you are looking for a way to determine "no key pressed".
As documented, getch returns -1 in "no delay mode", i.e. setting shell.nodelay() to True.

Afterwards, you can assign the "-1" case to a stopping command (whatever that is in your case).

shell = curses.initscr()
shell.nodelay(True)  # <- make getch non-blocking
curses.noecho()


while True:
    key = shell.getch()
    
    if key == -1:  # <- no key pressed
        stop()  # <- appropriate stop functionality
        time.sleep(0.1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文