Capacitor Keyboard

Cap-go/capgo-skills/plugins/capacitor-features/skills/capacitor-keyboard

作者 Cap-go2cfb277a148832bdb1a835b01e4de323687b498b無授權條款72 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫今天更新

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位於plugins/capacitor-features/skills/capacitor-keyboard提交2cfb277

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架