How to get ascii characters from the keyboard?

Hi,

I would like to print the characters the user types on the keyboard. I know how to get scancodes/keycodes but how do you get ascii from that (lower/upper case etc.)?

thanks

Maybe this tutorial in the wiki might help you:
http://wiki.libsdl.org/Tutorials/TextInputAm 07.09.2013 16:21, schrieb tsfp:

Hi,

I would like to print the characters the user types on the keyboard. I
know how to get scancodes/keycodes but how do you get ascii from that
(lower/upper case etc.)?

thanks


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi

I am not sure what exactly you wish to accomplish; whether you are just trying to send bytes within the ascii range of values to stdout (i.e. print characters (like ‘a’, ‘F’, etc.)) or you actually want to print the ascii values corresponding to those characters to stdout. In any case, you could accomplish it by doing something like this:

Code:

#include “SDL2/SDL.h”

int main (int argc, char* argv[])
{
SDL_Window* SDL_Window_Pointer_MainWindow;
SDL_Window_Pointer_MainWindow = NULL;

SDL_Event SDL_Event_MainWindowEvent;
SDL_zero (SDL_Event_MainWindowEvent);

SDL_bool SDL_bool_RunMainWindowEventLoop;
SDL_bool_RunMainWindowEventLoop = SDL_TRUE;

SDL_Init (SDL_INIT_VIDEO);
SDL_Window_Pointer_MainWindow = SDL_CreateWindow ("Some Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200, 200, 0);

while (SDL_bool_RunMainWindowEventLoop)
{
	SDL_WaitEvent (&SDL_Event_MainWindowEvent);
	SDL_PushEvent (&SDL_Event_MainWindowEvent);
		
	while (SDL_PollEvent (&SDL_Event_MainWindowEvent))
	{
		switch (SDL_Event_MainWindowEvent.type) 
		{
			case SDL_QUIT:
				printf ("\nQuit event received.\n\n");
				SDL_bool_RunMainWindowEventLoop = SDL_FALSE;
			break;
				
			case SDL_KEYDOWN:
				switch (SDL_Event_MainWindowEvent.key.keysym.sym) 
				{
					case SDLK_a:
						if (SDL_Event_MainWindowEvent.key.repeat)
						{
							if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
							{
								printf ("'A' key down (repeat); Ascii 65. \n");
							}
							else
							{
								printf ("'a' key down (repeat); Ascii 97.\n");
							}
						}
						else
						{
							if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
							{
								printf ("'A' key down (non-repeat); Ascii 65.\n");	
							}
							else
							{
								printf ("'a' key down (non-repeat); Ascii 97.\n");
							}
						}
					break;

					/*...More keys here...*/
			
					default:
  						break;
				}
			break;

			/*...More events here...*/				

			default:
  				break;
		}
	}
}
SDL_DestroyWindow (SDL_Window_Pointer_MainWindow);
SDL_Quit ();
return 0;

}

Note: I assume you use C, so I wrote this up quickly. I am more used to writing code in 32/64-bit assembly using nasm, so it might not be perfect. I omitted all error checking to keep it as short as possible while still being fully functional. Compile it, play with it and see if it is what you were looking for.

Daniel Raufison wrote:

Maybe this tutorial in the wiki might help you:
http://wiki.libsdl.org/Tutorials/TextInput

I had already tried that in my project and it didn’t work. So… I re-tried in a new project and know it works… [Rolling Eyes]
And it does exactly what i wanted :slight_smile:
thx

@lgm: yes you can do that with if/else for every character, but that’s long and I hoped there would be a faster way to do that.

Daniel Raufison wrote:

Maybe this tutorial in the wiki might help you:
http://wiki.libsdl.org/Tutorials/TextInput

I had already tried that in my project and it didn’t work. So… I re-tried in a new project and now it works… [Rolling Eyes]
And it does exactly what i wanted [Smile]
thx

@lgm: yes you can do that with if/else for every character, but that’s long and I hoped there would be a faster way to do that.

I’ve having the same problem, I have a translation for lower to upper characters.

However, now I need CTRL Keys… like CTRL-X and others… how do I get the ASCII values when these are pressed from the MOD and keysym?

CTRL-X = 120 or 0x18.

lgm wrote:> Hi

I am not sure what exactly you wish to accomplish; whether you are just trying to send bytes within the ascii range of values to stdout (i.e. print characters (like ‘a’, ‘F’, etc.)) or you actually want to print the ascii values corresponding to those characters to stdout. In any case, you could accomplish it by doing something like this:

Code:

#include “SDL2/SDL.h”

int main (int argc, char* argv[])
{
SDL_Window* SDL_Window_Pointer_MainWindow;
SDL_Window_Pointer_MainWindow = NULL;

SDL_Event SDL_Event_MainWindowEvent;
SDL_zero (SDL_Event_MainWindowEvent);

SDL_bool SDL_bool_RunMainWindowEventLoop;
SDL_bool_RunMainWindowEventLoop = SDL_TRUE;

SDL_Init (SDL_INIT_VIDEO);
SDL_Window_Pointer_MainWindow = SDL_CreateWindow (“Some Title”, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200, 200, 0);

while (SDL_bool_RunMainWindowEventLoop)
{
SDL_WaitEvent (&SDL_Event_MainWindowEvent);
SDL_PushEvent (&SDL_Event_MainWindowEvent);

  while (SDL_PollEvent (&SDL_Event_MainWindowEvent))
  {
  	switch (SDL_Event_MainWindowEvent.type) 
  	{
  		case SDL_QUIT:
  			printf ("\nQuit event received.\n\n");
  			SDL_bool_RunMainWindowEventLoop = SDL_FALSE;
  		break;
  			
  		case SDL_KEYDOWN:
  			switch (SDL_Event_MainWindowEvent.key.keysym.sym) 
  			{
  				case SDLK_a:
  					if (SDL_Event_MainWindowEvent.key.repeat)
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (repeat); Ascii 65. \n");
  						}
  						else
  						{
  							printf ("'a' key down (repeat); Ascii 97.\n");
  						}
  					}
  					else
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (non-repeat); Ascii 65.\n");	
  						}
  						else
  						{
  							printf ("'a' key down (non-repeat); Ascii 97.\n");
  						}
  					}
  				break;

  				/*...More keys here...*/
  		
  				default:
  						break;
  			}
  		break;

  		/*...More events here...*/				

  		default:
  				break;
  	}
  }

}
SDL_DestroyWindow (SDL_Window_Pointer_MainWindow);
SDL_Quit ();
return 0;
}

