Added keyboard demo, and changed the spaceball demo to also build on windows
[freeglut] / progs / demos / keyboard / keyboard.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <GL/freeglut.h>
6
7 void display(void);
8 void draw_text(int x, int y, const char *str);
9 const char *skeyname(int skey);
10 void reshape(int x, int y);
11 void keypress(unsigned char key, int x, int y);
12 void keyrelease(unsigned char key, int x, int y);
13 void skeypress(int key, int x, int y);
14 void skeyrelease(int key, int x, int y);
15
16 unsigned int modstate;
17 int cur_key = -1;
18 int cur_skey = -1;
19 int win_width, win_height;
20
21 int main(int argc, char **argv)
22 {
23         glutInit(&argc, argv);
24         glutInitWindowSize(400, 200);
25         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
26         glutCreateWindow("Keyboard demo");
27
28         glutDisplayFunc(display);
29         glutReshapeFunc(reshape);
30         glutKeyboardFunc(keypress);
31         glutKeyboardUpFunc(keyrelease);
32         glutSpecialFunc(skeypress);
33         glutSpecialUpFunc(skeyrelease);
34
35         glutMainLoop();
36         return 0;
37 }
38
39 void display(void)
40 {
41         char str[256];
42
43         glClear(GL_COLOR_BUFFER_BIT);
44
45         strcpy(str, "Key:");
46         if(cur_key > 0) {
47                 if(isprint(cur_key)) {
48                         sprintf(str + 4, " '%c'", cur_key);
49                 } else {
50                         sprintf(str + 4, " 0x%02x", cur_key);
51                 }
52
53
54                 if(modstate & GLUT_ACTIVE_SHIFT) {
55                         strcat(str, "  shift");
56                 }
57                 if(modstate & GLUT_ACTIVE_CTRL) {
58                         strcat(str, "  ctrl");
59                 }
60                 if(modstate & GLUT_ACTIVE_ALT) {
61                         strcat(str, "  alt");
62                 }
63                 if(modstate & GLUT_ACTIVE_SUPER) {
64                         strcat(str, "  super");
65                 }
66         }
67         draw_text(win_width / 3, 2 * win_height / 3, str);
68
69         strcpy(str, "Special key: ");
70         if(cur_skey > 0) {
71                 strcat(str, skeyname(cur_skey));
72         }
73         draw_text(win_width / 3, win_height / 3, str);
74
75         glutSwapBuffers();
76 }
77
78 void draw_text(int x, int y, const char *str)
79 {
80         glRasterPos2i(x, y);
81         while(*str) {
82                 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str++);
83         }
84 }
85
86 const char *skeyname(int skey)
87 {
88         static const char *fkeys[] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"};
89
90         switch(skey) {
91         case GLUT_KEY_LEFT: return "left";
92         case GLUT_KEY_UP: return "up";
93         case GLUT_KEY_RIGHT: return "right";
94         case GLUT_KEY_DOWN: return "down";
95         case GLUT_KEY_PAGE_UP: return "page up";
96         case GLUT_KEY_PAGE_DOWN: return "page down";
97         case GLUT_KEY_HOME: return "home";
98         case GLUT_KEY_END: return "end";
99         case GLUT_KEY_INSERT: return "insert";
100         case GLUT_KEY_NUM_LOCK: return "num lock";
101         case GLUT_KEY_BEGIN: return "begin";
102         case GLUT_KEY_DELETE: return "delete";
103         case GLUT_KEY_SHIFT_L: return "L Shift";
104         case GLUT_KEY_SHIFT_R: return "R Shift";
105         case GLUT_KEY_CTRL_L: return "L Ctrl";
106         case GLUT_KEY_CTRL_R: return "R Ctrl";
107         case GLUT_KEY_ALT_L: return "L Alt";
108         case GLUT_KEY_ALT_R: return "R Alt";
109         case GLUT_KEY_SUPER_L: return "L Super";
110         case GLUT_KEY_SUPER_R: return "R Super";
111         default:
112                 if(skey >= GLUT_KEY_F1 && skey <= GLUT_KEY_F12) {
113                         return fkeys[skey - GLUT_KEY_F1];
114                 }
115
116                 break;
117         }
118         return "<unknown>";
119 }
120
121 void reshape(int x, int y)
122 {
123         win_width = x;
124         win_height = y;
125         glViewport(0, 0, x, y);
126         glMatrixMode(GL_PROJECTION);
127         glLoadIdentity();
128         glOrtho(0, x, 0, y, -1, 1);
129 }
130
131 void keypress(unsigned char key, int x, int y)
132 {
133         if(key == 27) exit(0);
134
135         modstate = glutGetModifiers();
136         cur_key = key;
137         glutPostRedisplay();
138 }
139
140 void keyrelease(unsigned char key, int x, int y)
141 {
142         cur_key = -1;
143         glutPostRedisplay();
144 }
145
146 void skeypress(int key, int x, int y)
147 {
148         cur_skey = key;
149         glutPostRedisplay();
150 }
151
152 void skeyrelease(int key, int x, int y)
153 {
154         cur_skey = -1;
155         glutPostRedisplay();
156 }