Jamba C++ API  6.3.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>
21 #include <vstgui4/vstgui/lib/events.h>
24 
26 
27 using namespace VSTGUI;
28 
54 class GlobalKeyboardHook : public SelfContainedViewListener, protected IKeyboardHook
55 {
56 public:
64  std::shared_ptr<GlobalKeyboardHook> onKeyboardEvent(KeyboardEventCallback iOnKeyboardEventCallback)
65  {
66  fOnKeyboardEventCallback = std::move(iOnKeyboardEventCallback);
67  return std::dynamic_pointer_cast<GlobalKeyboardHook>(shared_from_this());
68  }
69 
77  static std::shared_ptr<GlobalKeyboardHook> create(CView *iView)
78  {
79  return SelfContainedViewListener::create<GlobalKeyboardHook>(iView);
80  }
81 
87  std::shared_ptr<SelfContainedViewListener> registerView(CView *iView) override
88  {
90  maybeRegisterKeyboardHook();
91  return res;
92  }
93 
94 protected:
98  void viewAttached(CView *iView) override
99  {
100  DCHECK_F(iView == fView);
101  maybeRegisterKeyboardHook();
102  }
103 
107  void viewRemoved(CView *iView) override
108  {
109  DCHECK_F(iView == fView);
110  maybeUnregisterKeyboardHook();
111  }
112 
113 public:
118  void unregister() override
119  {
120  maybeUnregisterKeyboardHook();
122  }
123 
126  GlobalKeyboardHook() = default;
127 
128 protected:
129  // maybeRegisterKeyboardHook
131  {
132  if(!fFrame)
133  {
134  fFrame = fView->getFrame();
135  if(fFrame)
136  {
137  fFrame->registerKeyboardHook(this);
138  }
139  }
140  }
141 
142  // maybeUnregisterKeyboardHook
144  {
145  if(fFrame)
146  {
147  fFrame->unregisterKeyboardHook(this);
148  fFrame = nullptr;
149  }
150  }
151 
155  void onKeyboardEvent(KeyboardEvent &event, CFrame *frame) override
156  {
157  if(fOnKeyboardEventCallback)
158  fOnKeyboardEventCallback(event);
159  }
160 
161 protected:
162  CFrame *fFrame{};
163  KeyboardEventCallback fOnKeyboardEventCallback{};
164 };
165 
173 inline std::shared_ptr<GlobalKeyboardHook> registerGlobalKeyboardHook(CView *iView) { return GlobalKeyboardHook::create(iView); }
174 
175 }
void viewRemoved(CView *iView) override
Overridden to handle detaching the keyboard hook from the frame.
Definition: GlobalKeyboardHook.h:107
void maybeRegisterKeyboardHook()
Definition: GlobalKeyboardHook.h:130
std::shared_ptr< SelfContainedViewListener > registerView(CView *iView) override
This method overrides SelfContainedViewListener::registerView to account for the fact that the frame ...
Definition: GlobalKeyboardHook.h:87
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
std::shared_ptr< GlobalKeyboardHook > onKeyboardEvent(KeyboardEventCallback iOnKeyboardEventCallback)
Call this method to register the callback invoked on a keyboard event.
Definition: GlobalKeyboardHook.h:64
static std::shared_ptr< GlobalKeyboardHook > create(CView *iView)
Factory method to create a global keyboard hook attached to the view.
Definition: GlobalKeyboardHook.h:77
void maybeUnregisterKeyboardHook()
Definition: GlobalKeyboardHook.h:143
void onKeyboardEvent(KeyboardEvent &event, CFrame *frame) override
Delegate to callback.
Definition: GlobalKeyboardHook.h:155
std::function< void(KeyboardEvent &)> KeyboardEventCallback
Used to register global keyboard hooks.
Definition: Types.h:67
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:118
Definition: CustomController.h:24
void viewAttached(CView *iView) override
Overridden to handle attaching the keyboard hook to the frame.
Definition: GlobalKeyboardHook.h:98
virtual std::shared_ptr< SelfContainedViewListener > registerView(CView *iView)
Registers this class as a view listener.
Definition: SelfContainedViewListener.h:134
The purpose of this class is to implement a view listener that is "self-contained":
Definition: SelfContainedViewListener.h:85
The CView class provides a method to handle keyboard events (CView::onKeyboardEvent) which only works...
Definition: GlobalKeyboardHook.h:54
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:173