1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "GUI.h"
7 #include "GUI_termwin.h"
8
9
10 /* Variables that we'll need to clean up */
11 GUI *gui;
12
13 class KeyboardDisplay : public GUI_TermWin {
14 public:
KeyboardDisplay(int x,int y,int w,int h,GUI_TermWin * otherwin)15 KeyboardDisplay(int x, int y, int w, int h, GUI_TermWin *otherwin) :
16 GUI_TermWin(x, y, w, h, NULL) {
17 textout = otherwin;
18 }
KeyDown(SDL_keysym key)19 virtual GUI_status KeyDown(SDL_keysym key) {
20 GUI_status status;
21
22 KeyInput(key, 1);
23
24 /* Look for Ctrl-Q, and mark us done when we see it */
25 status = GUI_YUM;
26 if ( (SDL_GetKeyState(NULL)[SDLK_q] == SDL_PRESSED) &&
27 (SDL_GetModState() & KMOD_CTRL) ) {
28 status = GUI_QUIT;
29 }
30 return(status);
31 }
KeyUp(SDL_keysym key)32 virtual GUI_status KeyUp(SDL_keysym key) {
33 KeyInput(key, 0);
34 return(GUI_YUM);
35 }
36
37 protected:
38 GUI_TermWin *textout;
39
KeyInput(SDL_keysym keysym,int pressed)40 void KeyInput(SDL_keysym keysym, int pressed) {
41 char textbuf[BUFSIZ];
42 unsigned char charbuf[3];
43 int pos;
44
45 sprintf(textbuf, "SDL key '%s' (0x%.2x)",
46 SDL_GetKeyName(keysym.sym), keysym.scancode);
47 if ( pressed ) {
48 strcat(textbuf, " pressed!");
49 } else {
50 strcat(textbuf, " released!");
51 }
52 Clear();
53 AddText(textbuf, strlen(textbuf));
54
55 /* If the key was released, we're done */
56 if ( ! pressed ) {
57 return;
58 }
59
60 /* Print out the character that was pressed */
61 pos = 0;
62 if ( keysym.unicode && (keysym.unicode <= 255) ) {
63 /* <BS> backs over a key */
64 if ((keysym.unicode != '\b')&&(keysym.unicode <= 26)) {
65 charbuf[pos++] = '^';
66 charbuf[pos++] = keysym.unicode-1+'A';
67 } else
68 charbuf[pos++] = (unsigned char)keysym.unicode;
69 }
70 charbuf[pos] = 0;
71 textout->AddText((char *)charbuf, strlen((char *)charbuf));
72 }
73 };
74
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 SDL_Surface *screen;
78 GUI_TermWin *output;
79 GUI_TermWin *keywin;
80
81 /* Initialize SDL */
82 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
83 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
84 exit(1);
85 }
86 atexit(SDL_Quit);
87
88 /* Get a video mode for display */
89 screen = SDL_SetVideoMode(320, 240, 0, SDL_SWSURFACE);
90 if ( screen == NULL ) {
91 fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
92 exit(1);
93 }
94 SDL_WM_SetCaption("SDL Keyboard Test", "keyboard");
95
96 /* Create a GUI container */
97 gui = new GUI(screen);
98
99 /* Create the output windows */
100 output = new GUI_TermWin(0, 1+8+1, screen->w, screen->h-10);
101 gui->AddWidget(output);
102 keywin = new KeyboardDisplay(0, 1, screen->w, 8, output);
103 gui->AddWidget(keywin);
104
105 /* Run the GUI, and then clean up when it's done. */
106 gui->Run(NULL);
107 delete gui;
108 exit(0);
109
110 /* Make the compiler happy */
111 return(0);
112 }
113