get_modifiers.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef GET_MODIFIERS_H
  2. #define GET_MODIFIERS_H
  3. //#include "igl_inline.h"
  4. namespace igl
  5. {
  6. enum Modifier
  7. {
  8. MODIFIER_OPTION = 1,
  9. MODIFIER_SHIFT = 2,
  10. MODIFIER_CONTROL = 4,
  11. MODIFIER_COMMAND = 8,
  12. NUM_MODIFIERS = 4,
  13. };
  14. // Retrieve current modifier constellation.
  15. //
  16. // Returns int that's an "or" of the active modifiers above.
  17. //
  18. // FORCED INLINE
  19. inline int get_modifiers();
  20. }
  21. // Implementation
  22. /* glutGetModifiers return mask. */
  23. #ifndef GLUT_ACTIVE_SHIFT
  24. # define GLUT_ACTIVE_SHIFT 1
  25. #endif
  26. #ifndef GLUT_ACTIVE_CTRL
  27. # define GLUT_ACTIVE_CTRL 2
  28. #endif
  29. #ifndef GLUT_ACTIVE_ALT
  30. # define GLUT_ACTIVE_ALT 4
  31. #endif
  32. #ifndef GLUT_ACTIVE_COMMAND
  33. # define GLUT_ACTIVE_COMMAND 8
  34. #endif
  35. #ifdef __APPLE__
  36. //#include <Carbon/HIToolbox/Events.h>
  37. #include <Carbon/Carbon.h>
  38. #endif
  39. #warning "igl::get_modifiers is deprecated. If using GLUT, try Alec's glut patch www.alecjacobson.com/weblog/?p=3659 and use glutGetModifiers"
  40. // FORCED INLINE
  41. inline int igl::get_modifiers()
  42. {
  43. int mod = 0;
  44. #ifdef __APPLE__
  45. // http://stackoverflow.com/a/18082326/148668
  46. KeyMap keyStates;
  47. const auto & carbon_is_keydown = [&keyStates]( uint16_t vKey )->bool
  48. {
  49. uint8_t index = vKey / 32 ;
  50. uint8_t shift = vKey % 32 ;
  51. return keyStates[index].bigEndianValue & (1 << shift) ;
  52. };
  53. GetKeys(keyStates) ;
  54. mod |= (carbon_is_keydown(kVK_Command)?GLUT_ACTIVE_COMMAND:0);
  55. mod |= (carbon_is_keydown(kVK_Shift)?GLUT_ACTIVE_SHIFT:0);
  56. mod |= (carbon_is_keydown(kVK_Option)?GLUT_ACTIVE_ALT:0);
  57. mod |= (carbon_is_keydown(kVK_Control)?GLUT_ACTIVE_CTRL:0);
  58. #else
  59. # warning "igl::get_modifiers not supported on your OS, some demos may not work correctly."
  60. #endif
  61. return mod;
  62. }
  63. #endif