Skip to content

Keypad

The Keypad component provides a customizable numeric or custom keypad for input fields. It’s useful for applications requiring secure PIN entry, custom character input, or specialized input interfaces. The component supports various configurations including custom keys, key shuffling for security, and different positioning options.

This component requires:

  • Metro UI core
  • LESS variables and mixins
<!-- Basic numeric keypad -->
<input type="text" data-role="keypad">
<!-- Custom keys with separator -->
<input type="text"
data-role="keypad"
data-keys="1,2,3,4,5,6,7,8,9,0,ok"
data-except-keys="ok"
data-key-separator="-">
<!-- Alphabetic keypad -->
<input type="text"
data-role="keypad"
data-keys="q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m">
<!-- Keypad with label -->
<input type="text" data-role="keypad" data-label="Enter PIN:">
<!-- Keypad with shuffled keys for security -->
<input type="text"
data-role="keypad"
data-shuffle="true"
data-shuffle-count="5">
ParameterTypeDefaultDescription
keypadDeferrednumber0Deferred initialization time in milliseconds
labelstring""Label text for the keypad
keySizenumber36Size of each key in pixels
keysstring”1, 2, 3, 4, 5, 6, 7, 8, 9, 0”Keys to display, separated by delimiter
exceptKeysstring""Keys to exclude from input value
keySeparatorstring""Separator between keys in the input value
trimSeparatorbooleanfalseIf true, trims separators from the value
keyDelimiterstring”,“Delimiter for the keys parameter
copyInlineStylesbooleanfalseIf true, copies inline styles from the input to the keypad
targetstringnullTarget element to receive the keypad value
keyLengthnumber0Maximum length of the input value (0 = unlimited)
shufflebooleanfalseIf true, shuffles the keys on initialization
shuffleCountnumber3Number of times to shuffle the keys
serviceButtonsbooleantrueIf true, shows backspace and clear buttons
showValuebooleantrueIf true, shows the value in the input field
openbooleanfalseIf true, the keypad is open by default
useElementSizeForKeysbooleanfalseIf true, uses the element size for the keypad width
openModestring”auto”Mode for opening the keypad (“auto” or “up”)
clsKeypadstring""Additional CSS class for the keypad
clsInputstring""Additional CSS class for the input
clsKeysstring""Additional CSS class for the keys container
clsKeystring""Additional CSS class for each key
clsServiceKeystring""Additional CSS class for service keys
clsBackspacestring""Additional CSS class for the backspace key
clsClearstring""Additional CSS class for the clear key
clsLabelstring""Additional CSS class for the label

Shuffles the keys for security purposes.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.shuffle();

Shuffles the keys a specified number of times.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.shuffleKeys(5); // Shuffle 5 times

Gets or sets the value of the keypad.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
// Get value
var value = keypad.val();
// Set value
keypad.val("1234");

Opens the keypad.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.open();

Closes the keypad.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.close();

Disables the keypad.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.disable();

Enables the keypad.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.enable();

Toggles the enabled/disabled state of the keypad.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.toggleState();

Sets the position of the keypad keys.

var keypad = Metro.getPlugin('#my-keypad', 'keypad');
keypad.setPosition("top-left");
EventDescription
onKeypadCreateTriggered when the keypad is created
onChangeTriggered when the keypad value changes
onClearTriggered when the keypad is cleared
onBackspaceTriggered when the backspace key is pressed
onShuffleTriggered when the keys are shuffled
onKeyTriggered when a key is pressed
<input type="text"
data-role="keypad"
data-on-key="onKeyPressed"
data-on-change="onValueChanged">
function onKeyPressed(key, value, element) {
console.log("Key pressed:", key);
console.log("Current value:", value);
}
function onValueChanged(value) {
console.log("Value changed to:", value);
}
VariableDefault (Light)Dark ModeDescription
--keypad-backgroundvar(—input-background)var(—input-background)Background color of the keypad
--keypad-colorvar(—input-color)var(—input-color)Text color of the keypad
--keypad-border-color#E8E8E8#4A4D51Border color of the keypad
--keypad-border-radius4px4pxBorder radius of the keypad
/* Custom styling for a specific keypad */
#my-keypad {
--keypad-background: #f0f7ff;
--keypad-color: #0d47a1;
--keypad-border-color: #90caf9;
--keypad-border-radius: 8px;
}
  • .keypad - Base class for the keypad component
  • .keys - Container for the keypad keys
  • .key - Individual key
  • .service-key - Service keys (backspace, clear)
  • .keys.right - Positions the keypad to the right of the input
  • .keys.bottom - Positions the keypad below the input
  • .keys.top - Positions the keypad above the input
  • .keys.left - Positions the keypad to the left of the input
  • .keys.bottom-left - Positions the keypad below and to the left of the input
  • .keys.bottom-right - Positions the keypad below and to the right of the input
  • .keys.top-left - Positions the keypad above and to the left of the input
  • .keys.top-right - Positions the keypad above and to the right of the input
  • .open - Applied when the keypad is open
  • .focused - Applied when the input is focused
  • .disabled - Applied when the keypad is disabled
  1. For security-sensitive applications (like PIN entry), use the shuffle feature to prevent pattern recognition
  2. Provide clear labels for keypad inputs to improve accessibility
  3. Consider using custom keys for specialized input requirements
  4. For mobile applications, adjust the key size for better touch interaction
  5. Use the positioning options to ensure the keypad is visible on all screen sizes