Capacitor Keyboard

cap-go/capgo-skills/skills/capacitor-keyboard

作者 cap-go2cfb277a1488无许可证72 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

Guide to handling keyboard in Capacitor apps including visibility detection, accessory bar, scroll behavior, and input focus. Use this skill when users have keyboard-related issues.

AI 生成的概览

指导处理 Capacitor 移动应用中的软键盘行为,涵盖事件、尺寸调整、滚动与焦点。

功能
该技能提供在 Capacitor iOS 和 Android 应用中管理软键盘的说明与代码示例。内容包括安装 @capacitor/keyboard 插件、监听键盘显示与隐藏事件、读取键盘高度、配置 resize 模式与样式、将聚焦输入框滚动到可视区域,以及切换 iOS 辅助工具栏。此外还列出表单最佳实践和常见键盘问题的排查表。
适用场景
当 Capacitor 应用出现键盘相关问题时使用,例如内容被键盘遮挡、滚动或焦点异常,或需要显示、隐藏、监听键盘事件。配置键盘 resize 行为或 iOS 辅助工具栏时也适用。
运行要求
需要一个 Capacitor 项目,并通过 npm 安装 @capacitor/keyboard 插件,再用 npx cap sync 同步。仅为说明文档,不附带脚本。

Keyboard Handling in Capacitor

Manage keyboard behavior in iOS and Android apps.

When to Use This Skill

  • User has keyboard issues
  • User needs keyboard events
  • User wants to hide keyboard
  • User has scroll issues with keyboard
  • User wants keyboard accessory bar

Quick Start

bash
npm install @capacitor/keyboardnpx cap sync

Basic Usage

typescript
import { Keyboard } from '@capacitor/keyboard';
// Show keyboardawait Keyboard.show();
// Hide keyboardawait Keyboard.hide();
// Listen for keyboard eventsKeyboard.addListener('keyboardWillShow', (info) => {  console.log('Keyboard height:', info.keyboardHeight);});
Keyboard.addListener('keyboardWillHide', () => {  console.log('Keyboard hiding');});

Configuration

typescript
// capacitor.config.tsplugins: {  Keyboard: {    resize: 'body',        // 'body' | 'ionic' | 'native' | 'none'    style: 'dark',         // 'dark' | 'light' | 'default'    resizeOnFullScreen: true,  },},

Resize Modes

ModeDescription
bodyResize body element
ionicUse Ionic's keyboard handling
nativeNative WebView resize
noneNo automatic resize

Handle Keyboard Height

typescript
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');  });}
css
.chat-input {  position: fixed;  bottom: calc(var(--keyboard-height, 0px) + env(safe-area-inset-bottom));  left: 0;  right: 0;}

Scroll to Input

typescript
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',    });  }});

iOS Accessory Bar

typescript
// Show/hide the toolbar above keyboardawait Keyboard.setAccessoryBarVisible({ isVisible: true });

Form Best Practices

typescript
// Prevent zoom on iOS (use font-size >= 16px)input {  font-size: 16px;}
// Handle form submissionform.addEventListener('submit', async (e) => {  e.preventDefault();  await Keyboard.hide();  // Process form});
// Move to next fieldinput.addEventListener('keypress', (e) => {  if (e.key === 'Enter') {    const nextInput = getNextInput();    if (nextInput) {      nextInput.focus();    } else {      Keyboard.hide();    }  }});

Troubleshooting

IssueSolution
Content hiddenUse resize mode
Slow animationUse keyboardWillShow
iOS zoomUse 16px font-size
Android overlapSet windowSoftInputMode

Resources

来源与署名

来源:cap-go/capgo-skills位于skills/capacitor-keyboard提交2cfb277

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架