0

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.

1
  • Could you please mention your OS? Commented Feb 14, 2013 at 7:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response. This has been helpful. I've been using a command line like:
if msvcrt.kbhit() and msvcrt.getch() == chr(27): continue This is helpful, but now I have to figure out the correct placement within my program such that it will allow a user to continue to the next m in measurement at any time the program is running. I'm guessing I have to brush up more on control/flow of python coding. Any helpful hints on this is greatly appreciated. Thanks again.
Is your current code sample a good example of what is happening? Is there a call to SciExpMeasure() for each measurement, and once that's done it moves straight onto the next one? Is SciExpMeasure() time consuming? Are there any other breaks in your code where the user has to input something?
Sorry it's been awhile for me to get back to everyone (I've been working on finishing up my PhD). I appreciate your advice and am diving back into developing this code more. I'll give updates on how I implement this as I continue on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.