Getting mod keys in SDL

Could someone please tell me what is wrong with my code below? I've been reading through this book on SDL (Focus on SDL) and it keeps referencing members and functions and stuff without giving specific examples of how to use them. It's really frustrating.

The book mentions that you can use SDLMod to see if the user is pressing down any modifier keys, but there are no actual example programs in the book that do this, so I tried to figure it out myself. I just replaced "sym" with "mod" to get the modifier keys, but that didn't work. I'm just trying to determine if the user is pressing down ctrl-r or ctrl-t.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	for(;;)
	{
		if(SDL_PollEvent(&g_Event)==1)
		{
			if((g_Event.type == SDL_KEYDOWN) && (g_Event.key.keysym.sym == SDLK_r) && (g_Event.key.keysym.mod == KMOD_CTRL))
			{
				fprintf(stdout,"ctrl r.\n");
			}
			else if((g_Event.type == SDL_KEYDOWN) && (g_Event.key.keysym.sym == SDLK_t) && (g_Event.key.keysym.mod == KMOD_CTRL))
			{

				fprintf(stdout,"ctrl t.\n");
			}
			else if(g_Event.type == SDL_QUIT)
			{
				fprintf(stdout,"Quit event.\n");
				break;
			}
		}
	}
Last edited on
Looks like you need to use the bitwise AND to check SDL modifier keys.

1
2
3
4
if(event.key.keysym.mod & KMOD_CTRL)
{
    // Run code 
}

Examples can be seen here.
http://www.libsdl.org/release/SDL-1.2.15/docs/html/guideinputkeyboard.html
Yep, that fixed it. I'm not really sure why, though. I'm pretty new to C++ and the only uses of the "bitwise and" that I know of are for references, and the double && to combine booleans.

Anyway, the important thing is I know how to do it now. Thanks for your help!
Topic archived. No new replies allowed.