Class SecureKeyboard

java.lang.Object
com.codename1.ui.Component
com.codename1.security.SecureKeyboard
All Implemented Interfaces:
Animation, Editable, StyleListener

public final class SecureKeyboard extends Component

An on-screen keyboard drawn by the application itself, for fields a keylogging keyboard must never see.

On Android the system keyboard is an app the user chose, and it receives every keystroke typed into every other app. A malicious one is a keylogger nothing in the app can switch off: password and TextArea.SENSITIVE fields already ask the keyboard not to learn what is typed, but only a well-behaved keyboard honours the request. A field with a SecureKeyboard installed is never handed to the system keyboard at all. It is made uneditable, so the platform editor cannot open on it, and a tap shows this keyboard instead, which writes into the field directly.

TextField pin = new TextField("", "PIN", 6, TextArea.NUMERIC | TextArea.PASSWORD);
SecureKeyboard kb = SecureKeyboard.install(pin);
kb.setScrambled(true);

A common pattern is to install it only when DeviceIntegrity.isThirdPartyKeyboardActive(String...) reports a user-installed keyboard, so most users keep the keyboard they know.

What it does against the threats a penetration test checks for:

The layout follows the field's constraint: a number pad for TextArea.NUMERIC, TextArea.DECIMAL and TextArea.PHONENUMBER, letters with @ or / for TextArea.EMAILADDR and TextArea.URL, and letters otherwise. It draws its own keys in the style of the platform keyboards, light or dark following the theme's dark mode, under a strip with a lock icon that tells the user this is not their usual keyboard.

It types ASCII only: no accented letters, no other scripts, no dictation, no autofill. That is the price of keeping input away from the platform, and why it suits credentials and codes rather than free text.

  • Method Details

    • install

      public static SecureKeyboard install(TextArea field)

      Routes a field's input through a secure keyboard instead of the system keyboard.

      The field is made uneditable, which is what keeps the platform editor from ever opening on it, and a tap on it shows the returned keyboard. Installing twice returns the keyboard already installed.

      Parameters
      • field: the field to protect
      Returns

      the keyboard now serving the field

    • uninstall

      public static void uninstall(TextArea field)

      Removes a keyboard added by install(TextArea) and restores the field's editability.

      Parameters
      • field: the protected field
    • getInstalled

      public static SecureKeyboard getInstalled(TextArea field)

      The keyboard installed on a field, or null.

      Parameters
      • field: the field to look up
      Returns

      its keyboard, or null when none is installed

    • layoutFor

      public static SecureKeyboard.Layout layoutFor(int constraint)

      Picks the layout a field with this TextArea constraint gets.

      Parameters
      • constraint: a TextArea constraint, flags included
      Returns

      the matching layout

    • getField

      public TextArea getField()

      The field this keyboard types into.

      Returns

      the protected field

    • getLayout

      public SecureKeyboard.Layout getLayout()

      The current layout.

      Returns

      the layout in use

    • setLayout

      public void setLayout(SecureKeyboard.Layout layout)

      Overrides the layout chosen from the field's constraint.

      Parameters
      • layout: the layout to use; null restores the one the constraint picks
    • isScrambled

      public boolean isScrambled()

      Whether the digit keys are shuffled each time the keyboard opens.

      Returns

      true when the digits are scrambled

    • setScrambled

      public void setScrambled(boolean scrambled)

      Shuffles the digit keys every time the keyboard opens, with SecureRandom, so touch coordinates recorded by malware do not reveal which digits were typed. Off by default: it slows typing down, so keep it for PINs and one-time codes.

      Parameters
      • scrambled: true to shuffle the digits
    • setDigitOrder

      public void setDigitOrder(String order)

      Puts the digit keys in a fixed order, for documentation screenshots and tests that need the same picture every time. A fixed order is exactly what scrambling exists to avoid, so do not use this in place of setScrambled(boolean); it also turns scrambling off, since the next show() would otherwise replace the order.

      Parameters
      • order: the ten digits, each exactly once, in the order the keys show them
    • isShowing

      public boolean isShowing()

      Whether the keyboard is on screen.

      Returns

      true while it is showing

    • show

      public void show()
      Shows the keyboard at the bottom of the field's form. Called by a tap on the field; call it yourself to open the keyboard when a screen appears.
    • hide

      public void hide()
      Hides the keyboard and reports the edit as complete, as the platform editor does when it closes: the field's action listeners fire.
    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Component
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • animate

      public boolean animate()
      Description copied from class: Component

      Allows the animation to reduce "repaint" calls when it returns false. It is called once for every frame. Frames are defined by the com.codename1.ui.Display class.

      Returns

      true if a repaint is desired or false if no repaint is necessary

      Specified by:
      animate in interface Animation
      Overrides:
      animate in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component