troubleshooting Question

Understanding Keyboard Events in the Win32 message loop

Avatar of Sandra-24
Sandra-24 asked on
C++
9 Comments1 Solution1005 ViewsLast Modified:
I've got a few questions about windows keyboard messages.

What I want to do is use the keys WASD to scroll.

W for up, D for right, S for down, A for left.

I also want to be able to press up/down and left/right keys at the sametime for diagonal scrolling.

I thought this could be implemented roughly like this:

case WM_KEYDOWN:
      switch(key)
      {
      case IS_D: // d is pressed
            while(KEY_D_STILL_DOWN()) // d is still down
            {
                  x += 4; // move 4 pixels right
                  if(KEY_W_DOWN()) // w is down as well
                        y -= 4; // move 4 pixels up
                  else if(KEY_S_DOWN()) // or s is down as well
                        y += 4; // move 4 pixels down
                  // the routine I call in WM_PAINT, uses x and y to paint the correct region
                  Paint();
            }

I could of course call TranslateMessage() instead and handle WM_CHAR, but I don't like that since by the time I handle the WM_CHAR message, KEY_D_STILL_DOWN() might return true and nothing will happen. I'm open to suggestions though.

The part I don't know how to do is KEY_D_STILL_DOWN() and KEY_W_DOWN(). I also have no idea how to test IS_D, since the WM_KEYDOWN message returns a virtual keycode.

How could I do this?

Thanks,
-Sandra

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros