How can simulate until key down do writing?

I write program to control motor with wiring Pi’s function,in this program do digital write (for example )40 time;
How can simulate write until key down after key up stop writing?in linux

void loop() {
	char ch;
	scanf("%c", &ch);
	if (ch == 77){
		for (int steps = 0; steps < number_of_steps; steps++) {
			/* 01 */
			digitalWrite(motor_pin_1, LOW);
			digitalWrite(motor_pin_2, HIGH);
			delay(step_delay);
			/* 11 */
			digitalWrite(motor_pin_1, HIGH);
			digitalWrite(motor_pin_2, HIGH);
			delay(step_delay);
			/* 10 */
			digitalWrite(motor_pin_1, HIGH);
			digitalWrite(motor_pin_2, LOW);
			delay(step_delay);
			/* 00 */
			digitalWrite(motor_pin_1, LOW);
			digitalWrite(motor_pin_2, LOW);
			delay(step_delay);
		}
	}

	else if (ch == 75){
		for (int steps = 0; steps < number_of_steps; steps++) {
			/* 00 */
			digitalWrite(motor_pin_1, LOW);
			digitalWrite(motor_pin_2, LOW);
			delayMicroseconds(200);
			/* 10 */
			digitalWrite(motor_pin_1, HIGH);
			digitalWrite(motor_pin_2, LOW);
			delayMicroseconds(200);
			/* 11 */
			digitalWrite(motor_pin_1, HIGH);
			digitalWrite(motor_pin_2, HIGH);
			delayMicroseconds(200);
			/* 01 */
			digitalWrite(motor_pin_1, LOW);
			digitalWrite(motor_pin_2, HIGH);
			delayMicroseconds(200);



		}
	}
}

isan:
How can simulate write until key down after key up stop writing?in linux

I don't understand what you want.

The piece of code you posted seems to make a stepper move one way when 'M' is detected and the other way when 'K' is detected.

Wouldn't it be much simpler to have

if (ch == 'M'){

Post your complete code. Not just a snippet.

...R

You'll need to rephrase you question using more than one sentence - you don't explain enough about
your system to make sense of things. Be thorough!

Robin2:
I don't understand what you want.

The piece of code you posted seems to make a stepper move one way when 'M' is detected and the other way when 'K' is detected.

Wouldn't it be much simpler to have

if (ch == 'M'){

Post your complete code. Not just a snippet.

...R

yes

if (ch == 'M'){

can use
and yes stepper move one way when 'M' is detected and the other way when 'K' is detected but for 40 time for example or in loop for ever
now I want to do write until hold 'M' after release 'M' stop writing....
my code as similar as this link http://forum.arduino.cc/index.php?topic=143276.0

isan:
now I want to do write until hold 'M' after release 'M' stop writing....

You can't easily do that with the Arduino Serial Monitor because what you do not want to happen is for a continuous stream of 'M's to be sent to the Arduino.

If you write a simple program on your PC using Python (or your favourite language) it can detect when a key goes down and send (say) an 'M' to start the motor moving and later detect when the key is released and send (say) and 'm' to tell the Arduino to stop the motor.

If you want to use the Serial Monitor I suggest you use 'M' to start the move and 'S' to stop the move.

...R

no I want use C++ / C for this part I should use one key hold and release
can you explain more about this part you said:

it can detect when a key goes down and send (say) an ‘M’ to start the motor moving and later detect when the key is released and send (say) and ‘m’ to tell the Arduino to stop the motor.

isan:
can you explain more about this part you said:

I am assuming you mean that you want to write your PC program using C/C++. I'm afraid I am too lazy for that so I can only give general guidance.

The PC system will have events that trigger when a key is pressed and when it is released. In Python the Tkinter library has an onKeyPress() function. I presume there is an equivalent for C/C++.

...R

I think in c++ kbhit() is use and for linux i found this code:

    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
     
    int kbhit(void)
    {
      struct termios oldt, newt;
      int ch;
      int oldf;
     
      tcgetattr(STDIN_FILENO, &oldt);
      newt = oldt;
      newt.c_lflag &= ~(ICANON | ECHO);
      tcsetattr(STDIN_FILENO, TCSANOW, &newt);
      oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
      fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
     
      ch = getchar();
     
      tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
      fcntl(STDIN_FILENO, F_SETFL, oldf);
     
      if(ch != EOF)
      {
        ungetc(ch, stdin);
        return 1;
      }
     
      return 0;
    }

Is it work for me??

Other Way ,how can get how long key hold;wiringPi has function millis(); with this function can get how long any key hold??

 key=getch();
    keyholdTime=millis();
    if(millis()<1000){

    //do some thing
    }
    else {
     // do some thing
    }

The code in Reply #8 looks like it is Arduino code. You need PC code to deal with your problem and, as I said, I do not know enough about C/C++ on a PC to help you. I'm sure Google will have 1000s of answers.

And keyDown and keyUp do not depend on timing, they are events. Google "C++ keydown event" and see what you get

...R