Keyboard Interaction

I have a set of flippers ( like in a pinball game ) im rotating them about the origin etc, i would like to have it so as long as i hold down a key for example the ‘z’ key the flipper will rotate up, and when i release it, it will rotate back down to where it originally was. But as far as i see you cant do this with the switch statement the is ussually used. Thank for your help!

Did you check out my other post to you?
If you take a look at how I handle objects in that code, it would be easy to add the flipper function you asked about.

Use the switch statment to set what is called a state varible.
example:
int flipper_state; state = 0 flipper down; state = 1 flipper up;
int flipper_position; 0 = flipper at rest; 10 = flipper at full up position.
int key_state; key not pressed = 0; key pressed = 1;

  1. When a flipper key is pressed the flipper position starts to move from 0 to 10.
  2. When the flipper position reach’s ten, we set the flipper state to one.
  3. As long as a key is pressed the flipper stay’s in state = 1
  4. After key is released, we start changing flipper_position from 10 to 0.
  5. When flipper position has reached zero, then we change flipper state to zero.

Let me know if you understand it?

Originally posted by DragonXTC:
I have a set of flippers ( like in a pinball game ) im rotating them about the origin etc, i would like to have it so as long as i hold down a key for example the ‘z’ key the flipper will rotate up, and when i release it, it will rotate back down to where it originally was. But as far as i see you cant do this with the switch statement the is ussually used. Thank for your help!

To nexsome:

I understand most of what you say, but my question is, how do i tell what the key_state is ? a switch statement will take it one time correct? meaning it will rise up, set flipper_state to 1, then descend and set flipper_state to 0? do you have a while of some sorts checking the key_state? Thanx for your help

That is a problem with GLUT is does not give you the keyboard state, other then a key was pressed. Getting the keyboard state has been asked on this forum before, I do not have the answer myself, but will check on getting the key press state up/down.

The other option would be to use the mouse button’s for your flippers, glut will get the state of button up/down.

Originally posted by DragonXTC:
[b]To nexsome:

I understand most of what you say, but my question is, how do i tell what the key_state is ? a switch statement will take it one time correct? meaning it will rise up, set flipper_state to 1, then descend and set flipper_state to 0? do you have a while of some sorts checking the key_state? Thanx for your help[/b]

You could always store the states yourself…

When the key is pressed…

bKeyDown = true;

When the key is released…

bKeyDown = false;

Your update method, or your rendering method, or wherever you need to see what the state of the key is, you just check that variable. Nice and simple. You’d have to have a separate variable for each key you need to track, though.

If you are using windows, you can get the key-state with the win32 function, GetAsyncKeyState ( VK_UP ) <-- for example.

Not sure about glut however or whether this function will be available with a console app (should be!). Be aware that the Async nature of this call will mean your application (if running in the background) will still detect and respond to keyboard messages. You can stop this by checking the current window HWND is = your window HWND (not sure even if you can get the hWnd from glut).

Anyway, hope this helps.