Thread: problem with keyboard input

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    problem with keyboard input

    Hello!
    I'm programming a game. I use the keyboard input of my arrow keys to move the character in my game. It works, but there's a small defect.

    When pressing a button and not releasing it, the character steps one step forward, then stops for a very short time and then starts moving smoothly. If I don't release the key, how can it stop? Why doesn't it start moving smoothly right after pushing the key?

    I'm quite sure the problem is the input, not my, because changing the view has the same defect although the systems are different.

    I use standard Windows method for getting input (WM_KEYDOWN). I also tried AsyncKeys, but it made no difference. Should I try DirectInput too or is there some simpler solution. It seems to me that the problem I have should not be too difficult and is caused because of my lack of knowledge about input.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    One way of doing it is having a boolean to keep track if left key or right key is being pressed, set true when WM_KEYDOWN is sent and the key is the arrow key, set false when WM_KEYUP is sent.

    Then in your gameupdate you move the character if the bool is true otherwise it stays.

    The reason it stutters is (if i remember correctly) because windows has a small timeout betwean a WM_KEYDOWN is sent and it recognizes the key as being held down.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Thanks, problem solved!

    I have another problem however that fits into the same category, so I won't make a new topic for it.

    When I hold a key down for some time and then press another key, it starts working even if I still hold down the previous key. Is there a way to make them work together? I need it because it is quite usual in games, that you hold down the moving key and sometimes press left or right keys to change direction without releasing the moving key. When I do it, the moving stops. How to to it right?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To detect keys being down in combination, you need to "remember" which ones you have seen down and up events from. So you have some sort of map (array etc), when you get a "keydown", then set it to "down", and when you get "key up", you reset it to up.

    Then you can check in your map what keys are in what state and do the appropriate things.

    This is how ctrl, alt and such keys work in Windows normally - the state of these keys are "stored" away, and kept track of whilst other keys are being pressed and released.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Or use DirectInput ( so much better than using Windows Messages ) it is pretty simple to get right. It is explained pretty good in the DirectX SDK documentation.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Problems solved, thanks!

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Quote Originally Posted by Raigne View Post
    Or use DirectInput ( so much better than using Windows Messages ) it is pretty simple to get right.
    According to the DirectInput documentation, if you are using a keyboard for typing or a mouse for navigation (ie. all games that don't use joysticks or controllers) you should use Windows Messages. It certainly is easier to use though. I learned this a while back and still haven't talked myself into pulling DI out and putting Windows Messages back.

    http://msdn.microsoft.com/en-us/libr...06(VS.85).aspx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  2. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 12:32 PM
  3. Need help with keyboard input
    By wiramu in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2003, 02:44 PM
  4. FAQ Keyboard Input ? (C++)
    By Malikive in forum FAQ Board
    Replies: 6
    Last Post: 11-07-2001, 09:30 PM
  5. Keyboard input ?
    By Malikive in forum Game Programming
    Replies: 4
    Last Post: 11-06-2001, 11:14 PM