Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using GetKeyboardState

Posted on 1998-11-14
8
Medium Priority
?
691 Views
Last Modified: 2013-11-20
Hi,

How can I use the GetKeyboardState function?  I mean, I know how to get the array filled with information, but I don't know how to interpret it.  Can someone give me an example of how I can, for example, get whether or not any of the arrow keys are pushed down?

Thank you very much for you help.
0
Comment
Question by:a121496
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 1324697
Well, you could use it in this way:

BYTE aKeyState[ 256];

if ( GetKeyboardState( aKeyStaze))
{
 if ( 0x80000000 & aKeyState [ V_UP])
 {
   // 'cursor up' is pressed...
 }
}

Or you alternatively could use

if ( 0x80000000 & GetKeyState( VK_UP))
{
   // 'cursor up' is pressed...
}

(0x80000000 represents the bit mask for the highest bit in the highest byte. If this is set to '1', the key is pressed)
0
 
LVL 86

Expert Comment

by:jkr
ID: 1324698
Ooops sorry, a typo ;-)

if ( GetKeyboardState( aKeyStaze))

should of course read

if ( GetKeyboardState( aKeyState))

0
 

Author Comment

by:a121496
ID: 1324699
Thank you, but I have one more question now, how can I find out which elements in the array correspond to things like the letters on the keyboard ("asdf...") etc?  What numbers are associated with them?  If you answer this as well (I'm sure you can), I will give you an 'A'.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:jkr
ID: 1324700
That's easy ;-)
the 'VK_ *' constants can be used as indices to the array, and the ASCII codes for the characters correspond directly to the virtual keycodes:
(from 'winuser.h')
/* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
/* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */

0
 

Author Comment

by:a121496
ID: 1324701
Huh.  That does not seem to work.  I have included "stdafx.h" so winuser should be included too, but the compiler says that those constants are undefined

"O:\stuff\newgame\AsteroidDlg.cpp(256) : error C2065: 'VK_A' : undeclared identifier"

Ideas?
0
 

Author Comment

by:a121496
ID: 1324702
I also tried using the character constants like so:
keys['a']

but the if statment I'm using isn't called when the a key is pressed.

Things I checked:
1) The obvious: Caps lock is off.

2) I know the code is right.  Here it is though:
  if (0x80 & keys['a'])  // 'a'
  { m_Players[1].AdjustDegrees (-10);
    if (m_Players[1].GetDegrees () < 0)
    { m_Players[1].AdjustDegrees (360); }
  }
  if (0x80 & keys['d'])  // 'd'
  { m_Players[1].AdjustDegrees (10);
    if (m_Players[1].GetDegrees () > 359)
    { m_Players[1].AdjustDegrees (-360); }
  }

  if (0x80 & keys['w'])  // 'w'
  { m_Players[1].Accelerate (.025); }
  if (0x80 & keys[VK_SHIFT])
  { m_Players[1].Fire (1); }

The only 'if' that is called is the shift key one (the last one).  The others are ignored.

Forgot to mention also that 0x80000000 didn't work, I had to cut off a bunch of zeros.  I know that is not the problem because I am using this same code in another area with the VK_LEFT, UP, etc constants and it works there.
0
 

Author Comment

by:a121496
ID: 1324703
I am using VC++ 5.0 compiler.  Perhaps you are using a different version in which those constants were defined?
0
 

Author Comment

by:a121496
ID: 1324704
Ignore all that.  After experimenting, I found that I had to use CAPITAL letters in the [ ] like
keys['A']

Anyway, thanks for the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question