-2

I am reading serial data in while loop, I want to add something in code that if I press t than something should be print otherwise while loop should work. I tried to use readchar, but it wait for me to press some key, I dont want it to wait. I want while loop to work till I press some key

Raspberry pi 2.7

while True:
    data = s.recv(xxx)
    print(data)

    if (x == t)
        print(Hello)
    else:
        continue

it is waiting if i use read char.

jofrev
  • 324
  • 3
  • 11

1 Answers1

0

There are lots of questions already asked on similar topics which would solve your problem. Some of them include the following:

Loubo's "detect key press in python" here on SO, as well as Spae's "Python key press, detect (in terminal, no enter or pause)".

You could use:

import keyboard as kb
num = 0
while True:
    if kb.is_pressed('t'):
        num += 1
    print (num)

Besides that, you have grammar and indentation errors. Please consult this post on the SO Meta so further problems like this do not occur.

peki
  • 669
  • 7
  • 24
  • Hello Maxxy, Thanks alot for your help, I have added this code in my program but now I am getting error (you must be root to use this library on linux) – M.Haseeb Jul 08 '19 at 12:26