Simple-Keyboard: JavaScript Virtual Keyboard

3.8.133 · active · verified Sun Apr 19

Simple-Keyboard is a robust, lightweight, and highly customizable JavaScript virtual keyboard library designed for web applications. Currently at version 3.8.133, it maintains a stable release cadence with frequent updates addressing bugs and adding features. Key differentiators include broad browser compatibility (down to IE11 with specific bundles), built-in TypeScript support since v3.0.0, and a modular architecture allowing extensions for autocorrect, input masks, and key navigation. It is designed to be framework-agnostic, easily integrating into vanilla JavaScript, React, Angular, and Vue projects, providing an on-screen input solution suitable for touchscreens, kiosks, and general web input fields.

Common errors

Warnings

Install

Imports

Quickstart

Initializes simple-keyboard with a custom layout, demonstrating how to handle input changes, key presses, and dynamically switch between 'default', 'shift', and 'numbers' layouts.

import SimpleKeyboard from 'simple-keyboard';
import 'simple-keyboard/build/css/index.css';

// Create a target input element in your HTML: <input class="input" value="" />
// And a container for the keyboard: <div class="simple-keyboard"></div>

const inputElement = document.querySelector('.input') as HTMLInputElement;

const keyboard = new SimpleKeyboard({
  onChange: (input: string) => onChange(input),
  onKeyPress: (button: string) => onKeyPress(button),
  input: inputElement || undefined, // Pass the input element or its selector
  theme: 'hg-theme-default hg-layout-default',
  layout: {
    default: [
      'q w e r t y u i o p',
      'a s d f g h j k l',
      '{shift} z x c v b n m {backspace}',
      '{numbers} {space} {ent}'
    ],
    shift: [
      'Q W E R T Y U I O P',
      'A S D F G H J K L',
      '{shift} Z X C V B N M {backspace}',
      '{numbers} {space} {ent}'
    ],
    numbers: [
      '1 2 3',
      '4 5 6',
      '7 8 9',
      '{abc} 0 .'
    ]
  }
});

function onChange(input: string) {
  if (inputElement) {
    inputElement.value = input;
  }
  console.log('Input changed:', input);
}

function onKeyPress(button: string) {
  console.log('Button pressed:', button);

  if (button === '{shift}' || button === '{lock}') {
    handleShift();
  }
  if (button === '{numbers}' || button === '{abc}') {
    handleLayoutChange(button);
  }
}

function handleShift() {
  const currentLayout = keyboard.options.layoutName;
  const newLayout = currentLayout === 'default' ? 'shift' : 'default';
  keyboard.setOptions({ layoutName: newLayout });
}

function handleLayoutChange(button: string) {
  const newLayout = button === '{numbers}' ? 'numbers' : 'default';
  keyboard.setOptions({ layoutName: newLayout });
}

view raw JSON →