Jamba C++ API  5.1.1
GlobalKeyboardHook Class Reference

The CView class provides 2 methods to handle keyboard events (CView::onKeyDown and CView::onKeyUp) which only works as long as the view itself has focus. More...

#include <GlobalKeyboardHook.h>

Inherits SelfContainedViewListener, and IKeyboardHook.

Public Member Functions

 GlobalKeyboardHook ()=default
 public constructor for the sole purpose of std::make_shared. More...
 
std::shared_ptr< GlobalKeyboardHookonKeyDown (KeyboardEventCallback iOnKeyDownCallback)
 Call this method to register the callback invoked on a key down event. More...
 
std::shared_ptr< GlobalKeyboardHookonKeyUp (KeyboardEventCallback iOnKeyUpCallback)
 Call this method to register the callback invoked on a key up event. More...
 
std::shared_ptr< SelfContainedViewListenerregisterView (CView *iView) override
 This method overrides SelfContainedViewListener::registerView to account for the fact that the frame (on which the global keyboard hook is registered) may already be available. More...
 
void unregister () override
 If you need to unregister the keyboard hook early (meaning before the view is deleted), you can do so by calling this method. More...
 
- Public Member Functions inherited from SelfContainedViewListener
 SelfContainedViewListener ()=default
 The constructor which unfortunately has to be declared public for the purpose of a creating a shared instance. More...
 

Static Public Member Functions

static std::shared_ptr< GlobalKeyboardHookcreate (CView *iView)
 Factory method to create a global keyboard hook attached to the view. More...
 
- Static Public Member Functions inherited from SelfContainedViewListener
template<typename T , typename ... Args>
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. More...
 

Protected Member Functions

void maybeRegisterKeyboardHook ()
 
void maybeUnregisterKeyboardHook ()
 
int32_t onKeyDown (const VstKeyCode &iCode, CFrame *frame) override
 Delegate to callback. More...
 
int32_t onKeyUp (const VstKeyCode &iCode, CFrame *frame) override
 Delegate to callback. More...
 
void viewAttached (CView *iView) override
 Overridden to handle attaching the keyboard hook to the frame. More...
 
void viewRemoved (CView *iView) override
 Overridden to handle detaching the keyboard hook from the frame. More...
 
- Protected Member Functions inherited from SelfContainedViewListener
void viewWillDelete (CView *iView) override
 Called by the view itself when it is going to be deleted. More...
 

Protected Attributes

CFrame * fFrame {}
 
KeyboardEventCallback fOnKeyDownCallback {}
 
KeyboardEventCallback fOnKeyUpCallback {}
 
- Protected Attributes inherited from SelfContainedViewListener
std::shared_ptr< SelfContainedViewListenerfThis {}
 
CView * fView {}
 

Detailed Description

The CView class provides 2 methods to handle keyboard events (CView::onKeyDown and CView::onKeyUp) which only works as long as the view itself has focus.

This helper class is designed to set a keyboard hook which is global so it will work whether the view has the focus or not. This class automatically handles registering the hook to the frame (when it becomes available) and unregistering it automatically as well. As an added bonus it is possible to dynamically change the callbacks after registration.

Typical usage would be to use Views::registerGlobalKeyboardHook() this way:

// assuming we are writing a custom view
void initState(GUIState *iGUIState)
{
->onKeyDown([this](VstKeyCode const &iKeyCode) -> auto {
if(iKeyCode.character == 'z')
{
// do something...
return CKeyboardEventResult::kKeyboardEventHandled;
}
})
->onKeyUp([this](VstKeyCode const &iKeyCode) -> auto {
if(iKeyCode.character == 'z')
{
// do something else...
return CKeyboardEventResult::kKeyboardEventHandled;
}
});
}

Constructor & Destructor Documentation

◆ GlobalKeyboardHook()

GlobalKeyboardHook ( )
default

public constructor for the sole purpose of std::make_shared.

Do not call directly

Member Function Documentation

◆ create()

static std::shared_ptr<GlobalKeyboardHook> create ( CView *  iView)
inlinestatic

Factory method to create a global keyboard hook attached to the view.

You can use the convenient global function Views::registerGlobalKeyboardHook instead.

Parameters
iViewthe target view of the keyboard events
Returns
the helper class to register callback on

◆ maybeRegisterKeyboardHook()

void maybeRegisterKeyboardHook ( )
inlineprotected

◆ maybeUnregisterKeyboardHook()

void maybeUnregisterKeyboardHook ( )
inlineprotected

◆ onKeyDown() [1/2]

std::shared_ptr<GlobalKeyboardHook> onKeyDown ( KeyboardEventCallback  iOnKeyDownCallback)
inline

Call this method to register the callback invoked on a key down event.

It replaces any other if there was one and is optional.

Parameters
iOnKeyDownCallbacksee GUI::KeyboardEventCallback for the function definition
Returns
itself (as a shared pointer) for chaining calls (see example at the top)

◆ onKeyDown() [2/2]

int32_t onKeyDown ( const VstKeyCode &  iCode,
CFrame *  frame 
)
inlineoverrideprotected

Delegate to callback.

◆ onKeyUp() [1/2]

std::shared_ptr<GlobalKeyboardHook> onKeyUp ( KeyboardEventCallback  iOnKeyUpCallback)
inline

Call this method to register the callback invoked on a key up event.

It replaces any other if there was one and is optional.

Parameters
iOnKeyUpCallbacksee GUI::KeyboardEventCallback for the function definition
Returns
itself (as a shared pointer) for chaining calls (see example at the top)

◆ onKeyUp() [2/2]

int32_t onKeyUp ( const VstKeyCode &  iCode,
CFrame *  frame 
)
inlineoverrideprotected

Delegate to callback.

◆ registerView()

std::shared_ptr<SelfContainedViewListener> registerView ( CView *  iView)
inlineoverridevirtual

This method overrides SelfContainedViewListener::registerView to account for the fact that the frame (on which the global keyboard hook is registered) may already be available.

We just don't know at which point in the lifecycle of the view, this method is being called.

Reimplemented from SelfContainedViewListener.

◆ unregister()

void unregister ( )
inlineoverridevirtual

If you need to unregister the keyboard hook early (meaning before the view is deleted), you can do so by calling this method.

Reimplemented from SelfContainedViewListener.

◆ viewAttached()

void viewAttached ( CView *  iView)
inlineoverrideprotected

Overridden to handle attaching the keyboard hook to the frame.

◆ viewRemoved()

void viewRemoved ( CView *  iView)
inlineoverrideprotected

Overridden to handle detaching the keyboard hook from the frame.

Member Data Documentation

◆ fFrame

CFrame* fFrame {}
protected

◆ fOnKeyDownCallback

KeyboardEventCallback fOnKeyDownCallback {}
protected

◆ fOnKeyUpCallback

KeyboardEventCallback fOnKeyUpCallback {}
protected

The documentation for this class was generated from the following file: