dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Getting things to move around

  1. #1
    Join Date
    Sep 2007
    Posts
    100

    Getting things to move around

    I have to do a game for my class and we are required to take input from the keyboard in order to move things around. Mainly we will be using the arrow keys to shift an object to the left, right, and down.

    Does anyone a function that will do this?

    I was researching and came across GetAsyncKeyState, would this be a plausible choice and how can I implement it in code?

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Getting things to move around

    I assume you're working in Windows, since GetAsyncKeyState is a windows function (this is key to establishing context for an answer).

    Typically, though, the method used is to respond to WM_KEYDOWN, WM_KEYUP or related (WM_SYSKEYxx) messages.

    The GetAyncKeyState is, well, asynchronous. It has to be 'polled' - that is, repeated at timed intervals in order to catch transitions in the state of a key.

    The message paradigm is better in that you receive a message for each of the key operations (one message for each key press or release).

    There is no 'function' to perform here. This is more of a question of design or architecture. The answer requires even more context from you, because the means are different if you're using MFC or writing WinAPI code (or something else).

    If you research key message processing within the context of your framework or API, you'll end up discovering the related code design and possibly the sample code that gives an otherwise completed example.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  3. #3
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: Getting things to move around

    Hello,

    A similar problem where images are moves using mouse is dealt in the FAQ How do I drag an image?. Moving things using keyboard is much simpler, where one has to handle WM_KEYUP / WM_KWYDOWN messages.

    Regards,
    Pravin.
    Let me know if I have helped by rating this post

    Recent FAQs

    Drag an image
    Area of a window exposed on desktop
    Display rotated bitmap

  4. #4
    Join Date
    Mar 2007
    Posts
    39

    Re: Getting things to move around

    Just to add another possible solution to this, if you have the time and interest, I would suggest taking a look at DirectInput. It is pretty straightforward and easy to use, and even in tools or utilities made in MFC I often use it instead of windows messages for user input.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)