user2070870
user2070870

Reputation: 101

if keyboard sequence pressed, continue loop in python

I have a Python script which carries out a for loop which runs modules used to carry out a scientific experiment measuring different physical phenomena. I would like to create a keyboard sequence recognized by my program which will continue the for loop (skip the current measurement) and start the next sequence of measurements.

    measurement = EXPERIMENT()
    for m in measurement:

        SciExpMeasure(value1,value2, value3)

I would like for a user to be able to enter some keyboard sequence (e.g. 'Ctrl+n') such that

    measurement = EXPERIMENT()
    for m in measurement:
        if keyboardSequence: continue

        SciExpMeasure(value1, value2, value3)

The idea if for a user monitoring the data acquisition to be able to skip a bad measurement and continue to the next one. I've looked into 'press any key to continue' examples and don't think that those options will work for me in this application since they seem to WAIT for 'any to to be pressed' before continuing.

Thanks in advance.

Upvotes: 0

Views: 586

Answers (1)

Pep_8_Guardiola
Pep_8_Guardiola

Reputation: 5252

Take a look at the Console I/O section of msvcrt. Specifically:

msvcrt.kbhit():

Return true if a keypress is waiting to be read.

and then msvcrt.getch():

Read a keypress and return the resulting character. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed. If the pressed key was a special function key, this will return '\000' or '\xe0'; the next call will return the keycode. The Control-C keypress cannot be read with this function.

Upvotes: 1

Related Questions