KeyboardMacOS.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Input/MacOS/KeyboardMacOS.h>
  6. #import <GameController/GameController.h>
  7. static EKey sToKey(GCKeyCode inValue)
  8. {
  9. if (inValue == GCKeyCodeKeyA) return EKey::A;
  10. if (inValue == GCKeyCodeKeyB) return EKey::B;
  11. if (inValue == GCKeyCodeKeyC) return EKey::C;
  12. if (inValue == GCKeyCodeKeyD) return EKey::D;
  13. if (inValue == GCKeyCodeKeyE) return EKey::E;
  14. if (inValue == GCKeyCodeKeyF) return EKey::F;
  15. if (inValue == GCKeyCodeKeyG) return EKey::G;
  16. if (inValue == GCKeyCodeKeyH) return EKey::H;
  17. if (inValue == GCKeyCodeKeyI) return EKey::I;
  18. if (inValue == GCKeyCodeKeyJ) return EKey::J;
  19. if (inValue == GCKeyCodeKeyK) return EKey::K;
  20. if (inValue == GCKeyCodeKeyL) return EKey::L;
  21. if (inValue == GCKeyCodeKeyM) return EKey::M;
  22. if (inValue == GCKeyCodeKeyN) return EKey::N;
  23. if (inValue == GCKeyCodeKeyO) return EKey::O;
  24. if (inValue == GCKeyCodeKeyP) return EKey::P;
  25. if (inValue == GCKeyCodeKeyQ) return EKey::Q;
  26. if (inValue == GCKeyCodeKeyR) return EKey::R;
  27. if (inValue == GCKeyCodeKeyS) return EKey::S;
  28. if (inValue == GCKeyCodeKeyT) return EKey::T;
  29. if (inValue == GCKeyCodeKeyU) return EKey::U;
  30. if (inValue == GCKeyCodeKeyV) return EKey::V;
  31. if (inValue == GCKeyCodeKeyW) return EKey::W;
  32. if (inValue == GCKeyCodeKeyX) return EKey::X;
  33. if (inValue == GCKeyCodeKeyY) return EKey::Y;
  34. if (inValue == GCKeyCodeKeyZ) return EKey::Z;
  35. if (inValue == GCKeyCodeZero) return EKey::Num0;
  36. if (inValue == GCKeyCodeOne) return EKey::Num1;
  37. if (inValue == GCKeyCodeTwo) return EKey::Num2;
  38. if (inValue == GCKeyCodeThree) return EKey::Num3;
  39. if (inValue == GCKeyCodeFour) return EKey::Num4;
  40. if (inValue == GCKeyCodeFive) return EKey::Num5;
  41. if (inValue == GCKeyCodeSix) return EKey::Num6;
  42. if (inValue == GCKeyCodeSeven) return EKey::Num7;
  43. if (inValue == GCKeyCodeEight) return EKey::Num8;
  44. if (inValue == GCKeyCodeNine) return EKey::Num9;
  45. if (inValue == GCKeyCodeSpacebar) return EKey::Space;
  46. if (inValue == GCKeyCodeComma) return EKey::Comma;
  47. if (inValue == GCKeyCodePeriod) return EKey::Period;
  48. if (inValue == GCKeyCodeEscape) return EKey::Escape;
  49. if (inValue == GCKeyCodeLeftShift) return EKey::LShift;
  50. if (inValue == GCKeyCodeRightShift) return EKey::RShift;
  51. if (inValue == GCKeyCodeLeftControl) return EKey::LControl;
  52. if (inValue == GCKeyCodeRightControl) return EKey::RControl;
  53. if (inValue == GCKeyCodeLeftAlt) return EKey::LAlt;
  54. if (inValue == GCKeyCodeRightAlt) return EKey::RAlt;
  55. if (inValue == GCKeyCodeLeftArrow) return EKey::Left;
  56. if (inValue == GCKeyCodeRightArrow) return EKey::Right;
  57. if (inValue == GCKeyCodeUpArrow) return EKey::Up;
  58. if (inValue == GCKeyCodeDownArrow) return EKey::Down;
  59. if (inValue == GCKeyCodeReturnOrEnter) return EKey::Return;
  60. return EKey::Unknown;
  61. }
  62. // This class receives keyboard connect callbacks
  63. @interface KeyboardDelegate : NSObject
  64. @end
  65. @implementation KeyboardDelegate
  66. {
  67. KeyboardMacOS *mKeyboard;
  68. }
  69. - (KeyboardDelegate *)init:(KeyboardMacOS *)Keyboard
  70. {
  71. mKeyboard = Keyboard;
  72. [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent *(NSEvent *event) {
  73. // Ignore all keystrokes except Command-Q (Quit).
  74. if ((event.modifierFlags & NSEventModifierFlagCommand) && [event.charactersIgnoringModifiers isEqual:@"q"])
  75. return event;
  76. else
  77. return nil;
  78. }];
  79. return self;
  80. }
  81. - (void)keyboardDidConnect:(NSNotification *)notification
  82. {
  83. GCKeyboard *keyboard = (GCKeyboard *)notification.object;
  84. if (!keyboard)
  85. return;
  86. __block KeyboardDelegate *weakSelf = self;
  87. keyboard.keyboardInput.keyChangedHandler = ^(GCKeyboardInput *keyboard, GCControllerButtonInput *key, GCKeyCode keyCode, BOOL pressed) {
  88. KeyboardDelegate *strongSelf = weakSelf;
  89. if (strongSelf == nil)
  90. return;
  91. EKey ekey = sToKey(keyCode);
  92. if (ekey != EKey::Invalid)
  93. strongSelf->mKeyboard->OnKeyPressed(ekey, pressed);
  94. };
  95. }
  96. @end
  97. bool KeyboardMacOS::Initialize(ApplicationWindow *inWindow)
  98. {
  99. KeyboardDelegate *delegate = [[KeyboardDelegate alloc] init: this];
  100. [NSNotificationCenter.defaultCenter addObserver: delegate selector: @selector(keyboardDidConnect:) name: GCKeyboardDidConnectNotification object: nil];
  101. return true;
  102. }
  103. void KeyboardMacOS::Poll()
  104. {
  105. // Make the pending buffer the active buffer
  106. mKeyBuffer = mPendingKeyBuffer;
  107. mPendingKeyBuffer.clear();
  108. }
  109. EKey KeyboardMacOS::GetFirstKey()
  110. {
  111. mCurrentKey = 0;
  112. return GetNextKey();
  113. }
  114. EKey KeyboardMacOS::GetNextKey()
  115. {
  116. if (mCurrentKey < mKeyBuffer.size())
  117. return mKeyBuffer[mCurrentKey++];
  118. return EKey::Invalid;
  119. }
  120. void KeyboardMacOS::OnKeyPressed(EKey inKey, bool inPressed)
  121. {
  122. if (inPressed && mPendingKeyBuffer.size() < mPendingKeyBuffer.capacity())
  123. mPendingKeyBuffer.push_back(inKey);
  124. mKeyPressed[(int)inKey] = inPressed;
  125. }