Conditional -- if a letter or number key is pressed

I'm using SDL 1.2 and I'm trying to figure out how to write an if{} statement that will call a function if the user presses a letter or number key, or basically any key *besides* a modifier key (like shift, control, option, command...). I can't do it this way:

1
2
3
4
5
6
7
if ((SDL_PollEvent(&event)) && (event.type == SDL_KEYDOWN))
{
	if ((event.key.keysym.sym != SDLK_LSHIFT) && (event.key.keysym.sym != SDLK_LCTRL) && etc etc...)
	{
		myFunction();
	}
}


because I don't want to exclude an event in which the user presses a letter/number key *and* a modifier key. So to summarize:

1. If the user presses a letter/number key, call the function.
2. If the user presses a modifier key, don't call the function.
3. If the user presses a letter/number key and a modifier key, call the function.

The only solution I can think of would be to just include every single letter/number key like this:

1
2
3
4
5
6
Uint8 *keystates = SDL_GetKeyState(NULL);

if ((keystates[SDLK_a]) || (keystates[SDLK_b]) || (keystates[SDLK_c]) etc etc...)
{
	myFunction();
}


but that would be such a long line of code, and I'm sure there must be a better way, right?

I'd really appreciate any input. And please keep in mind that I'm still pretty new to coding, so you might have to "dumb down" your answer a little. I certainly don't want to use any code that I don't understand!

Thanks.
Here's a terrible example I slapped together. This is just the first thing I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <windows.h>

void function_a() {
	std::cout << "\'a\'" << std::endl;
}

void function_ctrl_a() {
	std::cout << "\'ctrl\' + \'a\'" << std::endl;
}

int main(int argc, char* argv[]) {

	typedef void(*FunctionType)();

	FunctionType functions[2] = {function_a, function_ctrl_a};

	while (true) {
		Sleep(100);
		if (GetAsyncKeyState(0x41)) {
			functions[0 + ((bool)GetAsyncKeyState(VK_CONTROL))]();
		}
	}
	return 0;
}


Basically, you have an array or some sort of container of function pointers. There are different ways of making this sexy. You could sort of interleave the non-control and control functions at regularly spaced index intervals in the array.

You may want to stick around and wait for someone else to blow my suggestion out of the water.

I don't even know if this is what you're asking... ahhhhhhh
Last edited on
Thanks for your reply, xismn. I don't think this is exactly what I needed, but I'm not sure because some of this stuff is a little over my head. I didn't even realize that you could make an array of function pointers. It sounds like something that would be very useful, but I'm not sure if it would be helpful here, as I want to call the same function, regardless of which key is pressed, as long as it's a letter or number.

Anyway, I'm trying to do this in SDL 1.2, which I realize is outdated now, but I'm kind of invested in it. Also, I probably should have mentioned that I'm using OS X.

Hmmmm... okay, your code gave me an idea: I could create a Uint8 vector, and fill it with the SDL constants for all the keys whose states I want to check: SDLK_a, SDLK_b, SDLK_c, etc. Then I could just cycle through the vector...

1
2
3
4
5
6
7
8
9
10
bool keypress = false;
for (iter = keyVector.begin(); iter < keyVector.end(); iter++)
{
	if (keystates[*iter])
	keypress = true;
}
if (keypress)
{
	myFunction();
}


I haven't tried it yet, but it seems like this would work, right? I'm afraid this might slow down my program, though. Well, I'm going to give it a shot; I'll probably be back with more questions later. Thanks again.
I'm glad I was able to inspire you a bit. I see no problem with your approach.
Topic archived. No new replies allowed.