Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
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 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <vstgui4/vstgui/lib/cframe.h>
22#include <vstgui4/vstgui/lib/events.h>
25
27
28using namespace VSTGUI;
29
55class GlobalKeyboardHook : public SelfContainedViewListener, protected IKeyboardHook
56{
57public:
65 std::shared_ptr<GlobalKeyboardHook> onKeyboardEvent(KeyboardEventCallback iOnKeyboardEventCallback)
66 {
67 fOnKeyboardEventCallback = std::move(iOnKeyboardEventCallback);
68 return std::dynamic_pointer_cast<GlobalKeyboardHook>(shared_from_this());
69 }
70
78 static std::shared_ptr<GlobalKeyboardHook> create(CView *iView)
79 {
81 }
82
88 std::shared_ptr<SelfContainedViewListener> registerView(CView *iView) override
89 {
92 return res;
93 }
94
95protected:
99 void viewAttached(CView *iView) override
100 {
101 DCHECK_F(iView == fView);
103 }
104
108 void viewRemoved(CView *iView) override
109 {
110 DCHECK_F(iView == fView);
112 }
113
114public:
124
128
129protected:
130 // maybeRegisterKeyboardHook
132 {
133 if(!fFrame)
134 {
135 fFrame = fView->getFrame();
136 if(fFrame)
137 {
138 fFrame->registerKeyboardHook(this);
139 }
140 }
141 }
142
143 // maybeUnregisterKeyboardHook
145 {
146 if(fFrame)
147 {
148 fFrame->unregisterKeyboardHook(this);
149 fFrame = nullptr;
150 }
151 }
152
156 void onKeyboardEvent(KeyboardEvent &event, CFrame *frame) override
157 {
160 }
161
162protected:
163 CFrame *fFrame{};
165};
166
174inline std::shared_ptr<GlobalKeyboardHook> registerGlobalKeyboardHook(CView *iView) { return GlobalKeyboardHook::create(iView); }
175
176}
void maybeRegisterKeyboardHook()
Definition GlobalKeyboardHook.h:131
std::shared_ptr< GlobalKeyboardHook > onKeyboardEvent(KeyboardEventCallback iOnKeyboardEventCallback)
Call this method to register the callback invoked on a keyboard event.
Definition GlobalKeyboardHook.h:65
std::shared_ptr< SelfContainedViewListener > registerView(CView *iView) override
This method overrides SelfContainedViewListener::registerView to account for the fact that the frame ...
Definition GlobalKeyboardHook.h:88
void viewAttached(CView *iView) override
Overridden to handle attaching the keyboard hook to the frame.
Definition GlobalKeyboardHook.h:99
GlobalKeyboardHook()=default
public constructor for the sole purpose of std::make_shared.
CFrame * fFrame
Definition GlobalKeyboardHook.h:163
void viewRemoved(CView *iView) override
Overridden to handle detaching the keyboard hook from the frame.
Definition GlobalKeyboardHook.h:108
void unregister() override
If you need to unregister the keyboard hook early (meaning before the view is deleted),...
Definition GlobalKeyboardHook.h:119
KeyboardEventCallback fOnKeyboardEventCallback
Definition GlobalKeyboardHook.h:164
void onKeyboardEvent(KeyboardEvent &event, CFrame *frame) override
Delegate to callback.
Definition GlobalKeyboardHook.h:156
static std::shared_ptr< GlobalKeyboardHook > create(CView *iView)
Factory method to create a global keyboard hook attached to the view.
Definition GlobalKeyboardHook.h:78
void maybeUnregisterKeyboardHook()
Definition GlobalKeyboardHook.h:144
static std::shared_ptr< T > create(CView *iView, Args &&...iArgs)
This is the main method that should be used to create an instance of this class.
Definition SelfContainedViewListener.h:100
SelfContainedViewListener()=default
The constructor which unfortunately has to be declared public for the purpose of a creating a shared ...
CView * fView
Definition SelfContainedViewListener.h:164
virtual std::shared_ptr< SelfContainedViewListener > registerView(CView *iView)
Registers this class as a view listener.
Definition SelfContainedViewListener.h:135
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:115
Definition CustomController.h:25
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:174
std::function< void(KeyboardEvent &)> KeyboardEventCallback
Used to register global keyboard hooks.
Definition Types.h:68