Jamba  3.2.0
GlobalKeyboardHook.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <vstgui4/vstgui/lib/cframe.h>
23 
24 namespace pongasoft {
25 namespace VST {
26 namespace GUI {
27 namespace Views {
28 
29 using namespace VSTGUI;
30 
63 class GlobalKeyboardHook : public SelfContainedViewListener, protected IKeyboardHook
64 {
65 public:
73  std::shared_ptr<GlobalKeyboardHook> onKeyDown(KeyboardEventCallback iOnKeyDownCallback)
74  {
75  fOnKeyDownCallback = std::move(iOnKeyDownCallback);
76  return std::dynamic_pointer_cast<GlobalKeyboardHook>(shared_from_this());
77  }
78 
86  std::shared_ptr<GlobalKeyboardHook> onKeyUp(KeyboardEventCallback iOnKeyUpCallback)
87  {
88  fOnKeyUpCallback = std::move(iOnKeyUpCallback);
89  return std::dynamic_pointer_cast<GlobalKeyboardHook>(shared_from_this());
90  }
91 
99  static std::shared_ptr<GlobalKeyboardHook> create(CView *iView)
100  {
101  return SelfContainedViewListener::create<GlobalKeyboardHook>(iView);
102  }
103 
109  std::shared_ptr<SelfContainedViewListener> registerView(CView *iView) override
110  {
111  auto res = SelfContainedViewListener::registerView(iView);
112  maybeRegisterKeyboardHook();
113  return res;
114  }
115 
116 protected:
120  void viewAttached(CView *iView) override
121  {
122  DCHECK_F(iView == fView);
123  maybeRegisterKeyboardHook();
124  }
125 
129  void viewRemoved(CView *iView) override
130  {
131  DCHECK_F(iView == fView);
132  maybeUnregisterKeyboardHook();
133  }
134 
135 public:
140  void unregister() override
141  {
142  maybeUnregisterKeyboardHook();
144  }
145 
148  GlobalKeyboardHook() = default;
149 
150 protected:
151  // maybeRegisterKeyboardHook
153  {
154  if(!fFrame)
155  {
156  fFrame = fView->getFrame();
157  if(fFrame)
158  {
159  fFrame->registerKeyboardHook(this);
160  }
161  }
162  }
163 
164  // maybeUnregisterKeyboardHook
166  {
167  if(fFrame)
168  {
169  fFrame->unregisterKeyboardHook(this);
170  fFrame = nullptr;
171  }
172  }
173 
177  int32_t onKeyDown(const VstKeyCode &iCode, CFrame *frame) override
178  {
179  return fOnKeyDownCallback ? fOnKeyDownCallback(iCode): CKeyboardEventResult::kKeyboardEventNotHandled;
180  }
181 
185  int32_t onKeyUp(const VstKeyCode &iCode, CFrame *frame) override
186  {
187  return fOnKeyUpCallback ? fOnKeyUpCallback(iCode): CKeyboardEventResult::kKeyboardEventNotHandled;
188  }
189 
190 protected:
191  CFrame *fFrame{};
192  KeyboardEventCallback fOnKeyDownCallback{};
193  KeyboardEventCallback fOnKeyUpCallback{};
194 };
195 
203 inline std::shared_ptr<GlobalKeyboardHook> registerGlobalKeyboardHook(CView *iView) { return GlobalKeyboardHook::create(iView); }
204 
205 }
206 }
207 }
208 }
void viewRemoved(CView *iView) override
Definition: GlobalKeyboardHook.h:129
virtual void unregister()
Definition: SelfContainedViewListener.h:109
std::shared_ptr< SelfContainedViewListener > registerView(CView *iView) override
Definition: GlobalKeyboardHook.h:109
int32_t onKeyDown(const VstKeyCode &iCode, CFrame *frame) override
Definition: GlobalKeyboardHook.h:177
Definition: Clock.h:22
void maybeUnregisterKeyboardHook()
Definition: GlobalKeyboardHook.h:165
std::shared_ptr< GlobalKeyboardHook > onKeyUp(KeyboardEventCallback iOnKeyUpCallback)
Definition: GlobalKeyboardHook.h:86
static std::shared_ptr< GlobalKeyboardHook > create(CView *iView)
Definition: GlobalKeyboardHook.h:99
std::function< CKeyboardEventResult(VstKeyCode const &)> KeyboardEventCallback
Definition: Types.h:68
int32_t onKeyUp(const VstKeyCode &iCode, CFrame *frame) override
Definition: GlobalKeyboardHook.h:185
Definition: Types.h:29
Definition: Types.h:36
void maybeRegisterKeyboardHook()
Definition: GlobalKeyboardHook.h:152
Definition: SelfContainedViewListener.h:83
Definition: GlobalKeyboardHook.h:63
std::shared_ptr< GlobalKeyboardHook > onKeyDown(KeyboardEventCallback iOnKeyDownCallback)
Definition: GlobalKeyboardHook.h:73
virtual std::shared_ptr< SelfContainedViewListener > registerView(CView *iView)
Definition: SelfContainedViewListener.h:129
std::shared_ptr< GlobalKeyboardHook > registerGlobalKeyboardHook(CView *iView)
Definition: GlobalKeyboardHook.h:203
void unregister() override
Definition: GlobalKeyboardHook.h:140
void viewAttached(CView *iView) override
Definition: GlobalKeyboardHook.h:120