File: KeyTest.cs

package info (click to toggle)
taoframework 2.1.svn20090801-15.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,312 kB
  • sloc: cs: 113,093; makefile: 908; sh: 107; ansic: 7
file content (324 lines) | stat: -rw-r--r-- 11,978 bytes parent folder | download | duplicates (6)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#region License
/*
MIT License
Copyright 2003-2004 Randy Ridge
http://www.taoframework.com
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion License

#region Original Credits/License
/*
Copyright (c) 2002-2004 Marcus Geelnard

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would
   be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not
   be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
   distribution.

Marcus Geelnard
marcus.geelnard at home.se
*/
#endregion Original Credits/License

using System;
using Tao.Glfw;
using Tao.OpenGl;

namespace GlfwExamples {
    #region Class Documentation
    /// <summary>
    ///     This is a small test application for GLFW.
    /// </summary>
    /// <remarks>
    ///     Keyboard input test.
    /// </remarks>
    #endregion Class Documentation
    public sealed class KeyTest {
        // --- Fields ---
        #region Private Static Fields
        private static bool isRunning = true;
        private static bool isKeyRepeat = false;
        private static bool isSystemKeys = true;
        #endregion Private Static Fields

        // --- Application Entry Point ---
        #region Run()
        /// <summary>
        ///     The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Run() {
            int windowWidth, windowHeight;
            double currentTime;

            // Initialise GLFW
            Glfw.glfwInit();

            // Open OpenGL window
            if(Glfw.glfwOpenWindow(250, 100, 0, 0, 0, 0, 0, 0, Glfw.GLFW_WINDOW) == Gl.GL_FALSE) {
                Glfw.glfwTerminate();
                return;
            }

            // Set key callback function
            Glfw.glfwSetKeyCallback(new Glfw.GLFWkeyfun(Key));

            // Set title
            Glfw.glfwSetWindowTitle("Press some keys!");

            // Main loop
            while(isRunning) {
                // Get time
                currentTime = Glfw.glfwGetTime();

                // Get window size (may be different than the requested size)
                Glfw.glfwGetWindowSize(out windowWidth, out windowHeight);
                windowHeight = windowHeight > 0 ? windowHeight : 1;

                // Set viewport
                Gl.glViewport(0, 0, windowWidth, windowHeight);

                // Clear color buffer
                Gl.glClearColor((float) (0.5f + 0.5f * (float) Math.Sin(3.0 * currentTime)),
                    0, 0, 0);
                Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);

                // Swap buffers
                Glfw.glfwSwapBuffers();

                // Check if the window was closed
                if(Glfw.glfwGetWindowParam(Glfw.GLFW_OPENED) == Gl.GL_FALSE) {
                    isRunning = false;
                }
            }