Note: I assume you use C, so I wrote this up quickly. I am more used to writing code in 32/64-bit assembly using nasm, so it might not be perfect. I omitted all error checking to keep it as short as possible while still being fully functional. Compile it, play with it and see if it is what you were looking for.

There isnt a ASCII value for CTRL … you check if CTRL is pressed and if x or whatever is pressed. If you really want it to be a number then check if they are pressed and assign a number yourself to whatever variable you need.

int value = 0;

if( (event.key.keysym.sym == SDLK_X) && (event.key.keysym.mod & KMOD_CTRL) )
value = 120;

Magnet wrote:> I’ve having the same problem, I have a translation for lower to upper characters.

However, now I need CTRL Keys… like CTRL-X and others… how do I get the ASCII values when these are pressed from the MOD and keysym?

CTRL-X = 120 or 0x18.

lgm wrote:

Hi

I am not sure what exactly you wish to accomplish; whether you are just trying to send bytes within the ascii range of values to stdout (i.e. print characters (like ‘a’, ‘F’, etc.)) or you actually want to print the ascii values corresponding to those characters to stdout. In any case, you could accomplish it by doing something like this:

Code:

#include “SDL2/SDL.h”

int main (int argc, char* argv[])
{
SDL_Window* SDL_Window_Pointer_MainWindow;
SDL_Window_Pointer_MainWindow = NULL;

SDL_Event SDL_Event_MainWindowEvent;
SDL_zero (SDL_Event_MainWindowEvent);

SDL_bool SDL_bool_RunMainWindowEventLoop;
SDL_bool_RunMainWindowEventLoop = SDL_TRUE;

SDL_Init (SDL_INIT_VIDEO);
SDL_Window_Pointer_MainWindow = SDL_CreateWindow ("Some Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200, 200, 0);

while (SDL_bool_RunMainWindowEventLoop)
{
  SDL_WaitEvent (&SDL_Event_MainWindowEvent);
  SDL_PushEvent (&SDL_Event_MainWindowEvent);
  	
  while (SDL_PollEvent (&SDL_Event_MainWindowEvent))
  {
  	switch (SDL_Event_MainWindowEvent.type) 
  	{
  		case SDL_QUIT:
  			printf ("\nQuit event received.\n\n");
  			SDL_bool_RunMainWindowEventLoop = SDL_FALSE;
  		break;
  			
  		case SDL_KEYDOWN:
  			switch (SDL_Event_MainWindowEvent.key.keysym.sym) 
  			{
  				case SDLK_a:
  					if (SDL_Event_MainWindowEvent.key.repeat)
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (repeat); Ascii 65. \n");
  						}
  						else
  						{
  							printf ("'a' key down (repeat); Ascii 97.\n");
  						}
  					}
  					else
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (non-repeat); Ascii 65.\n");	
  						}
  						else
  						{
  							printf ("'a' key down (non-repeat); Ascii 97.\n");
  						}
  					}
  				break;

  				/*...More keys here...*/
  		
  				default:
  						break;
  			}
  		break;

  		/*...More events here...*/				

  		default:
  				break;
  	}
  }
}
  SDL_DestroyWindow (SDL_Window_Pointer_MainWindow);
