@capacitor/keyboard
The Keyboard API provides keyboard display and visibility control, along with event tracking when the keyboard shows and hides.
Install
npm install @capacitor/keyboard
npx cap sync
Example
import { Keyboard } from '@capacitor/keyboard';
Keyboard.addListener('keyboardWillShow', info => {
console.log('keyboard will show with height:', info.keyboardHeight);
});
Keyboard.addListener('keyboardDidShow', info => {
console.log('keyboard did show with height:', info.keyboardHeight);
});
Keyboard.addListener('keyboardWillHide', () => {
console.log('keyboard will hide');
});
Keyboard.addListener('keyboardDidHide', () => {
console.log('keyboard did hide');
});
Configuration
On iOS, the keyboard can be configured with the following options:
Prop | Type | Description | Default | Since |
---|---|---|---|---|
resize | KeyboardResize | Configure the way the app is resized when the Keyboard appears. Only available on iOS. | native | 1.0.0 |
style | KeyboardStyle | Override the keyboard style if your app doesn't support dark/light theme changes. If not set, the keyboard style will depend on the device appearance. Only available on iOS. | 1.0.0 | |
resizeOnFullScreen | boolean | There is an Android bug that prevents the keyboard from resizing the WebView when the app is in full screen (i.e. if StatusBar plugin is used to overlay the status bar). This setting, if set to true, add a workaround that resizes the WebView even when the app is in full screen. Only available for Android | 1.1.0 |
Examples
In capacitor.config.json
:
{
"plugins": {
"Keyboard": {
"resize": "body",
"style": "DARK",
"resizeOnFullScreen": true
}
}
}
In capacitor.config.ts
:
/// <reference types="@capacitor/keyboard" />
import { CapacitorConfig } from '@capacitor/cli';
import { KeyboardResize, KeyboardStyle } from '@capacitor/keyboard';
const config: CapacitorConfig = {
plugins: {
Keyboard: {
resize: KeyboardResize.Body,
style: KeyboardStyle.Dark,
resizeOnFullScreen: true,
},
},
};
export default config;
Compatibility with cordova-plugin-ionic-keyboard
To maintain compatibility with
cordova-plugin-ionic-keyboard
,
the following events also work with window.addEventListener
:
keyboardWillShow
keyboardDidShow
keyboardWillHide
keyboardDidHide
API
show()
show() => Promise<void>
Show the keyboard. This method is alpha and may have issues.
This method is only supported on Android.
Since: 1.0.0
hide()
hide() => Promise<void>
Hide the keyboard.
Since: 1.0.0
setAccessoryBarVisible(...)
setAccessoryBarVisible(options: { isVisible: boolean; }) => Promise<void>
Set whether the accessory bar should be visible on the keyboard. We recommend disabling the accessory bar for short forms (login, signup, etc.) to provide a cleaner UI.
This method is only supported on iPhone devices.
Param | Type |
---|---|
options | { isVisible: boolean; } |
Since: 1.0.0
setScroll(...)
setScroll(options: { isDisabled: boolean; }) => Promise<void>
Programmatically enable or disable the WebView scroll.
This method is only supported on iOS.
Param | Type |
---|---|
options | { isDisabled: boolean; } |
Since: 1.0.0
setStyle(...)
setStyle(options: KeyboardStyleOptions) => Promise<void>
Programmatically set the keyboard style.
This method is only supported on iOS.
Param | Type |
---|---|
options | KeyboardStyleOptions |
Since: 1.0.0
setResizeMode(...)
setResizeMode(options: KeyboardResizeOptions) => Promise<void>
Programmatically set the resize mode.
This method is only supported on iOS.
Param | Type |
---|---|
options | KeyboardResizeOptions |
Since: 1.0.0
getResizeMode()
getResizeMode() => Promise<KeyboardResizeOptions>
Get the currently set resize mode.
This method is only supported on iOS.
Returns: Promise<KeyboardResizeOptions>
Since: 4.0.0
addListener('keyboardWillShow', ...)
addListener(eventName: 'keyboardWillShow', listenerFunc: (info: KeyboardInfo) => void) => Promise<PluginListenerHandle>
Listen for when the keyboard is about to be shown.
On Android keyboardWillShow and keyboardDidShow fire almost at the same time.
Param | Type |
---|---|
eventName | 'keyboardWillShow' |
listenerFunc | (info: KeyboardInfo) => void |
Returns: Promise<PluginListenerHandle>
Since: 1.0.0
addListener('keyboardDidShow', ...)
addListener(eventName: 'keyboardDidShow', listenerFunc: (info: KeyboardInfo) => void) => Promise<PluginListenerHandle>
Listen for when the keyboard is shown.
On Android keyboardWillShow and keyboardDidShow fire almost at the same time.
Param | Type |
---|---|
eventName | 'keyboardDidShow' |
listenerFunc | (info: KeyboardInfo) => void |
Returns: Promise<PluginListenerHandle>
Since: 1.0.0
addListener('keyboardWillHide', ...)
addListener(eventName: 'keyboardWillHide', listenerFunc: () => void) => Promise<PluginListenerHandle>
Listen for when the keyboard is about to be hidden.
On Android keyboardWillHide and keyboardDidHide fire almost at the same time.
Param | Type |
---|---|
eventName | 'keyboardWillHide' |
listenerFunc | () => void |
Returns: Promise<PluginListenerHandle>
Since: 1.0.0
addListener('keyboardDidHide', ...)
addListener(eventName: 'keyboardDidHide', listenerFunc: () => void) => Promise<PluginListenerHandle>