PDA

View Full Version : Keyboard and GLUT



Hec
06-10-2002, 12:52 PM
Hi!

I need to know the state of keys (pressed, realeased), i saw the example in the new OpenGL Toolkit FAQ and there is an half-answer for this, becouse in my glut.h (version 3) file, i didn't find any function where i can register a:
keyboardfunc( int key, int state )
callback function

So how do i register this callback, or how do i call this function?

Thanks.

jmathies
06-10-2002, 02:06 PM
Originally posted by Hec:
Hi!

I need to know the state of keys (pressed, realeased), i saw the example in the new OpenGL Toolkit FAQ and there is an half-answer for this, becouse in my glut.h (version 3) file, i didn't find any function where i can register a:
keyboardfunc( int key, int state )
callback function

So how do i register this callback, or how do i call this function?

Thanks.

I'll update the faq with this example code -
hope this helps.

Regards,
Jim




// gluttest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include "glut.h"
#include <stdio.h>
#pragma comment( lib, "glut32" )

static bool keystate[256];

void glutKeyboardUpCallback( unsigned char key, int x, int y )
{
printf( "keyup=%i\n", key );
keystate[key] = false;
}


void glutKeyboardCallback( unsigned char key, int x, int y )
{
printf( "keydn=%i\n", key );
keystate[key] = true;
}

void glutDisplayCallback( void )
{
Sleep(100);

for ( int i=0; i <= 255; i++ )
{
if ( keystate[i] == true )
printf( "ASCII key %i is pressed.\n", i );
}

glutPostRedisplay();
}

int main(int argc, char* argv[])
{


glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow( "test" );
glutKeyboardFunc( glutKeyboardCallback );
glutKeyboardUpFunc( glutKeyboardUpCallback );
glutDisplayFunc( glutDisplayCallback );
glutMainLoop();

return 0;
}

Hec
06-11-2002, 01:37 PM
Thanks Jim!