simple-keyboard

repository·master·Indexed 25 days ago

https://github.com/hodgef/simple-keyboard

A JavaScript virtual keyboard library providing a customizable UI for virtual text input. It is compatible with web frameworks such as React, Angular, and Vue. The library supports internal input state management, custom layouts, and extensibility through modules like Autocorrect, Input Mask, Key Navigation, and Swipe Keyboard. It includes features for caret positioning, multi-instance synchronization, and flexible styling via button themes and attributes.

Tokens
3.1K
Snippets
0
Records
20
Agent score
79%

What's inside simple-keyboard

  1. Extend simple-keyboard with modules

    master

    The core functionality of simple-keyboard can be extended using specialized modules. Available modules include:

    • Autocorrect: Provides text correction capabilities.
    • Input Mask: Formats input as the user types.
    • Key Navigation: Enables navigating between keys.
    • Swipe Keyboard: Adds swipe gestures for input.

    You can also create your own custom modules by following the instructions on the Modules page.

  2. Use modern browser bundles for better performance

    master
    By default, simple-keyboard supports older browsers like Internet Explorer 11. If you do not need to support legacy browsers, you can use the Modern Browsers bundle (index.modern.js) located in the build directory to reduce bundle size and improve performance.
  3. Manage caret positioning and input synchronization

    master

    SimpleKeyboard automatically tracks the caret (cursor) position in input and textarea elements.

    • disableCaretPositioning: If set to true in options, the keyboard will not attempt to track or update the caret position.
    • syncInstanceInputs: When enabled, the keyboard will detect input events from any instance that has the data-skInstance attribute, allowing multiple keyboards to potentially interact with the same input focus logic.
    • updateCaretOnSelectionChange: If enabled, the keyboard listens to the selectionchange event to keep the caret position in sync with user selections (Note: Firefox may have limitations with this event).
  4. Initialize SimpleKeyboard

    master

    You can create a new instance of SimpleKeyboard by passing a selector (string), a DOM element, or an options object to the constructor.

    If you pass a string, it is treated as the CSS class of the container element. If you pass an HTMLDivElement, it must have a class name. If you pass an object, it is treated as the KeyboardOptions object.

    Note: The keyboard maintains an internal, non-persistent input state to decouple it from the DOM. You can manage multiple inputs by specifying an inputName in the options.

  5. Configure button themes and attributes

    master

    You can customize the appearance and HTML attributes of buttons using the buttonTheme and buttonAttributes options.

    • buttonTheme: An array of objects used to apply specific CSS classes to certain buttons. Each object must contain a class string (the classes to apply) and a buttons string (a space-separated list of button names that should receive these classes).
    • buttonAttributes: An array of objects used to add custom HTML attributes to buttons. Each object must contain an attribute string, a value string, and optionally a buttons string (space-separated list of button names). If buttons is omitted, the attribute is applied to all buttons.
  6. Configure Physical Keyboard Highlighting

    master

    To synchronize the visual state of the virtual keyboard with a physical keyboard, use these options:

    • physicalKeyboardHighlight: Enables highlighting of keys pressed on a physical keyboard.
    • physicalKeyboardHighlightPress: If true, calls keyboard.handleButtonClicked(buttonName) for the highlighted button.
    • physicalKeyboardHighlightPressUseClick: If true, triggers a native button.click() on the highlighted button.
    • physicalKeyboardHighlightPressUsePointerEvents: Determines if physicalKeyboardHighlightPress should use pointer events.
    • physicalKeyboardHighlightTextColor: Sets the text color for the highlighted key.
    • physicalKeyboardHighlightBgColor: Sets the background color for the highlighted key.
    • physicalKeyboardHighlightPreventDefault: If true, uses preventDefault to disable default browser actions during highlighting.
  7. Configure SimpleKeyboard options

    master

    The KeyboardOptions object allows you to customize the behavior and appearance of the keyboard. Key options include:

    OptionTypeDescription
    layoutNamestringSpecifies which layout to use (default: "default").
    themestringCSS class for the keyboard wrapper.
    inputNamestringIdentifier for the internal input (default: "default").
    onKeyPressfunctionCallback triggered on key press. Receives (button: string, event: KeyboardHandlerEvent).
    onChangefunctionCallback triggered on input change. Receives (input: string, event: KeyboardHandlerEvent).
    onInitfunctionCallback triggered once on first render.
    onRenderfunctionCallback triggered every time the keyboard renders.
    maxLengthnumber | objectRestrains input length. Can be a number or an object mapping input names to lengths.
    inputPatternRegExp | objectRestrains input to a specific regular expression.
    syncInstanceInputsbooleanIf true, synchronizes the internal input of all SimpleKeyboard instances.
    enableLayoutCandidatesbooleanEnables support for an input method editor candidate list.
    debugbooleanLogs key presses and input changes to the console.
  8. Configure Layout Candidates (IME support)

    master

    Simple-keyboard supports Input Method Editor (IME) candidate lists via the following options:

    • enableLayoutCandidates: Enables candidate list support.
    • layoutCandidates: An object { [key: string]: string } defining character suggestions for specific key presses.
    • layoutCandidatesPageSize: Determines the size of the candidate list.
    • layoutCandidatesCaseSensitiveMatch: Determines if matches are case-sensitive.
    • disableCandidateNormalization: If true, disables automatic normalization for selected candidates.
    • enableLayoutCandidatesKeyPress: Enables onKeyPress triggering for candidate items.