Jamba C++ API  4.0.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 
25 
26 using namespace VSTGUI;
27 
62 class GlobalKeyboardHook : public SelfContainedViewListener, protected IKeyboardHook
63 {
64 public:
72  std::shared_ptr<GlobalKeyboardHook> onKeyDown(KeyboardEventCallback iOnKeyDownCallback)
73  {
74  fOnKeyDownCallback = std::move(iOnKeyDownCallback);
75  return std::dynamic_pointer_cast<GlobalKeyboardHook>(shared_from_this());
76  }
77 
85  std::shared_ptr<GlobalKeyboardHook> onKeyUp(KeyboardEventCallback iOnKeyUpCallback)
86  {
87  fOnKeyUpCallback = std::move(iOnKeyUpCallback);
88  return std::dynamic_pointer_cast<GlobalKeyboardHook>(shared_from_this());
89  }
90 
98  static std::shared_ptr<GlobalKeyboardHook> create(CView *iView)
99  {
100  return SelfContainedViewListener::create<GlobalKeyboardHook>(iView);
101  }
102 
108  std::shared_ptr<SelfContainedViewListener> registerView(CView *iView) override
109  {
110  auto res = SelfContainedViewListener::registerView(iView);
111  maybeRegisterKeyboardHook();
112  return res;
113  }
114 
115 protected:
119  void viewAttached(CView *iView) override
120  {
121  DCHECK_F(iView == fView);
122  maybeRegisterKeyboardHook();
123  }
124 
128  void viewRemoved(CView *iView) override
129  {
130  DCHECK_F(iView == fView);
131  maybeUnregisterKeyboardHook();
132  }
133 
134 public:
139  void unregister() override
140  {
141  maybeUnregisterKeyboardHook();
143  }
144 
147  GlobalKeyboardHook() = default;
148 
149 protected:
150  // maybeRegisterKeyboardHook
152  {
153  if(!fFrame)
154  {
155  fFrame = fView->getFrame();
156  if(fFrame)
157  {
158  fFrame->registerKeyboardHook(this);
159  }
160  }
161  }
162 
163  // maybeUnregisterKeyboardHook
165  {
166  if(fFrame)
167  {
168  fFrame->unregisterKeyboardHook(this);
169  fFrame = nullptr;
170  }
171  }
172 
176  int32_t onKeyDown(const VstKeyCode &iCode, CFrame *frame) override
177  {
178  return fOnKeyDownCallback ? fOnKeyDownCallback(iCode): CKeyboardEventResult::kKeyboardEventNotHandled;
179  }
180 
184  int32_t onKeyUp(const VstKeyCode &iCode, CFrame *frame) override
185  {
186  return fOnKeyUpCallback ? fOnKeyUpCallback(iCode): CKeyboardEventResult::kKeyboardEventNotHandled;
187  }
188 
189 protected:
190  CFrame *fFrame{};
191  KeyboardEventCallback fOnKeyDownCallback{};
192  KeyboardEventCallback fOnKeyUpCallback{};
193 };
194 
202 inline std::shared_ptr<GlobalKeyboardHook> registerGlobalKeyboardHook(CView *iView) { return GlobalKeyboardHook::create(iView); }
203 
204 }
void viewRemoved(CView *iView) override
Overridden to handle detaching the keyboard hook from the frame.
Definition: GlobalKeyboardHook.h:128
void maybeRegisterKeyboardHook()
Definition: GlobalKeyboardHook.h:151
std::shared_ptr< GlobalKeyboardHook > onKeyDown(KeyboardEventCallback iOnKeyDownCallback)
Call this method to register the callback invoked on a key down event.
Definition: GlobalKeyboardHook.h:72
std::shared_ptr< SelfContainedViewListener > registerView(CView *iView) override
This method overrides SelfContainedViewListener::registerView to account for the fact that the frame ...
Definition: GlobalKeyboardHook.h:108
virtual void unregister()
You can call this method at anytime to unregister this class as the listener to the view that was pre...
Definition: SelfContainedViewListener.h:114
static std::shared_ptr< GlobalKeyboardHook > create(CView *iView)
Factory method to create a global keyboard hook attached to the view.
Definition: GlobalKeyboardHook.h:98
void maybeUnregisterKeyboardHook()
Definition: GlobalKeyboardHook.h:164
Definition: Types.h:29
void unregister() override
If you need to unregister the keyboard hook early (meaning before the view is deleted),...
Definition: GlobalKeyboardHook.h:139
Definition: CustomController.h:24
void viewAttached(CView *iView) override
Overridden to handle attaching the keyboard hook to the frame.
Definition: GlobalKeyboardHook.h:119
virtual std::shared_ptr< SelfContainedViewListener > registerView(CView *iView)
Registers this class as a view listener.
Definition: SelfContainedViewListener.h:134
int32_t onKeyUp(const VstKeyCode &iCode, CFrame *frame) override
Delegate to callback.
Definition: GlobalKeyboardHook.h:184
The purpose of this class is to implement a view listener that is "self-contained":
Definition: SelfContainedViewListener.h:85
The CView class provides 2 methods to handle keyboard events (CView::onKeyDown and CView::onKeyUp) wh...
Definition: GlobalKeyboardHook.h:62
std::shared_ptr< GlobalKeyboardHook > registerGlobalKeyboardHook(CView *iView)
This is the main entry point that you should use to register a global keyboard hook.
Definition: GlobalKeyboardHook.h:202
std::function< CKeyboardEventResult(VstKeyCode const &)> KeyboardEventCallback
Used to register global keyboard hooks.
Definition: Types.h:68
Definition: Types.h:36
std::shared_ptr< GlobalKeyboardHook > onKeyUp(KeyboardEventCallback iOnKeyUpCallback)
Call this method to register the callback invoked on a key up event.
Definition: GlobalKeyboardHook.h:85
int32_t onKeyDown(const VstKeyCode &iCode, CFrame *frame) override
Delegate to callback.
Definition: GlobalKeyboardHook.h:176