get_modifiers.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef GET_MODIFIERS_H
  9. #define GET_MODIFIERS_H
  10. //#include "igl_inline.h"
  11. namespace igl
  12. {
  13. enum Modifier
  14. {
  15. MODIFIER_OPTION = 1,
  16. MODIFIER_SHIFT = 2,
  17. MODIFIER_CONTROL = 4,
  18. MODIFIER_COMMAND = 8,
  19. NUM_MODIFIERS = 4,
  20. };
  21. // Retrieve current modifier constellation.
  22. //
  23. // Returns int that's an "or" of the active modifiers above.
  24. //
  25. // FORCED INLINE
  26. inline int get_modifiers();
  27. }
  28. // Implementation
  29. /* glutGetModifiers return mask. */
  30. #ifndef GLUT_ACTIVE_SHIFT
  31. # define GLUT_ACTIVE_SHIFT 1
  32. #endif
  33. #ifndef GLUT_ACTIVE_CTRL
  34. # define GLUT_ACTIVE_CTRL 2
  35. #endif
  36. #ifndef GLUT_ACTIVE_ALT
  37. # define GLUT_ACTIVE_ALT 4
  38. #endif
  39. #ifndef GLUT_ACTIVE_COMMAND
  40. # define GLUT_ACTIVE_COMMAND 8
  41. #endif
  42. #ifdef __APPLE__
  43. //#include <Carbon/HIToolbox/Events.h>
  44. #include <Carbon/Carbon.h>
  45. #endif
  46. #warning "igl::get_modifiers is deprecated. If using GLUT, try Alec's glut patch www.alecjacobson.com/weblog/?p=3659 and use glutGetModifiers"
  47. // FORCED INLINE
  48. inline int igl::get_modifiers()
  49. {
  50. int mod = 0;
  51. #ifdef __APPLE__
  52. // http://stackoverflow.com/a/18082326/148668
  53. KeyMap keyStates;
  54. const auto & carbon_is_keydown = [&keyStates]( uint16_t vKey )->bool
  55. {
  56. uint8_t index = vKey / 32 ;
  57. uint8_t shift = vKey % 32 ;
  58. return keyStates[index].bigEndianValue & (1 << shift) ;
  59. };
  60. GetKeys(keyStates) ;
  61. mod |= (carbon_is_keydown(kVK_Command)?GLUT_ACTIVE_COMMAND:0);
  62. mod |= (carbon_is_keydown(kVK_Shift)?GLUT_ACTIVE_SHIFT:0);
  63. mod |= (carbon_is_keydown(kVK_Option)?GLUT_ACTIVE_ALT:0);
  64. mod |= (carbon_is_keydown(kVK_Control)?GLUT_ACTIVE_CTRL:0);
  65. #else
  66. # warning "igl::get_modifiers not supported on your OS, some demos may not work correctly."
  67. #endif
  68. return mod;
  69. }
  70. #endif