Detect if a letter key is pressed?

May 11, 2014 at 11:46pm
How would I detect if any letter key is pressed. Would I have to use GetAsyncKeyState() for every letter key? Or could I have some kind of loop going through all the hexadecimal values for every letter? I don't know how I would go through hexadecimal values with a loop.
May 12, 2014 at 12:08am
closed account (j3Rz8vqX)
every letter key
==
some kind of loop going through all the hexadecimal values for every letter


Either way, you're going to be doing what you set out to do; detect keys pressed.

Response question:

What keys are you specifically detecting?

Certainly not all of the keys are of value, unless you're creating a key pressed detection program.

Else, stick with the keys you're listening for.

Tip:
Hex values and integer values are interchangeable; you can detect using integer representation.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

0xFE is the last value, and has an integer value of 254.
May 12, 2014 at 12:14am
Looping on GetAsyncKeyState is possible, but is not necessary.


If you are using an actual window, it's much easier to just capture the WM_KEYDOWN message which is sent to your window whenever the user presses any key. Then you can check the key they pressed, and it's in a letter, do whatever you want.


Practically all game libs (like SFML, SDL, Allegro) have similar functionality where they send events when a key is pressed.

Same with widgetry libs -- they also have the same thing (Qt, wxWidgets)
May 12, 2014 at 12:36am
I only want to detect letters A-Z. I tried this code, but it didn't work. Are these the right integer values for it? Also, I'm not using a window for this so I can't do Allegro stuff.

1
2
3
4
5
6
7
8
9
10
int numKeys = 0;

for (int i = 203; i <= 228; i++) {
if(GetAsyncKeyState(i))
	numKeys++;
}

if (numKeys > 0) {
stuff
}
May 12, 2014 at 2:12am
closed account (j3Rz8vqX)
No, you should know that every digit represents 16x its column.
http://www.binaryhexconverter.com/hex-to-decimal-converter

16*4 = 64 + 1 = 65
It is 65.
May 12, 2014 at 2:14am
Don't use magic numbers. If you want to check for A-Z, then just use A-Z:

1
2
3
4
for (int i = 'A'; i <= 'Z'; i++) {
if(GetAsyncKeyState(i))
	numKeys++;
}
May 12, 2014 at 2:36am
Thanks for the help. I used this to get it working.

1
2
3
4
for (int i = 'A'; i <= 'Z'; i++) {
if(GetAsyncKeyState(i))
	numKeys++;
}
Topic archived. No new replies allowed.