# BUG: On-screen keyboard appears when dragging slider

**URL:** <https://forum.juce.com/t/bug-on-screen-keyboard-appears-when-dragging-slider/51244>\
**Category:** Android\
**Created:** [April 30, 2022, 2:50am UTC](https://forum.juce.com/t/bug-on-screen-keyboard-appears-when-dragging-slider/51244 "2022-04-30T02:50:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![p-i](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/p-i/32/1785_2.png) [@p-i](https://forum.juce.com/u/p-i)\
**Post date:** [April 30, 2022, 2:50am UTC](https://forum.juce.com/t/bug-on-screen-keyboard-appears-when-dragging-slider/51244/1 "2022-04-30T02:50:01Z")

</div>

NOTE: I’m on the `develop` branch of JUCE, pulled yesterday. Last commit:

```auto
commit 31d4f9059d2fe0f51b6dbc11f8530139d82c9b17 (HEAD -> develop)
Author: attila <attila@juce.com>
Date: Wed Apr 27 15:44:45 2022 +0200

    Android: Fix dismissing screen keyboard when interacting with TextEditor

```

I have a slider, which has its associated textbox.  
On macOS, as I drag the slider the value in the textbox changes. Correct behaviour!  
However on Android, dragging the slider pops up the on-screen keyboard.

Here are all the code-references to the slider:

```auto
    Slider sliderPurity;

        sliderPurity.setRange(0, 15, 1);
        sliderPurity.setValue(0, dontSendNotification);
        sliderPurity.setTextBoxIsEditable(false);
        sliderPurity.setTextBoxStyle(Slider::TextBoxRight, true, 30, 30);
        addAndMakeVisible(sliderPurity);

        sliderPurity .setBounds(r.removeFromTop(50)); r.removeFromTop(10);

    float getFracPure() {
        return sliderPurity.getValue() / sliderPurity.getMaximum();
    }

```

One thing I notice in the screenshots:

 ![Screenshot 2022-04-30 at 09.48.05](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/6/67496e1ef96de3a998e9dab807b5456485695a93.jpeg)

 ![c56329aa-e30b-43aa-8250-6075575664f0](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/e/ebdc5da29609d094f15c45285c541308bd599d0e.jpeg)

… the caret is already in a PRIOR textbox. So it’s possible that is influencing the behaviour. A few days ago I reported an issue of the keyboard NOT popping up in response to a textbox gaining focus, and it was patched in `develop` branch.

Could it be that my slider attempts to take focus, is denied (`sliderPurity.setTextBoxIsEditable(false);` maybe?), focus returns to the PRIOR textbox and (thanks to the latest patch) the keyboard appears?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [June 13, 2022, 10:28am UTC](https://forum.juce.com/t/bug-on-screen-keyboard-appears-when-dragging-slider/51244/2 "2022-06-13T10:28:09Z")

</div>

I’m assuming the slider and editor have the same parent component.

I think what happens is this:

- Dragging the slider tries to give it keyboard focus.
- The slider doesn’t want focus, so the parent is given a chance to take focus.
- The parent itself also doesn’t want focus, so it checks each of its children to see whether they want focus.
- The TextEditor _does_ want focus, so focus is passed to that component.

One potential workaround would be to make the TextEditor’s parent component keyboard-focusable. That way, when the click event is propagated up the component hierarchy from the slider, the editor’s parent will take focus, rather than passing it on to the editor.

As an alternative, I’ve pushed a change to TextEditor that will avoid showing the onscreen keyboard unless the editor is clicked directly:

> <https://github.com/juce-framework/JUCE/commit/2fc7d12ae70589b16a0dc6eb3ab4c698192d20e1>
>
> Previously, individual components had to ask the peer to hide and show
> the keybo…ard, by calling textInputRequired() and
> dismissPendingTextInput() respectively. When an onscreen keyboard (OSK)
> was required, most Peer implementation would directly hide/show the OSK
> inside these function. However, the iOS ComponentPeer implementation
> instead listened to the application's global keyboard focus, and only
> opened the OSK when the focused component was also a TextInputTarget
> with active input.
> 
> The iOS scheme seems like a better design, as it enforces that the OSK
> hiding and showing is synced with the keyboard focus of the application.
> In the other implementations, it was possible for a Component to call
> textInputRequired even when it didn't have the keyboard focus, putting
> the application into an inconsistent state. The iOS scheme also makes
> the TextInputTarget interface more useful, as it enforces that the OSK
> will only display for components that implement TextInputTarget, and
> return true from isTextInputActive().
> 
> This patch changes all Peer implementations to match the iOS
> implementation, improving consistency. Each time the global keyboard
> focus changes, refreshTextInputTarget is called automatically, and the
> OSK is shown if the focused component is a TextInputTarget that returns
> true from isTextInputActive, and hidden otherwise. Components can also
> call refreshTextInputTarget manually. This should be done whenever the
> component updates the return value of isTextInputActive(). Effectively,
> the Peer is now responsible for keeping track of the focused
> TextInputTarget, rather than allowing individual components to hide and
> show the OSK at will.
> 
> Additionally, this patch adds an option to the TextEditor to
> automatically dismiss the OSK when the mouse is clicked outside of the
> editor. This should improve user experience on mobile platforms, where
> touches on sibling components may cause a TextEditor to gain keyboard
> focus and unnecessarily display the OSK.

Please try this out and let me know if you encounter any issues that mean that this isn’t an acceptable fix.
