Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Keyboard and Game!

Keyboard and Game!

Scheduled Pinned Locked Moved C / C++ / MFC
helpgame-devquestionannouncement
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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

    C U P 3 Replies Last reply
    0
    • L Lost User

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • L Lost User

        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

        U Offline
        U Offline
        Ulf Ohlen
        wrote on last edited by
        #3

        One way (in MFC) is to override CWinApp::OnIdle() and use GetKeyState() to check if a key is pressed. -------------- "Aagh!! I'm a victim of a Random Act of Management!"

        1 Reply Last reply
        0
        • L Lost User

          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

          P Offline
          P Offline
          Pavlos Touboulidis
          wrote on last edited by
          #4

          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;)

          1. One way would be to use DirectInput (part of DirectX). I haven't used it, but I'm sure it won't be hard.
          2. 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.
          3. 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().
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups