Jamba  3.1.0
pongasoft::VST::GUI::Views::GlobalKeyboardHook Class Reference

#include <GlobalKeyboardHook.h>

Inherits pongasoft::VST::GUI::Views::SelfContainedViewListener, and IKeyboardHook.

Public Member Functions

 GlobalKeyboardHook ()=default
 
std::shared_ptr< GlobalKeyboardHook > onKeyDown (KeyboardEventCallback iOnKeyDownCallback)
 
std::shared_ptr< GlobalKeyboardHook > onKeyUp (KeyboardEventCallback iOnKeyUpCallback)
 
std::shared_ptr< SelfContainedViewListener > registerView (CView *iView) override
 
void unregister () override
 
- Public Member Functions inherited from pongasoft::VST::GUI::Views::SelfContainedViewListener
 SelfContainedViewListener ()=default
 

Static Public Member Functions

static std::shared_ptr< GlobalKeyboardHook > create (CView *iView)
 
- Static Public Member Functions inherited from pongasoft::VST::GUI::Views::SelfContainedViewListener
template<typename T , typename ... Args>
static std::shared_ptr< T > create (CView *iView, Args &&...iArgs)
 

Protected Member Functions

void maybeRegisterKeyboardHook ()
 
void maybeUnregisterKeyboardHook ()
 
int32_t onKeyDown (const VstKeyCode &iCode, CFrame *frame) override
 
int32_t onKeyUp (const VstKeyCode &iCode, CFrame *frame) override
 
void viewAttached (CView *iView) override
 
void viewRemoved (CView *iView) override
 
- Protected Member Functions inherited from pongasoft::VST::GUI::Views::SelfContainedViewListener
void viewWillDelete (CView *iView) override
 

Protected Attributes

CFrame * fFrame {}
 
KeyboardEventCallback fOnKeyDownCallback {}
 
KeyboardEventCallback fOnKeyUpCallback {}
 
- Protected Attributes inherited from pongasoft::VST::GUI::Views::SelfContainedViewListener
std::shared_ptr< SelfContainedViewListener > fThis {}
 
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)
{
  CustomView::initState(iGUIState);
  Views::registerGlobalKeyboardHook(this)
    ->onKeyDown([this](VstKeyCode const &iKeyCode) -> auto {
      if(iKeyCode.character == 'z')
      {
        // do something...
        return CKeyboardEventResult::kKeyboardEventHandled;
      }
      return CKeyboardEventResult::kKeyboardEventNotHandled;
    })
    ->onKeyUp([this](VstKeyCode const &iKeyCode) -> auto {
      if(iKeyCode.character == 'z')
      {
        // do something else...
        return CKeyboardEventResult::kKeyboardEventHandled;
      }
      return CKeyboardEventResult::kKeyboardEventNotHandled;
    });
 }

Constructor & Destructor Documentation

◆ GlobalKeyboardHook()

pongasoft::VST::GUI::Views::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> pongasoft::VST::GUI::Views::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 pongasoft::VST::GUI::Views::GlobalKeyboardHook::maybeRegisterKeyboardHook ( )
inlineprotected

◆ maybeUnregisterKeyboardHook()

void pongasoft::VST::GUI::Views::GlobalKeyboardHook::maybeUnregisterKeyboardHook ( )
inlineprotected

◆ onKeyDown() [1/2]

std::shared_ptr<GlobalKeyboardHook> pongasoft::VST::GUI::Views::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 pongasoft::VST::GUI::Views::GlobalKeyboardHook::onKeyDown ( const VstKeyCode &  iCode,
CFrame *  frame 
)
inlineoverrideprotected

Delegate to callback

◆ onKeyUp() [1/2]

std::shared_ptr<GlobalKeyboardHook> pongasoft::VST::GUI::Views::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 pongasoft::VST::GUI::Views::GlobalKeyboardHook::onKeyUp ( const VstKeyCode &  iCode,
CFrame *  frame 
)
inlineoverrideprotected

Delegate to callback

◆ registerView()

std::shared_ptr<SelfContainedViewListener> pongasoft::VST::GUI::Views::GlobalKeyboardHook::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 pongasoft::VST::GUI::Views::SelfContainedViewListener.

◆ unregister()

void pongasoft::VST::GUI::Views::GlobalKeyboardHook::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 pongasoft::VST::GUI::Views::SelfContainedViewListener.

◆ viewAttached()

void pongasoft::VST::GUI::Views::GlobalKeyboardHook::viewAttached ( CView *  iView)
inlineoverrideprotected

Overridden to handle attaching the keyboard hook to the frame

◆ viewRemoved()

void pongasoft::VST::GUI::Views::GlobalKeyboardHook::viewRemoved ( CView *  iView)
inlineoverrideprotected

Overridden to handle detaching the keyboard hook from the frame

Member Data Documentation

◆ fFrame

CFrame* pongasoft::VST::GUI::Views::GlobalKeyboardHook::fFrame {}
protected

◆ fOnKeyDownCallback

KeyboardEventCallback pongasoft::VST::GUI::Views::GlobalKeyboardHook::fOnKeyDownCallback {}
protected

◆ fOnKeyUpCallback

KeyboardEventCallback pongasoft::VST::GUI::Views::GlobalKeyboardHook::fOnKeyUpCallback {}
protected

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