Keyboard and Game!
-
I'm trying to make a game, and every thing works fine! But the keyboard!... If i press the key, it doesnt act all the time. Like it should in a game. When i move my ship, by pressing the button, my ship only move a litle, then after a second it moves full. But i want to get around this problem. Like, i want it to move when i hold the key, and stop when i release it. I think whats wrong is i use the windows keyboard input to Move my ship(The keyboard repeat delay). Anyone got a theori to my problem, and or a fix? Thanks
-
I'm trying to make a game, and every thing works fine! But the keyboard!... If i press the key, it doesnt act all the time. Like it should in a game. When i move my ship, by pressing the button, my ship only move a litle, then after a second it moves full. But i want to get around this problem. Like, i want it to move when i hold the key, and stop when i release it. I think whats wrong is i use the windows keyboard input to Move my ship(The keyboard repeat delay). Anyone got a theori to my problem, and or a fix? Thanks
Are you using DirectX, OpenGL or just GDI ? No matter what, you should be using DirectInput for your keyboard reading. Christian Secrets of a happy marriage #27: Never go to bed if you are mad at each other. It's more fun to stay up and fight.
-
I'm trying to make a game, and every thing works fine! But the keyboard!... If i press the key, it doesnt act all the time. Like it should in a game. When i move my ship, by pressing the button, my ship only move a litle, then after a second it moves full. But i want to get around this problem. Like, i want it to move when i hold the key, and stop when i release it. I think whats wrong is i use the windows keyboard input to Move my ship(The keyboard repeat delay). Anyone got a theori to my problem, and or a fix? Thanks
-
I'm trying to make a game, and every thing works fine! But the keyboard!... If i press the key, it doesnt act all the time. Like it should in a game. When i move my ship, by pressing the button, my ship only move a litle, then after a second it moves full. But i want to get around this problem. Like, i want it to move when i hold the key, and stop when i release it. I think whats wrong is i use the windows keyboard input to Move my ship(The keyboard repeat delay). Anyone got a theori to my problem, and or a fix? Thanks
Most games have a "main loop", in conjuction with some timer code to make it run at a constant rate. You can also make a game behaving like a standard application - I mean make it event driven. Of course, you can combine these two... Here is some pseudocode for the 1st and 2nd type: Main Loop
do
{
if (is_key_down(LEFT_ARROW_KEY))
player.MoveLeft();
if (is_key_down(RIGHT_ARROW_KEY))
player.MoveRight();draw_background();
draw_sprites();
draw_info();timer_sync_code();
}while(!is_key_down(ESCAPE));
Event Driven Here, the main loop is the standard message dispatcher
MyApp::OnTimer()
{
draw_background();
draw_sprites();
draw_info();
}MyApp::OnKeyPressed(int key)
{
switch(key)
{
case LEFT_ARROW_KEY:
player.MoveLeft();
break;
case RIGHT_ARROW_KEY:
player.MoveRight();
break;
etc, etc, etc
}
}The reason you have this problem is because windows sends WM_CHAR and WM_KEYDOWN messages using the keyboard delay and repeat rate. If it wasn't like that, it would be impossible to typeeeeeeeeeeeeee;)
- One way would be to use DirectInput (part of DirectX). I haven't used it, but I'm sure it won't be hard.
- If you're using the game loop method, you could replace the pseudo-is_key_down functions with GetKeyState() (or GetAsyncKeyState()). These functions take the virtual key you're interested in as parameter and return the status of that key. If I remember correctly, you should "and" the return value with 0x8000 to get the pressed/unpressed state.
- If you're using the OnTimer() and OnKey() method, you can remove the OnKey() function and use what I wrote above in (2), inside OnTimer().