Skip to content

InlineKeyboard

Class

Advanced inline keyboard with built-in pagination and 50+ language selection.

Constructor

InlineKeyboard(
    row_width: int = 3,
    callback_pattern: str = "",
    count_pages: int = 0,
    current_page: int = 0,
    custom_locales: dict[str, str] | None = None,
)
Parameter Type Default Description
row_width int 3 Buttons per row
callback_pattern str "" Pattern for callback data
count_pages int 0 Total pages (pagination)
current_page int 0 Current page number
custom_locales dict None User-defined custom locales

Methods

paginate method

Create pagination keyboard with duplicate prevention and LRU caching.

keyboard.paginate(
    count_pages: int,
    current_page: int,
    callback_pattern: str,
    source: str | None = None,
)
Parameter Type Description
count_pages int Total pages (≥ 1)
current_page int Current page (≥ 1)
callback_pattern str Must contain {number} placeholder
source str \| None Source ID for multi-client isolation

Raises

  • PaginationError — invalid parameters
  • PaginationUnchangedError — identical keyboard already exists for this source
Usage
from pykeyboard import InlineKeyboard, PaginationUnchangedError

kb = InlineKeyboard()
try:
    kb.paginate(10, 3, "page:{number}")
except PaginationUnchangedError:
    pass  # keyboard unchanged

languages method

Create language selection keyboard with validation.

keyboard.languages(
    callback_pattern: str,
    locales: str | list[str],
    row_width: int = 2,
)
Parameter Type Description
callback_pattern str Must contain {locale} placeholder
locales str \| list[str] Locale code(s) to display
row_width int Buttons per row (≥ 1)

Raises

LocaleError — invalid locale parameters

Usage
kb = InlineKeyboard()
kb.languages("lang:{locale}", ["en_US", "es_ES", "fr_FR"])

add method

Add buttons in rows based on row_width.

keyboard.add(*buttons)
Usage
kb = InlineKeyboard(row_width=2)
kb.add(
    InlineButton("A", "a"),
    InlineButton("B", "b"),
    InlineButton("C", "c"),  # wraps to next row
)

row method

Add a single row of buttons (ignores row_width).

keyboard.row(*buttons)

add_custom_locale method

Register a custom locale for language keyboards.

keyboard.add_custom_locale(locale_code: str, display_name: str)
Usage
kb = InlineKeyboard()
kb.add_custom_locale("en_PIRATE", "\U0001F3F4\u200D\u2620\uFE0F Pirate English")
kb.languages("lang:{locale}", ["en_US", "en_PIRATE"])

remove_custom_locale method

Remove a custom locale. Returns True if removed, False if it didn't exist.

keyboard.remove_custom_locale(locale_code: str) -> bool

get_custom_locales method

Get all custom locales defined on this keyboard.

Returns: dict[str, str] — locale codes → display names


get_all_locales method

Get all available locales (built-in + custom).

Returns: dict[str, str] — all locale codes → display names


clear_pagination_hashes classmethod

Clear stored pagination hashes for memory management.

InlineKeyboard.clear_pagination_hashes(source: str | None = None) -> int

Returns: Number of hashes cleared.


get_pagination_hash_stats classmethod

Get statistics about stored pagination hashes.

Returns: dict[str, Any] with hash storage stats.


Properties

pyrogram_markup property

Get the Pyrogram InlineKeyboardMarkup for use with reply_markup=.

Returns: InlineKeyboardMarkup