Re: Keyboard interaction

Valeri Fine (fine@bnl.gov)
Thu, 24 Jun 1999 13:05:38 -0400


> Dear rooters,
>
> is there a class in ROOT that allows to interact with the keyboard ?
Very likle y the C function int kbhit() may help you.

Microsoft VC++ docs says:

" . . . Checks the console for keyboard input.

int kbhit( void );

Return Value

kbhit returns a nonzero value if a key has been pressed. Otherwise, it
returns 0.

Remarks

The kbhit function checks the console for a recent keystroke. If the
function returns
a nonzero value, a keystroke is waiting in the buffer. The program can then
call _getch
or _getche to get the keystroke.

Example

/* KBHIT.C: This program loops until the user
* presses a key. If _kbhit returns nonzero, a
* keystroke is waiting in the buffer. The program
* can call _getch or _getche to get the keystroke.
*/

#include <conio.h>
#include <stdio.h>

void main( void )
{
/* Display message until key is pressed. */
while( !_kbhit() )
_cputs( "Hit me!! " );

/* Use _getch to throw key away. */
printf( "\nKey struck was '%c'\n", _getch() );
_getch();
}

> E.g., I want to exit an infinite loop in my program by pressing a button
> on my keyboard and to achieve this I'd like to use a ROOT class.
> Additionally, this should not block the program, i.e. it should not stop
> and wait for input, but it should just try to look to the keyboard
> buffer whether a key was pressed or not.
>
> Thank you for any comments on this
>
The method above works fine for "console window" like "telnet" "xterm" on
UNIX
or "MS DOS prompt" under Window. If you want to "click" spme thinh on the
"graphical"
Windows/Widget the ROOT does provide several method for that.
For example one can click on TCanvas windows "View" -> "Interrupt"
afterwards one can
check this "interrupt" event via gROOT->IsInterrupted().
Under Windows is makes sense right away (since it multithread) under UNIX
some delay should
be expected.

The first method (kbhit()) should work under either system (I guess).

Valery

> Thomas
> --
> Thomas Eberl Phone: (+49 89) 289 1 2425
> Physik-Department E12, Technische Universitaet Muenchen
> James-Franck-Strasse *** 85748 Garching b. Muenchen
> Email: Thomas.Eberl@physik.tu-muenchen.de
>