SDL_Quit ();
return 0;

}

Note: I assume you use C, so I wrote this up quickly. I am more used to writing code in 32/64-bit assembly using nasm, so it might not be perfect. I omitted all error checking to keep it as short as possible while still being fully functional. Compile it, play with it and see if it is what you were looking for.

There isnt a ASCII value for CTRL … you check if CTRL is pressed and if x or whatever is pressed. If you really want it to be a number then check if they are pressed and assign a number yourself to whatever variable you need.

int value = 0;

if( (event.key.keysym.sym == SDLK_X) && ((event.key.keysym.mod & KMOD_CTRL) == KMOD_CTRL) )
value = 120;

Magnet wrote:> I’ve having the same problem, I have a translation for lower to upper characters.

However, now I need CTRL Keys… like CTRL-X and others… how do I get the ASCII values when these are pressed from the MOD and keysym?

CTRL-X = 120 or 0x18.

lgm wrote:

Hi

I am not sure what exactly you wish to accomplish; whether you are just trying to send bytes within the ascii range of values to stdout (i.e. print characters (like ‘a’, ‘F’, etc.)) or you actually want to print the ascii values corresponding to those characters to stdout. In any case, you could accomplish it by doing something like this:

Code:

#include “SDL2/SDL.h”

int main (int argc, char* argv[])
{
SDL_Window* SDL_Window_Pointer_MainWindow;
SDL_Window_Pointer_MainWindow = NULL;

SDL_Event SDL_Event_MainWindowEvent;
SDL_zero (SDL_Event_MainWindowEvent);

SDL_bool SDL_bool_RunMainWindowEventLoop;
SDL_bool_RunMainWindowEventLoop = SDL_TRUE;

SDL_Init (SDL_INIT_VIDEO);
SDL_Window_Pointer_MainWindow = SDL_CreateWindow ("Some Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200, 200, 0);

while (SDL_bool_RunMainWindowEventLoop)
{
  SDL_WaitEvent (&SDL_Event_MainWindowEvent);
  SDL_PushEvent (&SDL_Event_MainWindowEvent);
  	
  while (SDL_PollEvent (&SDL_Event_MainWindowEvent))
  {
  	switch (SDL_Event_MainWindowEvent.type) 
  	{
  		case SDL_QUIT:
  			printf ("\nQuit event received.\n\n");
  			SDL_bool_RunMainWindowEventLoop = SDL_FALSE;
  		break;
  			
  		case SDL_KEYDOWN:
  			switch (SDL_Event_MainWindowEvent.key.keysym.sym) 
  			{
  				case SDLK_a:
  					if (SDL_Event_MainWindowEvent.key.repeat)
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (repeat); Ascii 65. \n");
  						}
  						else
  						{
  							printf ("'a' key down (repeat); Ascii 97.\n");
  						}
  					}
  					else
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (non-repeat); Ascii 65.\n");	
  						}
  						else
  						{
  							printf ("'a' key down (non-repeat); Ascii 97.\n");
  						}
  					}
  				break;

  				/*...More keys here...*/
  		
  				default:
  						break;
  			}
  		break;

  		/*...More events here...*/				

  		default:
  				break;
  	}
  }
}
  SDL_DestroyWindow (SDL_Window_Pointer_MainWindow);
SDL_Quit ();
return 0;

}

