Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium



Posted on 2003-03-24
Medium Priority
Last Modified: 2008-02-26
I am writing a simple openGL program and I am using the the 'r' key as a toggle to spin the head of an object. Is there a way to use the 'r' key as a true toggle so that the head will spin continually with one tap of the key and spin until the key is pressed again?

Here is how I have it setup now:

void keyboard (unsigned char key, int x, int y)
     switch (key)
     case 'r':
          head = (head + 10) % 360;  //update global variable day
          glutPostRedisplay ();  //force a call to display

     case 'R':
          head = (head + 10) % 360;  //update global variable head
          glutPostRedisplay ();  //force a call to display

Question by:Kirby27

Expert Comment

ID: 8198934
Spawn a timer when the key is pressed.  If the timer is already running, then kill the timer.  

When the timer fires it executes your spin code.  This will let one keypress spin it forever at whatever rate your timer fires until the key is pressed again.


Author Comment

ID: 8200015
Thanks for the input but this is my very first attempt with openGL and you'll have to be more specific.

Accepted Solution

ivec earned 75 total points
ID: 8201128
Here's one way to do it.
First add the following constant, global variable and function:

enum { kTimerDelay = 100; };
int headStep = 0; // angle increment at each iteration

void timerFunc(int val)
  // update the head (more params could be added)
  head = (head + headStep) % 360; // kept as is

  glutPostRedisplay ();  // request redisplay

  // schedule the next callback
  glutTimerFunc( kTimerDelay , &timeFunc );

Right after your glut initialization code, add a call to:
  glutTimerFunc( kTimerDelay , &timerFunc );
to initiate the repeated timer.

Then update your keyboard callback as follows:
  switch( key )
    case 'r': case 'R':  // can be just combined this way
      if( headStep == 0 )
        headStep = 10;
        headStep = 0; //NB: instead, could inc/dec-rement.

This should do...

Expert Comment

ID: 8202449
What programming program?? VC++?

Expert Comment

ID: 8202674
I suggest an easier solution without using timer. I guess you have an infinite loop in your programm which draws a frame and asks for keyboard input. Make another variable like

    int incHead = 0;

Change your keyboard handler so it will operate on the new variable:

    case 'r':
         incHead = 10;

    case 'R':
         incHead = 0;

And change you infinite loop to contain the following somewhere at the end:

    head += incHead;
    head %= 360;

That means that you'll keep spinning each frame all the time while your incHead is 10 and stop when it's 0.
By changing the 10 to something else you change the speed of the rotation.

Hope it helps.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question