Reading key problem

Hello! hope everyone is fine.. I use code in c++ in UBUNTU (Linux) like below to read arrow keys for my functionality:

if(read(kfd, &c, 1) < 0)




//puts("after read");




but this captures Arrow keys as long as I press them...if I dont press any key , execution is blocked at read()...what I want is if I press keys it should execute their case in switch...but if I do nothing then it should execute some other code let say to call any other function...can anyone help please(need code in c++ for Ubuntu)...thanks in advance.
safdar from Austria
4 answers Last reply
More about reading problem
  1. The correct way is to create two threads, one waiting on a key-press event and the other (probably a lower priority) free running.
  2. one thread will read the key..its okay...but the other thread will run freely and will call the function all the time no matter whether I press the key or not...My problem is that program should read the arrow keys and send commands Forward,Backward,Up and Down depending upon the key...but when I dont press any key it must send command STOP.
    if one thread for all four movements...and one thread for STOP then there will be no motion because of freely sending Stop command by the second thread....
    Simply saying...I have to control a Robot...I say code must send Stop command all the time but should also read the soon as key is pressed it should leave the stop and should give motion command, and when key is released it should again start sending Stop command and also should wait for the arrow key press...
    may this problem be fixed..
  3. In that case, try using some form of timer, reset/restart it on each control key, if it times-out (due to no control keys) send a stop.
  4. yes this idea was there in my mind but I cannot understand when to send STOP command because the execution of program blocks on read(). How to bypass read() and call the stop command. Can you please send me little code so that I can understand. Thanks for your guidance.
    Safdar from Austria
Ask a new question