1 //! \example tutorial-event-keyboard.cpp
2 #include <visp3/gui/vpDisplayOpenCV.h>
3 #include <visp3/gui/vpDisplayX.h>
4 #include <visp3/gui/vpDisplayGTK.h>
5 #include <visp3/gui/vpDisplayGDI.h>
6 #include <visp3/gui/vpDisplayD3D.h>
7 
main()8 int main()
9 {
10   vpImage<unsigned char> I(240, 320); // Create a black image
11 #if defined(VISP_HAVE_X11)
12   vpDisplay *d = new vpDisplayX;
13 #elif defined(VISP_HAVE_GTK)
14   vpDisplay *d = new vpDisplayGTK;
15 #elif defined(VISP_HAVE_GDI)
16   vpDisplay *d = new vpDisplayGDI;
17 #elif defined(VISP_HAVE_D3D9)
18   vpDisplay *d = new vpDisplayD3D;
19 #elif defined(VISP_HAVE_OPENCV)
20   vpDisplay *d = new vpDisplayOpenCV;
21 #else
22   std::cout << "Sorry, no video device is available" << std::endl;
23   return -1;
24 #endif
25   // Initialize the display with the image I. Display and image are
26   // now link together.
27 #ifdef VISP_HAVE_DISPLAY
28   d->init(I);
29 #endif
30   // Set the display background with image I content
31   vpDisplay::display(I);
32   // Flush the foreground and background display
33   vpDisplay::flush(I);
34   // Wait for keyboard event
35   std::cout << "Waiting a keyboard event..." << std::endl;
36   vpDisplay::getKeyboardEvent(I, true);
37   std::cout << "A keyboard event was detected" << std::endl;
38   // Non blocking keyboard event loop
39   int cpt_event = 0;
40   char key[10];
41   std::cout << "Enter a non blocking keyboard event detection loop..." << std::endl;
42   do {
43     bool event = vpDisplay::getKeyboardEvent(I, &key[0], false);
44     if (event) {
45       std::cout << "Key detected: " << key << std::endl;
46       cpt_event ++;
47     }
48     vpTime::wait(5); // wait 5 ms
49   } while(cpt_event < 5);
50 #ifdef VISP_HAVE_DISPLAY
51   delete d;
52 #endif
53 }
54