Numeric Text Field Input (patch included)

Just came across this looking to adapt numeric input fields for the Android port of my app. 

Not quite sure how it would work currently but I think this is important for e.g. sliders and text inputs. 

Somehow need to get an InputType parameter to the onCreateInputConnection function in the Java Activity, is my first guess.

Anyone managed a way round this?


I've been searrching around the JUCE code to try and find a way to get a numerical text input keyboard to appear when editing the value for a Slider. Currently a generic keyboard comes up which is not very user friendly.

So far I see that TextEditor inherits TextInputTarget, which has:

​virtual VirtualKeyboardType getKeyboardType()       { return textKeyboard; }

So I've gone and made a few alterations to the TextEditor, Label and Slider classes, patches below:

Subject: [PATCH] add keyboardType for Android/iOS
 modules/juce_gui_basics/widgets/juce_Label.cpp    | 4 +++-
 modules/juce_gui_basics/widgets/juce_Label.h      | 5 +++++
 modules/juce_gui_basics/widgets/juce_Slider.cpp   | 1 +
 modules/juce_gui_basics/widgets/juce_TextEditor.h | 5 +++++
 4 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/modules/juce_gui_basics/widgets/juce_Label.cpp b/modules/juce_gui_basics/widgets/juce_Label.cpp
index 7ab3d5e..04b8fce 100644
--- a/modules/juce_gui_basics/widgets/juce_Label.cpp
+++ b/modules/juce_gui_basics/widgets/juce_Label.cpp
@@ -32,7 +32,8 @@ Label::Label (const String& name, const String& labelText)
       minimumHorizontalScale (0.7f),
       editSingleClick (false),
       editDoubleClick (false),
-      lossOfFocusDiscardsChanges (false)
+      lossOfFocusDiscardsChanges (false),
+      keyboardType (TextEditor::textKeyboard)
     setColour (TextEditor::textColourId, Colours::black);
     setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
@@ -209,6 +210,7 @@ void Label::showEditor()
         addAndMakeVisible (editor = createEditorComponent());
         editor->setText (getText(), false);
         editor->addListener (this);
+        editor->setKeyboardType (keyboardType);
         if (editor == nullptr) // may be deleted by a callback
diff --git a/modules/juce_gui_basics/widgets/juce_Label.h b/modules/juce_gui_basics/widgets/juce_Label.h
index 825b0a0..58cfdd6 100644
--- a/modules/juce_gui_basics/widgets/juce_Label.h
+++ b/modules/juce_gui_basics/widgets/juce_Label.h
@@ -162,6 +162,9 @@ public:
     /** Specifies the amount that the font can be squashed horizontally. */
     float getMinimumHorizontalScale() const noexcept                    { return minimumHorizontalScale; }
+    /** Set keyboard type for passing down to TextEditor/ */
+    void setKeyboardType (TextEditor::VirtualKeyboardType type)         { keyboardType = type; }
@@ -338,6 +341,8 @@ private:
     bool updateFromTextEditorContents (TextEditor&);
+    TextEditor::VirtualKeyboardType keyboardType;
diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp
index 8c76b90..daaebbe 100644
--- a/modules/juce_gui_basics/widgets/juce_Slider.cpp
+++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp
@@ -565,6 +565,7 @@ public:
             owner.addAndMakeVisible (valueBox = lf.createSliderTextBox (owner));
             valueBox->setWantsKeyboardFocus (false);
+            valueBox->setKeyboardType (TextEditor::numericKeyboard);
             valueBox->setText (previousTextBoxContent, dontSendNotification);
             valueBox->setTooltip (owner.getTooltip());
diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.h b/modules/juce_gui_basics/widgets/juce_TextEditor.h
index 4dd5b21..4dbf85f 100644
--- a/modules/juce_gui_basics/widgets/juce_TextEditor.h
+++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h
@@ -583,6 +583,10 @@ public:
     void setInputRestrictions (int maxTextLength,
                                const String& allowedCharacters = String::empty);
+    VirtualKeyboardType getKeyboardType()               { return keyboardType; }
+    void setKeyboardType (VirtualKeyboardType type)     { keyboardType = type; }
     /** This abstract base class is implemented by LookAndFeel classes to provide
         TextEditor drawing functionality.
@@ -692,6 +696,7 @@ private:
     juce_wchar passwordCharacter;
     OptionalScopedPointer<InputFilter> inputFilter;
     Value textValue;
+    VirtualKeyboardType keyboardType;

This allows me to set the inputMethodType to 'number' and I can see it by doing Log calles in the Java activity. However, as I have learned today, the line 

​imm.setInputMethod (getWindowToken(), type);

has no effect as Android does not allow setting the keyboard type programmatically. Searching around I see some suggestions to use showInputMethodPicker() however this is more for using preset language keyboards AFAICS. 

So... the next step I plan to take is to use a totally custom keyboard in Java, following examples/suggestions such as and


1 Like

So ... I now have custom soft keyboards working, both numeric and decimal types.  Jules has kindly adjusted and merged my patches to get this working on the JUCE side, including some minor changes to iOS to bring up numeric or decimal keyboards. I will post up an example project soon with my keyboard Fragment and modified Java Activity.