keyboard polling

This is a discussion on keyboard polling within the Game Programming forums, part of the General Programming Boards category; I'm starting to understand OpenGL and GLUT pretty well, but I always used the glutKeyboardFunc, glutMouseFunc, glutMotionFunc, etc to handle ...

  1. #1
    Lode Runner
    Join Date
    May 2004
    Posts
    53

    keyboard polling

    I'm starting to understand OpenGL and GLUT pretty well, but I always used the glutKeyboardFunc, glutMouseFunc, glutMotionFunc, etc to handle user input. Someone once told me I'd get better results using keyboard polling methods. I don't have the slightest idea how to do that

    Anyone care to enlighten me about it ?

  2. #2
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Since no one wants to talk about polling, perhaps someone would just help me use something better than the glut function. Something asynchronous or whatever. Just help me get better response times from the inputs.

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    well in most cases people use an event loop. It goes someting like this:

    main ( )
    {

    ...code...

    while ( 1 )
    {
    bool exit = processMessages ( );
    drawObjectsOnScreen();
    if ( exit ) break;
    }

    ...code...
    }


    bool processMessages ( )
    {
    if ( event occurred )
    {
    switch ( event )
    {
    case KEYDOWN: do something...
    case KEYUP: do something....
    case MOUSEBUTTONCLICK: do something....

    }
    }
    }
    My Website

    "Circular logic is good because it is."

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Use DirectInput.

    You really need to get a book about DirectX though in order to understand it. If you get the DX9 SDK from Microsoft then in the help file they explain quite well how to use DirectInput.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    he's using opengl

  6. #6
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    he's using opengl
    I think Bubba already knows that. He is suggesting for the OP to use DirectInput as it provides a nicer interface to keyboard interprettations and can be easily expanded for multiply devices.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I though DirectInput was a DirectX feature. Sorry

  8. #8
    Banned
    Join Date
    May 2004
    Posts
    129
    It is.

    You can use OpenGL for rendering, but also use DirectInput for, you guessed it, getting input.

  9. #9
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    You can use OpenGL for rendering, but also use DirectInput for, you guessed it, getting input.
    exactly, sorry for being unclear.

  10. #10
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    yes... uuuuh. but I've hated DirectX since when I was using a PC, now I'm working with Mac OS X and Linux. So before I get started on DirectX and other Microsoft undocumented APIs, and get myself banned from the forum, does anyone have a non-OS, non-platform based solution ?

    Links would be just as much appeciated.

    And DavidP, your solution must be great... Can you go into specifics ? I don't quite see an actual implementation.

  11. #11
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    For those of you whom refuse to use Direct Input for some reason or another, simple system like the one I'm using will probably suit your needs.

    Important variables
    Code:
    
    typedef enum KEY_STATE { KS_UP, KS_DOWN, KS_IGNORE };
    
    typedef struct {
    	KEY_STATE state;
    	long time;
    } sKey;
    
    typedef struct {
    	int x,y; // position
    	int wheelDelta; // wheel change
    	int prevx, prevy, prevdelta;
    	int button;
    } sMouse;
    
    sKey keys[256];
    sMouse mouse;
    using variables
    Code:
    		case WM_KEYDOWN:						
    		{
    			keys[wParam].state = KS_DOWN;	     // key is in down state			
    			keys[wParam].time = GetTickCount();  // keypress happend when?
    			return 0;						
    		}
    		case WM_KEYUP:							// Has A Key Been Released?
    		{
    			keys[wParam].state = KS_UP;				 // key is in up state
    			keys[wParam].time = GetTickCount();  // keyup happend when?
    			return 0;						
    		}
    		case WM_SIZE:							// Resize The OpenGL Window
    		{
    			g_winWidth = LOWORD(lParam);
    			g_winHeight = HIWORD(lParam);
    			ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));		// LoWord=Width, HiWord=Height
    			return 0;						// Jump Back
    		}
    		case WM_LBUTTONDOWN:
    		case WM_LBUTTONUP:
    		case WM_RBUTTONDOWN:
    		case WM_RBUTTONUP:
    		case WM_MBUTTONDOWN:
    		case WM_MBUTTONUP:
    		case WM_MOUSEMOVE:
    		{
    			/*
    				MK_CONTROL
    				The CTRL key is down.
    				MK_LBUTTON
    				The left mouse button is down.
    				MK_MBUTTON
    				The middle mouse button is down.
    				MK_RBUTTON
    				The right mouse button is down.
    				MK_SHIFT
    				The SHIFT key is down.
    			*/
    			mouse.prevx = mouse.x;
    			mouse.prevy = mouse.y;
    			mouse.x = GET_X_LPARAM(lParam); 
    			mouse.y = GET_Y_LPARAM(lParam); 
    			mouse.button = wParam;
    			return 0;
    		}
    		case WM_MOUSEWHEEL:
    		{
    			mouse.wheelDelta = HIWORD( wParam ); // wheel rotation 
    			//xPos = LOWORD(lParam); // horizontal position of pointer 
    			//yPos = HIWORD(lParam); // vertical position of pointer 
    			return 0;
    		}
    With those variables, you can impliment a lot of functionality. The skys the limmit.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    ....started on DirectX and other Microsoft undocumented APIs.....
    Not true. The SDK is very well documented. One tripe over to msdn would show you that or one d/l of the DX9 SDK. The help file, while not written for clarity or instructional purposes, is very extensive.

    Keep in mind that ANY solution you use in Windows that does not use DirectInput will be slower as far as games are concerned. Windows API was not designed for low-latency environments, especially in the keyboard functions.

    Face it, the days when you could design a game or an engine independent of an OS are gone. You must use the OS in order to access the hardware...hence the design philosophy behind 32-bit operating systems operating in protected mode and thus the near removal of the interrupt driven OS (not entirely but close).

    Notice in the above example you must wait for a message to be sent before retrieving key presses. With DirectInput this is not so and no messages are sent, hence it is not a message-based input library, although it does have other features that are.
    Last edited by VirtualAce; 06-14-2004 at 07:40 AM.

  13. #13
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    great.

    one last question : what library are : WM_LBUTTONDOWN, WM_KEYDOWN, WM_MOUSEMOVE, etc from ?

  14. #14
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    um.
    uhhhh.
    #include <windows.h> and you'll be fine.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    25
    um.
    uhhhh.
    now I'm working with Mac OS X and Linux

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 03:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 02:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. Keyboard polling loop
    By Paul Balme in forum C Programming
    Replies: 5
    Last Post: 10-22-2001, 05:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21