Keys swallowed under Cubase & Windows (with fix)

Dear forum,

Currently if VST plugin has a TextEdit on it, it consumes keys it shouldn't. I think fix is the following, am I right?

juce_win32_Windowing.cpp:

            case VK_LEFT:
            case VK_RIGHT:
            case VK_UP:
            case VK_DOWN:
            case VK_PRIOR:
            case VK_NEXT:
            case VK_HOME:
            case VK_END:
            case VK_DELETE:
            case VK_INSERT:
            case VK_F1:
            case VK_F2:
            case VK_F3:
            case VK_F4:
            case VK_F5:
            case VK_F6:
            case VK_F7:
            case VK_F8:
            case VK_F9:
            case VK_F10:
            case VK_F11:
            case VK_F12:
            case VK_F13:
            case VK_F14:
            case VK_F15:
            case VK_F16:
                used = handleKeyUpOrDown (true);
                used = handleKeyPress (extendedKeyModifier | (int) key, 0) || used;
                break;

Should be:

            case VK_UP:
            case VK_DOWN:
                used = handleKeyUpOrDown (true); //! fix for keys
            case VK_LEFT:
            case VK_RIGHT:
            case VK_PRIOR:
            case VK_NEXT:
            case VK_HOME:
            case VK_END:
            case VK_DELETE:
            case VK_INSERT:
            case VK_F1:
            case VK_F2:
            case VK_F3:
            case VK_F4:
            case VK_F5:
            case VK_F6:
            case VK_F7:
            case VK_F8:
            case VK_F9:
            case VK_F10:
            case VK_F11:
            case VK_F12:
            case VK_F13:
            case VK_F14:
            case VK_F15:
            case VK_F16:
                //used = handleKeyUpOrDown (true); //! fix for keys
                used = handleKeyPress (extendedKeyModifier | (int) key, 0) || used;
                break;

It works for me, is it a proper way to handle that? If somebody checks it in please let me know.

Thank you,

Victor

No, definitely not the right approach.. But you'd want a TextEditor to consume these keys (?)

If you have some kind of custom TextEditor and you want it to ignore the keys, then you'd subclass TextEditor and override its keyPressed method to ignore them and return false.

Jules,

No, opposite, I do not want editor to consume keys, but currently it does. If I return false in keyPressed method of subclassed TextEditor - it's return value is ignored, and my Cubase doesn't get it's F3 key (for example).

Why do we need handleKeyUpOrDown call for F3 and other keys? This call sets "used" variable to true and we have key swallowed..

Thank you,

Victor

 

So override handleKeyUpOrDown as well as keyPressed, and return false from all of these for keys that you definitely want to ignore.

Jules,

handleKeyUpOrDown is method of ComponentPeer, not one of Component or TextEditor, so overriding it is either wrong, or I'm not getting you right. I'll just post you code that doesn't work (F3 and other keystrokes are trapped):

PluginEditor::PluginEditor (Processor& p) : AudioProcessorEditor (&p), processor (p)
{
    static TextEditor te;
    addAndMakeVisible(te);
    te.setBounds(10,10,50,20);

    setSize (400, 300);
}

Everything else is wizard generated, #define JucePlugin_EditorRequiresKeyboardFocus 1. F3 trapped in juce_win32_Windowing.cpp::ComponentPeer::doKeyDown.

The following also won't work, because currently, value returned by keyPressed is ignored:

PluginEditor::PluginEditor (Processor& p) : AudioProcessorEditor (&p), processor (p)
{
    static class CTextEditor : public TextEditor
    {
    public:
        virtual bool CTextEditor::keyPressed(KeyPress const &key)
        { return false; }
    } te;
    addAndMakeVisible(te);
    te.setBounds(10,10,50,20);

    setSize (400, 300);
}

If there's a way to subclass ComponentPeer and use it more or less elegant way I'd like to know it.

Thank you,

Victor

Sorry, I meant Component::keyStateChanged, which is what handleKeyUpOrDown calls.

(That's some pretty dodgy code you've got going on there - adding a static component as a child?? Yikes!)

Jules,

Thought key up and key down are arrow keys ) Works fine now.

Code from above is not the real one, but a mere illustration of what I mean. Obviously no use in the component that's in scope of a constructor only.

Thank you,

Victor

Glad you got it working.

We try to call people out when they post bad code on here - not just to help the poster improve, but because we have a lot of beginners reading the forum, and they might not otherwise be aware when something is a bad example to follow!

Jules,

Next time I'll note that it's not an example to follow, sure there may be someone who will buy what you don't expect at all.

Thank you once again,

Victor