keyboard.cpp 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "keyboard.hpp"
  2. bool Keyboard::getKey( KeySym key ) {
  3. KeyCode keycode = XKeysymToKeycode( x11->display, key );
  4. if ( keycode != 0 ) {
  5. // Get the whole keyboard state
  6. char keys[32];
  7. XQueryKeymap( x11->display, keys );
  8. // Check our keycode
  9. return ( keys[ keycode / 8 ] & ( 1 << ( keycode % 8 ) ) ) != 0;
  10. } else {
  11. return false;
  12. }
  13. }
  14. bool Keyboard::anyKeyDown() {
  15. // Thanks to SFML for some reliable key state grabbing.
  16. // Get the whole keyboard state
  17. char keys[ 32 ];
  18. XQueryKeymap( x11->display, keys );
  19. // Each bit indicates a different key, 1 for pressed, 0 otherwise.
  20. // Every bit should be 0 if nothing is pressed.
  21. for ( unsigned int i = 0; i < 32; i++ ) {
  22. if ( keys[ i ] != 0 ) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. Keyboard::Keyboard( X11* x11 ) {
  29. this->x11 = x11;
  30. int err = XGrabKeyboard( x11->display, x11->root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
  31. if ( err != GrabSuccess ) {
  32. throw new std::runtime_error( "Failed to grab keyboard.\n" );
  33. }
  34. }
  35. Keyboard::~Keyboard() {
  36. XUngrabKeyboard( x11->display, CurrentTime );
  37. }