capacitor-keyboard by cap-go/capgo-skills
npx skills add https://github.com/cap-go/capgo-skills --skill capacitor-keyboard管理 iOS 和 Android 应用中的键盘行为。
bun add @capacitor/keyboard
bunx cap sync
import { Keyboard } from '@capacitor/keyboard';
// 显示键盘
await Keyboard.show();
// 隐藏键盘
await Keyboard.hide();
// 监听键盘事件
Keyboard.addListener('keyboardWillShow', (info) => {
console.log('键盘高度:', info.keyboardHeight);
});
Keyboard.addListener('keyboardWillHide', () => {
console.log('键盘正在隐藏');
});
// capacitor.config.ts
plugins: {
Keyboard: {
resize: 'body', // 'body' | 'ionic' | 'native' | 'none'
style: 'dark', // 'dark' | 'light' | 'default'
resizeOnFullScreen: true,
},
},
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 模式 | 描述 |
|---|
body | 调整 body 元素大小 |
ionic | 使用 Ionic 的键盘处理 |
native | 原生 WebView 调整大小 |
none | 无自动调整大小 |
import { Keyboard } from '@capacitor/keyboard';
import { Capacitor } from '@capacitor/core';
if (Capacitor.isNativePlatform()) {
Keyboard.addListener('keyboardWillShow', (info) => {
document.body.style.setProperty(
'--keyboard-height',
`${info.keyboardHeight}px`
);
});
Keyboard.addListener('keyboardWillHide', () => {
document.body.style.setProperty('--keyboard-height', '0px');
});
}
.chat-input {
position: fixed;
bottom: calc(var(--keyboard-height, 0px) + env(safe-area-inset-bottom));
left: 0;
right: 0;
}
Keyboard.addListener('keyboardWillShow', async (info) => {
const activeElement = document.activeElement as HTMLElement;
if (activeElement) {
// 等待键盘动画
await new Promise((r) => setTimeout(r, 100));
activeElement.scrollIntoView({
behavior: 'smooth',
block: 'center',
});
}
});
// 显示/隐藏键盘上方的工具栏
await Keyboard.setAccessoryBarVisible({ isVisible: true });
// 防止 iOS 上缩放(使用 font-size >= 16px)
input {
font-size: 16px;
}
// 处理表单提交
form.addEventListener('submit', async (e) => {
e.preventDefault();
await Keyboard.hide();
// 处理表单
});
// 移动到下一个输入框
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const nextInput = getNextInput();
if (nextInput) {
nextInput.focus();
} else {
Keyboard.hide();
}
}
});
| 问题 | 解决方案 |
|---|---|
| 内容被隐藏 | 使用调整大小模式 |
| 动画缓慢 | 使用 keyboardWillShow |
| iOS 缩放 | 使用 16px 字体大小 |
| Android 重叠 | 设置 windowSoftInputMode |
每周安装量
70
代码仓库
GitHub 星标数
18
首次出现
2026年2月6日
安全审计
安装于
gemini-cli69
opencode67
github-copilot65
codex65
kimi-cli63
amp63
Manage keyboard behavior in iOS and Android apps.
bun add @capacitor/keyboard
bunx cap sync
import { Keyboard } from '@capacitor/keyboard';
// Show keyboard
await Keyboard.show();
// Hide keyboard
await Keyboard.hide();
// Listen for keyboard events
Keyboard.addListener('keyboardWillShow', (info) => {
console.log('Keyboard height:', info.keyboardHeight);
});
Keyboard.addListener('keyboardWillHide', () => {
console.log('Keyboard hiding');
});
// capacitor.config.ts
plugins: {
Keyboard: {
resize: 'body', // 'body' | 'ionic' | 'native' | 'none'
style: 'dark', // 'dark' | 'light' | 'default'
resizeOnFullScreen: true,
},
},
| Mode | Description |
|---|---|
body | Resize body element |
ionic | Use Ionic's keyboard handling |
native | Native WebView resize |
none | No automatic resize |
import { Keyboard } from '@capacitor/keyboard';
import { Capacitor } from '@capacitor/core';
if (Capacitor.isNativePlatform()) {
Keyboard.addListener('keyboardWillShow', (info) => {
document.body.style.setProperty(
'--keyboard-height',
`${info.keyboardHeight}px`
);
});
Keyboard.addListener('keyboardWillHide', () => {
document.body.style.setProperty('--keyboard-height', '0px');
});
}
.chat-input {
position: fixed;
bottom: calc(var(--keyboard-height, 0px) + env(safe-area-inset-bottom));
left: 0;
right: 0;
}
Keyboard.addListener('keyboardWillShow', async (info) => {
const activeElement = document.activeElement as HTMLElement;
if (activeElement) {
// Wait for keyboard animation
await new Promise((r) => setTimeout(r, 100));
activeElement.scrollIntoView({
behavior: 'smooth',
block: 'center',
});
}
});
// Show/hide the toolbar above keyboard
await Keyboard.setAccessoryBarVisible({ isVisible: true });
// Prevent zoom on iOS (use font-size >= 16px)
input {
font-size: 16px;
}
// Handle form submission
form.addEventListener('submit', async (e) => {
e.preventDefault();
await Keyboard.hide();
// Process form
});
// Move to next field
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const nextInput = getNextInput();
if (nextInput) {
nextInput.focus();
} else {
Keyboard.hide();
}
}
});
| Issue | Solution |
|---|---|
| Content hidden | Use resize mode |
| Slow animation | Use keyboardWillShow |
| iOS zoom | Use 16px font-size |
| Android overlap | Set windowSoftInputMode |
Weekly Installs
70
Repository
GitHub Stars
18
First Seen
Feb 6, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli69
opencode67
github-copilot65
codex65
kimi-cli63
amp63
网页可访问性(A11y)开发指南:实现WCAG合规、键盘导航与屏幕阅读器支持
12,600 周安装
Web无障碍开发指南:WCAG 2.1标准、ARIA模式与键盘导航最佳实践
168 周安装
WordPress插件开发指南:架构、安全、生命周期与Settings API详解
158 周安装
产品分析指南:北极星指标、漏斗分析、事件追踪与关键指标框架
155 周安装
设计思维完整指南:IDEO/斯坦福五阶段方法论、工具模板与实践案例
157 周安装
error-detective 错误排查专家技能:日志分析、堆栈跟踪、分布式系统错误关联与模式识别
160 周安装
Google Workspace API 自动化脚本 - 集成 Drive、Gmail、Calendar 和 Docs
156 周安装