Tab key Issue


#1

Hi All,

I want to add a feature to my app which has lots of tabs. The feature is similar to mozilla firefox way of handling tabs. i.e. ctrl+tab must go to the next tab and ctrl+shift+tab must go to the previous tab.
I am using juce_1_53 for my app. This is code that i have written for the above feature i wish to add.
switch (commandID)
{
case SELECT_NEXT_TAB:
result.setInfo(T(“Select Next Tab”),T(“Select Next Tab”),T(“Action”), 0);
result.addDefaultKeypress( juce::KeyPress::tabKey , juce::ModifierKeys::commandModifier);
break;
case SELECT_PREVIOUS_TAB:
result.setInfo(T(“Select Previous Tab”),T(“Select Previous Tab”),T(“Action”), 0);
result.addDefaultKeypress( juce::KeyPress::tabKey , ( juce::ModifierKeys::commandModifier | juce::ModifierKeys::shiftModifier ) );
break;
}

and the perform method is as follows:
switch(info.commandID)
{
case SELECT_NEXT_TAB:
{
ILCustomizeComp* pComp = static_cast<ILCustomizeComp*>( ILUIFactory::GetInstance()->GetCustomizeComponent() ) ;
if( pComp )
{
juce::TabbedComponent* pTab = pComp->GetTabbedComp() ;
if( pTab )
{
int iCurrentTabIndex = ILUIStateManager::GetActiveTab() ;
if( iCurrentTabIndex+1 >= pTab->getNumTabs() )
iCurrentTabIndex = 0 ;
else
iCurrentTabIndex++ ;
pTab->setCurrentTabIndex( iCurrentTabIndex , true ) ;
}
}
bCommandPerformed = true ;
}
break;
case SELECT_PREVIOUS_TAB:
{
ILCustomizeComp* pComp = static_cast<ILCustomizeComp*>( ILUIFactory::GetInstance()->GetCustomizeComponent() ) ;
if( pComp )
{
juce::TabbedComponent* pTab = pComp->GetTabbedComp() ;
if( pTab )
{
int iCurrentTabIndex = ILUIStateManager::GetActiveTab() ;
if( iCurrentTabIndex-1 < 0 )
iCurrentTabIndex = pTab->getNumTabs()-1 ;
else
iCurrentTabIndex-- ;
pTab->setCurrentTabIndex( iCurrentTabIndex , true ) ;
}
}
bCommandPerformed = true ;
}
break;
}

This piece of code is not performing at all. i.e. I am not able to move to different tabs using this. But if a modify the code a bit, and use direction keys instead of tab key its working fine.
Please help me fix the issue or give me a suggestion on how i can get this feature done.
Thanks in Advance,
Adit


#2

I see that this problem is due to… this code in juce_Component_peer class…

bool ComponentPeer::handleKeyPress (const int keyCode,
const juce_wchar textCharacter)
{
updateCurrentModifiers();

Component* target = Component::getCurrentlyFocusedComponent() != nullptr
                        ? Component::getCurrentlyFocusedComponent()
                        : component;

if (target->isCurrentlyBlockedByAnotherModalComponent())
{
    Component* const currentModalComp = Component::getCurrentlyModalComponent();

    if (currentModalComp != nullptr)
        target = currentModalComp;
}

const KeyPress keyInfo (keyCode,
                        ModifierKeys::getCurrentModifiers().getRawFlags()
                           & ModifierKeys::allKeyboardModifiers,
                        textCharacter);

bool keyWasUsed = false;

while (target != nullptr)
{
    const WeakReference<Component> deletionChecker (target);
    const Array <KeyListener*>* const keyListeners = target->keyListeners;

    if (keyListeners != nullptr)
    {
        for (int i = keyListeners->size(); --i >= 0;)
        {
            keyWasUsed = keyListeners->getUnchecked(i)->keyPressed (keyInfo, target);

            if (keyWasUsed || deletionChecker == nullptr)
                return keyWasUsed;

            i = jmin (i, keyListeners->size());
        }
    }

    keyWasUsed = target->keyPressed (keyInfo);

    if (keyWasUsed || deletionChecker == nullptr)
        break;

/*************************** The problamatic code according to me in juce*********************************************/

—> if the key press has a tab key code… just transfer the focus to sibling component…

    if (keyInfo.isKeyCode (KeyPress::tabKey) &&  Component::getCurrentlyFocusedComponent() != nullptr)
    {
        Component* const currentlyFocused = Component::getCurrentlyFocusedComponent();
        currentlyFocused->moveKeyboardFocusToSibling (! keyInfo.getModifiers().isShiftDown());
        keyWasUsed = (currentlyFocused != Component::getCurrentlyFocusedComponent());
        break;
    }

    target = target->getParentComponent();
}

return keyWasUsed;

}


#3

(Please learn to use the ‘code’ tag when posting code!)

The code you mark as “problematic” should never actually be reached, because the keypress is consumed before it gets that far. At least, in my codebase that’s true, but since you’re using such an old version, I can’t guarantee that it’ll be the same - I can only really offer to help with the latest version.