Note: I assume you use C, so I wrote this up quickly. I am more used to writing code in 32/64-bit assembly using nasm, so it might not be perfect. I omitted all error checking to keep it as short as possible while still being fully functional. Compile it, play with it and see if it is what you were looking for.

There isnt a ASCII value for CTRL … you check if CTRL is pressed and if x or whatever is pressed. If you really want it to be a number then check if they are pressed and assign a number yourself to whatever variable you need.

int value = 0;

if( (event.key.keysym.sym == SDLK_X) && (event.key.keysym.mod & KMOD_CTRL) )
value = 120;

Magnet wrote:> I’ve having the same problem, I have a translation for lower to upper characters.

However, now I need CTRL Keys… like CTRL-X and others… how do I get the ASCII values when these are pressed from the MOD and keysym?

CTRL-X = 120 or 0x18.

lgm wrote:

Hi

I am not sure what exactly you wish to accomplish; whether you are just trying to send bytes within the ascii range of values to stdout (i.e. print characters (like ‘a’, ‘F’, etc.)) or you actually want to print the ascii values corresponding to those characters to stdout. In any case, you could accomplish it by doing something like this:

Code:

#include “SDL2/SDL.h”

int main (int argc, char* argv[])
{
SDL_Window* SDL_Window_Pointer_MainWindow;
SDL_Window_Pointer_MainWindow = NULL;

SDL_Event SDL_Event_MainWindowEvent;
SDL_zero (SDL_Event_MainWindowEvent);

SDL_bool SDL_bool_RunMainWindowEventLoop;
SDL_bool_RunMainWindowEventLoop = SDL_TRUE;

SDL_Init (SDL_INIT_VIDEO);
SDL_Window_Pointer_MainWindow = SDL_CreateWindow ("Some Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200, 200, 0);

while (SDL_bool_RunMainWindowEventLoop)
{
  SDL_WaitEvent (&SDL_Event_MainWindowEvent);
  SDL_PushEvent (&SDL_Event_MainWindowEvent);
  	
  while (SDL_PollEvent (&SDL_Event_MainWindowEvent))
  {
  	switch (SDL_Event_MainWindowEvent.type) 
  	{
  		case SDL_QUIT:
  			printf ("\nQuit event received.\n\n");
  			SDL_bool_RunMainWindowEventLoop = SDL_FALSE;
  		break;
  			
  		case SDL_KEYDOWN:
  			switch (SDL_Event_MainWindowEvent.key.keysym.sym) 
  			{
  				case SDLK_a:
  					if (SDL_Event_MainWindowEvent.key.repeat)
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (repeat); Ascii 65. \n");
  						}
  						else
  						{
  							printf ("'a' key down (repeat); Ascii 97.\n");
  						}
  					}
  					else
  					{
  						if (SDL_Event_MainWindowEvent.key.keysym.mod & (KMOD_SHIFT|KMOD_CAPS))
  						{
  							printf ("'A' key down (non-repeat); Ascii 65.\n");	
  						}
  						else
  						{
  							printf ("'a' key down (non-repeat); Ascii 97.\n");
  						}
  					}
  				break;

  				/*...More keys here...*/
  		
  				default:
  						break;
  			}
  		break;

  		/*...More events here...*/				

  		default:
  				break;
  	}
  }
}
  SDL_DestroyWindow (SDL_Window_Pointer_MainWindow);
SDL_Quit ();
return 0;

}

Note: I assume you use C, so I wrote this up quickly. I am more used to writing code in 32/64-bit assembly using nasm, so it might not be perfect. I omitted all error checking to keep it as short as possible while still being fully functional. Compile it, play with it and see if it is what you were looking for.

There is a TextInput api, if still applies unicode maps ascii.

Ya that’s pretty much what I did. Seems like the API could use some improvements in this area so we don’t have to do this manually along with all the SHIFT combinations.

[quote=“AlexRou”]There isnt a ASCII value for CTRL … you check if CTRL is pressed and if x or whatever is pressed. If you really want it to be a number then check if they are pressed and assign a number yourself to whatever variable you need.

int value = 0;

if( (event.key.keysym.sym == SDLK_X) && (event.key.keysym.mod & KMOD_CTRL) )
value = 120;

[quote=“Magnet”]I’ve having the same problem, I have a translation for lower to upper characters.

However, now I need CTRL Keys… like CTRL-X and others… how do I get the ASCII values when these are pressed from the MOD and keysym?

CTRL-X = 120 or 0x18.