# Key modifiers in complex components

**URL:** <https://forum.juce.com/t/key-modifiers-in-complex-components/2042>\
**Category:** General JUCE discussion\
**Created:** [August 23, 2007, 7:32pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042 "2007-08-23T19:32:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [August 23, 2007, 7:32pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/1 "2007-08-23T19:32:47Z")

</div>

Hi Jules!

I want to be able to apply my own modifier keys (e.g. for Cmd-V etc.) when the keyboard focus is on a slider control. This is fine when the keyboard focus is owned by a button sub-component of the slider.

However, when the focus is owned by the slider’s underlying text editor component, I can’t seem to prevent the text editor that is owned _by_ the slider grabbing the Cmd-V key, and using it for its own end (e.g. to paste current text). That’s a shame, as I really need instead to perform a higher-level action in reponse to Cmd-V…!

Can I please 🙂 ask if there is some way that for sliders, there could be a method I could call that would pass-on a request to the underlying text editor to ignore command modifiers?

Or perhaps a hook I could apply to components that allow patching-in of key handling, where I can do something like this (that I’m already doing with my text editor components)…? That would be very powerful.

```auto
  virtual bool keyPressed (const KeyPress &key)
  {
    if (key.getModifiers().isCommandDown())
    {
      return false;
    }

    return TextEditor::keyPressed(key);
  }
```

Thanks in advance,

Pete

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 24, 2007, 8:52am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/2 "2007-08-24T08:52:39Z")

</div>

But surely the text editor only exists while the value is actually being edited? In that case, wouldn’t you want the editor to get the cmd-v?

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [August 24, 2007, 2:30pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/3 "2007-08-24T14:30:52Z")

</div>

Hi Jules,

Actually no, I want a higher-level command to apply. In this case, the component is just the child of a row. The semantics of the UI require that a paste operation via “cmd-V” to be interpreted as a paste to the entire row; hence the app needs to ensure that the text component ignores the command in this case.

Another example I’ve just come up against is in the context of a grid (e.g. TableListBox), where I want to intercept the up/down keys to move the focus up/down from grid cell to grid cell. However, when I’m in the TextEditor sub-component of a ComboBox, or the TextEditor sub-component of a Slider, I can’t find a way to grab the key events and use them for my own purposes!!

Seems like the general (and simple) solution to this sort of key handling might be to allow an application to:

- ask ComboBox or Slider for child TextEditor (c.f. ListBox components can be asked its vertical Slider)
- allow the application to register a key event listener/observer/“hook” with any component (including the TextEditor I’d have got from the above call), that allows the app to intercept events as necessary _before_ the component handles them. In this case the listener can return a status code to indicate that the event has been handled.

With that in place, I can then get my apps keyboard handling nice and sorted! 🙂

One other associated request would be if I could have a method on a ComboBox that I can call to determine if the Drop-down list is displayed. I would ignore up/down key even overriding in that case. I would also like a method that could force the ComboBox pop-up to appear (so I could have that happen e.g. if user pressed Enter key when on the box).

Hoping this makes sense, and all the best!

Pete

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 24, 2007, 3:21pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/4 "2007-08-24T15:21:38Z")

</div>

Have you thought about using an editable Label instead of a TextEditor? That’d only intercept keys when it’s actually in the modal state of being edited. It’s more lightweight, too.

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [August 24, 2007, 4:46pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/5 "2007-08-24T16:46:17Z")

</div>

Hi Jules, no I hadn’t - many thanks for the tip, sounds like a good thing for me to do!

Have you had any thoughts on the subject of key event “hooks” for components?

Pete

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 24, 2007, 4:52pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/6 "2007-08-24T16:52:40Z")

</div>

Hmm… key event hooks - nice idea but would it be worth the extra bloat in the Component base class for such a rarely-needed feature?

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [August 25, 2007, 6:24am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/7 "2007-08-25T06:24:24Z")

</div>

Hi Jules,

The problem is that without the ability to tell components to do something different with navigation key events (and other key events), my application will have poor keyboard navigation support. I have a user base which is pretty fussy about keyboard support working “just right”, so if you aren’t able to do it in the core system, I’ll have to modify my copy of the Juce library to do this \<shudder!\>. :shock: 🙂

I don’t see there being much bloat, to be honest:

- new hook class ComponentKeyHook (or some such) with one pure method:  
bool KeyEventHook  
… returns true if the hook handles the event, otherwise false
- new methods on component allowing a hook to be registered/cleared  
(just like your common listener handling):  
addKeyEventHook  
removeKeyEventHook  
… and a new member variable or two and some new code in the  
key event handler

New method in both Slider and ComboBox, allowing the app to grab the TextEditor sub-component so it can add a hook:  
TextEditor \*getTextEditor (void)

Anyways, hoping that feedback helps. I know you’ve always got too much in your in-tray … don’t we all 🙂 … so hopefully you’ll consider it for some future development. I’m sure I won’t be the only Juce user who’d find the need to do this…?

All the best!