            // Close OpenGL window and terminate GLFW
            Glfw.glfwTerminate();
        }
        #endregion Run()

        // --- Private GLFW Callback Methods ---
        #region Key(int key, int action)
        /// <summary>
        ///     Handles GLFW key callback.
        /// </summary>
        /// <param name="key">
        ///     The key.
        /// </param>
        /// <param name="action">
        ///     The key action.
        /// </param>
        private static void Key(int key, int action) {
            if(action != Glfw.GLFW_PRESS) {
                return;
            }

            switch(key) {
                case Glfw.GLFW_KEY_ESC:
                    Console.WriteLine("ESC => quit program");
                    isRunning = false;
                    break;
                case Glfw.GLFW_KEY_F1:
                case Glfw.GLFW_KEY_F2:
                case Glfw.GLFW_KEY_F3:
                case Glfw.GLFW_KEY_F4:
                case Glfw.GLFW_KEY_F5:
                case Glfw.GLFW_KEY_F6:
                case Glfw.GLFW_KEY_F7:
                case Glfw.GLFW_KEY_F8:
                case Glfw.GLFW_KEY_F9:
                case Glfw.GLFW_KEY_F10:
                case Glfw.GLFW_KEY_F11:
                case Glfw.GLFW_KEY_F12:
                case Glfw.GLFW_KEY_F13:
                case Glfw.GLFW_KEY_F14:
                case Glfw.GLFW_KEY_F15:
                case Glfw.GLFW_KEY_F16:
                case Glfw.GLFW_KEY_F17:
                case Glfw.GLFW_KEY_F18:
                case Glfw.GLFW_KEY_F19:
                case Glfw.GLFW_KEY_F20:
                case Glfw.GLFW_KEY_F21:
                case Glfw.GLFW_KEY_F22:
                case Glfw.GLFW_KEY_F23:
                case Glfw.GLFW_KEY_F24:
                case Glfw.GLFW_KEY_F25:
                    Console.WriteLine("F{0}", 1 + key - Glfw.GLFW_KEY_F1);
                    break;
                case Glfw.GLFW_KEY_UP:
                    Console.WriteLine("UP");
                    break;
                case Glfw.GLFW_KEY_DOWN:
                    Console.WriteLine("DOWN");
                    break;
                case Glfw.GLFW_KEY_LEFT:
                    Console.WriteLine("LEFT");
                    break;
                case Glfw.GLFW_KEY_RIGHT:
                    Console.WriteLine("RIGHT");
                    break;
                case Glfw.GLFW_KEY_LSHIFT:
                    Console.WriteLine("LSHIFT");
                    break;
                case Glfw.GLFW_KEY_RSHIFT:
                    Console.WriteLine("RSHIFT");
                    break;
                case Glfw.GLFW_KEY_LCTRL:
                    Console.WriteLine("LCTRL");
                    break;
                case Glfw.GLFW_KEY_RCTRL:
                    Console.WriteLine("RCTRL");
                    break;
                case Glfw.GLFW_KEY_LALT:
                    Console.WriteLine("LALT");
                    break;
                case Glfw.GLFW_KEY_RALT:
                    Console.WriteLine("RALT");
                    break;
                case Glfw.GLFW_KEY_TAB:
                    Console.WriteLine("TAB");
                    break;
                case Glfw.GLFW_KEY_ENTER:
                    Console.WriteLine("ENTER");
                    break;
                case Glfw.GLFW_KEY_BACKSPACE:
                    Console.WriteLine("BACKSPACE");
                    break;
                case Glfw.GLFW_KEY_INSERT:
                    Console.WriteLine("INSERT");
                    break;
                case Glfw.GLFW_KEY_DEL:
                    Console.WriteLine("DEL");
                    break;
                case Glfw.GLFW_KEY_PAGEUP:
                    Console.WriteLine("PAGEUP");
                    break;
                case Glfw.GLFW_KEY_PAGEDOWN:
                    Console.WriteLine("PAGEDOWN");
                    break;
                case Glfw.GLFW_KEY_HOME:
                    Console.WriteLine("HOME");
                    break;
                case Glfw.GLFW_KEY_END:
                    Console.WriteLine("END");
                    break;
                case Glfw.GLFW_KEY_KP_0:
                    Console.WriteLine("KEYPAD 0");
                    break;
                case Glfw.GLFW_KEY_KP_1:
                    Console.WriteLine("KEYPAD 1");
                    break;
                case Glfw.GLFW_KEY_KP_2:
                    Console.WriteLine("KEYPAD 2");
                    break;
                case Glfw.GLFW_KEY_KP_3:
                    Console.WriteLine("KEYPAD 3");
                    break;
                case Glfw.GLFW_KEY_KP_4:
                    Console.WriteLine("KEYPAD 4");
                    break;
                case Glfw.GLFW_KEY_KP_5:
                    Console.WriteLine("KEYPAD 5");
                    break;
                case Glfw.GLFW_KEY_KP_6:
                    Console.WriteLine("KEYPAD 6");
                    break;
                case Glfw.GLFW_KEY_KP_7:
                    Console.WriteLine("KEYPAD 7");
                    break;
                case Glfw.GLFW_KEY_KP_8:
                    Console.WriteLine("KEYPAD 8");
                    break;
                case Glfw.GLFW_KEY_KP_9:
                    Console.WriteLine("KEYPAD 9");
                    break;
                case Glfw.GLFW_KEY_KP_DIVIDE:
                    Console.WriteLine("KEYPAD DIVIDE");
                    break;
                case Glfw.GLFW_KEY_KP_MULTIPLY:
                    Console.WriteLine("KEYPAD MULTIPLY");
                    break;
                case Glfw.GLFW_KEY_KP_SUBTRACT:
                    Console.WriteLine("KEYPAD SUBTRACT");
                    break;
                case Glfw.GLFW_KEY_KP_ADD:
                    Console.WriteLine("KEYPAD ADD");
                    break;
                case Glfw.GLFW_KEY_KP_DECIMAL:
                    Console.WriteLine("KEYPAD DECIMAL");
                    break;
                case Glfw.GLFW_KEY_KP_EQUAL:
                    Console.WriteLine("KEYPAD =");
                    break;
                case Glfw.GLFW_KEY_KP_ENTER:
                    Console.WriteLine("KEYPAD ENTER");
                    break;
                case Glfw.GLFW_KEY_SPACE:
                    Console.WriteLine("SPACE");
                    break;
                case (int) 'R':
                    isKeyRepeat = !isKeyRepeat;
                    if(isKeyRepeat) {
                        Glfw.glfwEnable(Glfw.GLFW_KEY_REPEAT);
                    }
                    else {
                        Glfw.glfwDisable(Glfw.GLFW_KEY_REPEAT);
                    }
                    Console.WriteLine("R => Key repeat: {0}", isKeyRepeat ? "ON" : "OFF");
                    break;
                case (int) 'S':
                    isSystemKeys = !isSystemKeys;
                    if(isSystemKeys) {
                        Glfw.glfwEnable(Glfw.GLFW_SYSTEM_KEYS);
                    }
                    else {
                        Glfw.glfwDisable(Glfw.GLFW_SYSTEM_KEYS);
                    }
                    Console.WriteLine("S => System keys: {0}", isSystemKeys ? "ON" : "OFF");
                    break;
                default:
                    if(key > 0 && key < 256) {
                        Console.WriteLine("{0}", (char) key);
                    }
                    else {
                        Console.WriteLine("???");
                    }
                    break;
            }
        }
        #endregion Key(int key, int action)
    }
}