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