Pete

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 25, 2007, 12:50pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/8 "2007-08-25T12:50:27Z")

</div>

How about, instead of all that, a change to the KeyListeners - at the moment, the component’s keyPressed method is called before the listeners are told, but if I reversed it to give the listeners the first go at the event, then they could override the components own method.

That’d do what you need, but would it bugger up other people’s code? Probably not…

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [August 25, 2007, 2:03pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/9 "2007-08-25T14:03:56Z")

</div>

Hi Jules,

I see where you are coming from … 🙂 … but I can’t see that without adding:  
getTextEditor() … to ComboBox and Slider  
… and in turn adding:  
setKeyListener/removeKeyListener() on Component…

… would the app be able to override key events on the internal TextEditor component within the ComboBox and Slider…?

…? Of course, you totally understand the framework, and I’m just learning my way around it!! 🙂

Pete

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 26, 2007, 8:28am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/10 "2007-08-26T08:28:11Z")

</div>

The combobox and slider both use Labels, not TextEditors, so there is no texteditor except when the user’s actually typing in them.

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [August 26, 2007, 8:05pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/11 "2007-08-26T20:05:40Z")

</div>

Hi Jules,

Got you! So… I wonder how best to get keyboard hooks into the ComboBox and Slider …?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 28, 2007, 9:05am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/12 "2007-08-28T09:05:59Z")

</div>

Could probably be done by changing the keylistener stuff like I mentioned earlier. Let me have a think…

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [August 29, 2007, 10:21am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/13 "2007-08-29T10:21:50Z")

</div>

Hi Jules, fabbo, many thanks. 🙂

Pete

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [November 2, 2007, 10:05am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/14 "2007-11-02T10:05:47Z")

</div>

Hi Jules, have you had that think yet? 🙂

BTW, intermorphic’s noatikl and liptikl are now due for release in the next couple of weeks; due in no small part to the power of Juce. Much kudos to you, Jules!

Pete

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 2, 2007, 11:45am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/15 "2007-11-02T11:45:45Z")

</div>

I think I changed around the keylistener order ages ago, probably about the time of this thread…

Thought you’d been a bit quiet lately - glad to hear you’ve been busy!

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [November 19, 2007, 10:44am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/16 "2007-11-19T10:44:21Z")

</div>

Hi Jules!

Just returning to this as we prepare noatikl for sale in next week or so. 🙂

I got your change to the key listener handling, but the problem is this:

- I have no way to get guaranteed access to the underlying TextEditor that is created dynamically by the Label owned by the Slider, to attach a key listener, as the TextEditor appears and disappears dynamically.
- when the Slider is attached to the ui, the Label is created immediately (and I can of course search for this Label by enumeration of the Slider child components of course, so could attach a key listener to this… but that is of no use of course as it is the TextEditor that handles the keyboard interaction…)
- when the Slider gets focus, the Label owned by the Slider creates a TextEditor to handle the editing; this TextEditor is created dynamically, internally to the Label, so I have no way to attach a key listener to it; so I cannot override the TextEditor key handling. 🙂
- the solution to this might be to have something like “editorCreated” and “editorDeleted” listeners on the label class, that would allow my app to see when the editor owned by the label appeared/disappeared, giving me a chance to attach/detach key listeners to that TextEditor as it appears/disappears?

Hoping this makes sense. 🙂

Pete

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 19, 2007, 2:16pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/17 "2007-11-19T14:16:12Z")

</div>

Hi Pete

How about if I add a LookAndFeel method to create the editor component? Then you could override that so that when one gets created, you can attach your listener to it…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 19, 2007, 2:17pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/18 "2007-11-19T14:17:49Z")

</div>

ah - actually you can already do this: if you override LookAndFeel::createSliderTextBox, and make it return a custom Label where you override Label::createEditorComponent(), you can attach the listener there. A bit fiddly, but it should work.

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [November 19, 2007, 6:16pm UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/19 "2007-11-19T18:16:20Z")

</div>

Brilliant idea Jules - and great foresight on your part. 🙂

I’ll let you know how I get on…!

Pete

---

<div class="post-metadata">

**Author:** ![peteatjuce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peteatjuce/32/2347_2.png) [@peteatjuce](https://forum.juce.com/u/peteatjuce)\
**Post date:** [November 20, 2007, 11:23am UTC](https://forum.juce.com/t/key-modifiers-in-complex-components/2042/20 "2007-11-20T11:23:06Z")

</div>

Hi Jules,

Just to confirm that worked, once I’d updated my copy of juce\_ComponentPeer.cpp … for the implementation of ComponentPeer::handleKeyPress()… to match the svn tip.

Once again: many thanks for that. 🙂

The next (directly related!) question is: how can I intercept the key events for combo boxes. 🙂

The reason is the same as before: I have them embedded in a table, and when the user presses up/down key when they have the focus and the pop-up list of values isn’t visible, I want to grab those events to allow me to change focus within the table up/down.

All the best!

Pete

[Next page](https://forum.juce.com/t/key-modifiers-in-complex-components/2042.md?page=2)
