[linux-audio-dev] C++ Keyboard Event Handling

David Olofson david at olofson.net
Fri Jan 20 12:24:08 UTC 2006


On Friday 20 January 2006 12:31, Carlo Capocasa wrote:
[...]
> I want to create a console keyboard to MIDI application and for this
> I would like to read the physical status of keyboard keys.

That is, MIDI output, as in sending MIDI messages to an onboard or 
external hardware synth, or a softsynth running in the driver or as a 
separate application? (As opposed to generating "wave" audio in your 
application.)

Anyway, keybord input is done by handling "press" and "release" 
events. When you get a "press" event, you set some state variable for 
the corresponding key to "down", and when you get a "release" event, 
you clear that state variable.

SDL also has an alternative API that has this logic built in: 
SDL_GetKeyState():

'man SDL_GetKeyState':
----------------------------------------------------------------------
...
SYNOPSIS
       #include "SDL.h"

       Uint8 *SDL_GetKeyState(int *numkeys);

DESCRIPTION
       Gets a snapshot of the current keyboard state.  The
       current  state  is return as a pointer to an array,
       the size of this array is stored  in  numkeys.  The
       array  is indexed by the SDLK_* symbols. A value of
       1 means the key is pressed and a value of  0  means
       its  not.  The  pointer returned is a pointer to an
       internal SDL array and should not be freed  by  the
       caller.

              Note:

              Use SDL_PumpEvents to update the state array.

EXAMPLE
       Uint8 *keystate = SDL_GetKeyState(NULL);
       if ( keystate[SDLK_RETURN] )
		printf("Return Key Pressed.");
...
----------------------------------------------------------------------


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'



More information about the Linux-audio-dev mailing list