Sign in with
Sign up | Sign in
Your question

Reading key problem

Last response: in Linux/Free BSD
Share
July 21, 2010 9:56:14 AM

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

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

{


exit(-1);

}

//puts("after read");

switch(c)

{

case KEYCODE_L:
.........
}
}

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

More about : reading key problem

July 21, 2010 6:31:27 PM

The correct way is to create two threads, one waiting on a key-press event and the other (probably a lower priority) free running.
m
0
l
July 21, 2010 8:32:55 PM

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 key....as 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..
thanks
m
0
l
Related resources
July 21, 2010 9:02:58 PM

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.
m
0
l
July 28, 2010 7:24:42 AM

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
m
0
l
!