VaFancy
VaFancy

Reputation: 145

How to break this loop in Python by detecting key press

from subprocess import call
try:
    while True:
        call (["raspivid -n -b 2666666.67 -t 5000 -o test.mp4"],shell=True)
        call (["raspivid -n -b 2666666.67 -t 5000 -o test1.mp4"],shell=True)
except KeyboardInterrupt:
    pass

I plan to make it breaking loop while I am pressing any button. However I tried lots of methods to break the and none of them worked.

Upvotes: 2

Views: 41355

Answers (5)

Berni Gf
Berni Gf

Reputation: 181

This is the solution I found with threads and standard libraries

Loop keeps going on until one key is pressed
Returns the key pressed as a single character string

Works in Python 2.7 and 3

import thread
import sys

def getch():
    import termios
    import sys, tty
    def _getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
    return _getch()

def input_thread(char):
    char.append(getch())

def do_stuff():
    char = []
    thread.start_new_thread(input_thread, (char,))
    i = 0
    while not char :
        i += 1

    print "i = " + str(i) + " char : " + str(char[0])

do_stuff()

Upvotes: 0

nvd
nvd

Reputation: 3371

Use a different thread to listen for a "ch".

import sys
import thread
import tty
import termios
from time import sleep

breakNow = False

def getch():

    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)

    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

    return ch

def waitForKeyPress():

    global breakNow

    while True:
        ch = getch()

        if ch == "b": # Or skip this check and just break
            breakNow = True
            break

thread.start_new_thread(waitForKeyPress, ())

print "That moment when I will break away!"

while breakNow == False:
    sys.stdout.write("Do it now!\r\n")
    sleep(1)

print "Finally!"

Upvotes: 4

ederollora
ederollora

Reputation: 1181

You could try this:

try:
    while True:
        do_something()
except KeyboardInterrupt:
    pass

Taken from: here

Upvotes: 2

anon582847382
anon582847382

Reputation: 20351

You want your code to be more like this:

from subprocess import call

while True:
    try:
        call(["raspivid -n -b 2666666.67 -t 5000 -o test.mp4"], shell=True)
        call(["raspivid -n -b 2666666.67 -t 5000 -o test1.mp4"], shell=True)
    except KeyboardInterrupt:
        break  # The answer was in the question!

You break a loop exactly how you would expect.

Upvotes: 8

Adrian B
Adrian B

Reputation: 1631

try this:

from subprocess import call
    while True:
        try:
            call (["raspivid -n -b 2666666.67 -t 5000 -o test.mp4"],shell=True)
            call (["raspivid -n -b 2666666.67 -t 5000 -o test1.mp4"],shell=True)
        except KeyboardInterrupt:
            break
        except:
            break

Upvotes: 0

Related Questions