1. clach04
  2. sdl_keycodes

Commits

clach04  committed 07a9fa1 Draft

initial check in of xdpirate's keyCodes.zip app from http://code.google.com/p/pirate-dev/downloads/detail?name=keyCodes.zip

SDL program to show key codes of pressed keys. Exit with Escape. Source included

  • Participants
  • Branches default

Comments (0)

Files changed (2)

File font.ttf

Binary file added.

File keycodes.c

View file
+#include <stdio.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_ttf.h>
+
+/* Simple application to return keycodes for pressed key */
+SDL_Surface *screen = NULL;
+SDL_Surface *text_surface = NULL;
+SDL_Color color = {255,255,255};
+TTF_Font *font = NULL;
+
+void kbai(void);
+
+int main() {
+	SDL_Init(SDL_INIT_VIDEO); // Initialize SDL
+	screen = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE); // Set screen video mode 
+	SDL_ShowCursor(SDL_DISABLE); // No need for cursor on a mouse-less device
+	TTF_Init(); // Initialize SDL_ttf
+	
+	font = TTF_OpenFont("font.ttf", 48);
+	
+	while(1) {
+		SDL_Event event;
+		while (SDL_PollEvent(&event)) {      
+			if (event.type == SDL_QUIT) {
+				kbai();
+			} else if(event.type == SDL_KEYDOWN) {
+				int KeyCode = event.key.keysym.sym;
+				char keyCodeStr[10];
+				sprintf(keyCodeStr, "%d", KeyCode);
+				
+				if(KeyCode == SDLK_ESCAPE) {
+					kbai();
+				} else {
+					int width, height, left, top;
+					
+					TTF_SizeText(font, keyCodeStr, &width, &height);
+					left = ((320 - width) / 2);
+					top = ((240 - height) / 2);
+					
+					SDL_Rect destination;
+					destination.x = left;
+					destination.y = top;
+					destination.w = width;
+					destination.h = height;
+					
+					text_surface = TTF_RenderText_Blended(font, keyCodeStr, color);
+					
+					SDL_FillRect(screen, NULL, 0x0000); // Clear screen
+					SDL_BlitSurface(text_surface, NULL, screen, &destination); // Blit text onto screen
+					SDL_Flip(screen); // Flip backbuffer onto framebuffer
+				}
+			}
+		}
+		
+		SDL_Delay(10);
+	}
+}
+
+void kbai(void) {
+	SDL_FreeSurface(text_surface);
+	TTF_CloseFont(font);
+	font = NULL;
+	TTF_Quit();
+	SDL_FreeSurface(screen);
+	screen = NULL;
+	SDL_Quit();
+
+	exit(0);
+}