# pygame-vkeyboard
[![Python package](https://github.com/Faylixe/pygame_vkeyboard/workflows/Python%20package/badge.svg?branch=master)](https://github.com/Faylixe/pygame_vkeyboard/actions) [![PyPI version](https://badge.fury.io/py/pygame-vkeyboard.svg)](https://badge.fury.io/py/pygame-vkeyboard) [![PyPI downloads](https://img.shields.io/pypi/dm/pygame-vkeyboard?color=purple)](https://pypi.org/project/pygame-vkeyboard)
Visual keyboard for Pygame engine. Aims to be easy to use as highly customizable as well.
<div align="center">
<table>
<tr>
<td><img src="https://raw.githubusercontent.com/Faylixe/pygame_vkeyboard/master/screenshot/vkeyboard_azerty.png">
</td>
<td><img src="https://github.com/Faylixe/pygame-vkeyboard/blob/master/screenshot/vkeyboard_numeric.gif?raw=true">
</td>
<td><img src="https://github.com/Faylixe/pygame-vkeyboard/blob/master/screenshot/vkeyboard_textinput.gif?raw=true">
</td>
</tr>
</table>
</div>
## Install
```bash
pip install pygame-vkeyboard
```
## Basic usage
``VKeyboard`` only require a pygame surface to be displayed on and a text consumer function, as in the following example :
```python
from pygame_vkeyboard import *
# Initializes your window object or surface your want
# vkeyboard to be displayed on top of.
surface = ...
def consumer(text):
print('Current text : %s' % text)
# Initializes and activates vkeyboard
layout = VKeyboardLayout(VKeyboardLayout.AZERTY)
keyboard = VKeyboard(surface, consumer, layout)
```
The keyboard has the following optional parameters:
- **show_text**: display a text bar with the current text
- **renderer** : define a custom renderer (see chapter below)
- **special_char_layout**: define a custom layout for special characters
- **joystick_navigation**: enable navigation using a joystick
## Event management
A ``VKeyboard`` object handles the following pygame event :
- **MOUSEBUTTONDOWN**
- **MOUSEBUTTONUP**
- **FINGERDOWN**
- **FINGERUP**
- **KEYDOWN**
- **KEYUP**
- **JOYHATMOTION**
- **JOYBUTTONDOWN**
- **JOYBUTTONUP**
In order to process those events, keyboard instance event handling method should be called like in the following example:
```python
while True:
events = pygame.event.get()
# Update internal variables
keyboard.update(events)
# Draw the keyboard
keyboard.draw(surface)
#
# Perform other tasks here
#
# Update the display
pygame.display.flip()
```
It will update key state accordingly as the keyboard buffer as well.
The buffer modification will be notified through the keyboard text consumer function.
The global performances can be improved avoiding to flip the entire display
at each loop by using the ``pygame.display.update()`` function.
```python
while True:
# Draw the keyboard
rects = keyboard.draw(surface)
# Update only the dirty rectangles of the display
pygame.display.update(rects)
```
**Note:** the ``surface`` parameter of the ``draw()`` method is optional, it is used to clear/hide the keyboard when it is necessary and may be mandatory if the surface has changed.
## Customize layout
The keyboard layout is the model that indicates keys are displayed and how they are dispatched
across the keyboard space. It consists in a ``VKeyboardLayout`` object which is built using list of string,
each string corresponding to a keyboard key row. ``VkeyboardLayout`` constructor signature is defined as following :
```python
def __init__(self, model, key_size=None, padding=5, height_ratio=None, allow_uppercase=True, allow_special_chars=True, allow_space=True)
```
If the **key_size** or **height_ratio** parameters are not provided, they will be computed dynamically regarding of
the target surface the keyboard will be rendered into (**height_ratio** is 50% by default).
In order to only display a numerical ``Vkeyboard`` for example, you can use a custom layout like this :
```python
model = ['123', '456', '789', '0']
layout = VKeyboardLayout(model)
```
## Custom rendering using VKeyboardRenderer
If you want to customize keyboard rendering you could provide a ``VKeyboardRenderer`` instance at ``VKeyboard``construction.
```python
keyboard = VKeyboard(surface, consumer, layout, renderer=VKeyboardRenderer.DARK)
```
Here is the list of default renderers provided with ``pygame-vkeyboard``:
- VKeyboardRenderer.DEFAULT
- VKeyboardRenderer.DARK
A custom ``VKeyboardRenderer`` can be built using following constructor :
```python
renderer = VKeyboardRenderer(
# Key font name/path.
'arial',
# Text color for key and text box (one per state: released, pressed).
((0, 0, 0), (255, 255, 255)),
# Text box cursor color.
(0, 0, 0),
# Color to highlight the selected key.
(20, 200, 98),
# Keyboard background color.
(50, 50, 50),
# Key background color (one per state, as for the text color).
((255, 255, 255), (0, 0, 0)),
# Text input background color.
(220, 220, 220),
# Optional special key text color (one per state, as for the text color).
((0, 250, 0), (255, 255, 255)),
# Optional special key background color (one per state, as for the text color).
((255, 255, 255), (0, 0, 0)),
)
```
Please note that the default renderer implementation require a unicode font.
You can also create your own renderer. Just override ``VKeyboardRenderer``class and override any of the following methods :
- **draw_background(surface)**: Draws the background of the keyboard.
- **draw_text(surface, text)**: Draws the text of the text input box.
- **draw_cursor(surface, cursor)**: Draws the cursor of the text input box.
- **draw_character_key(surface, key, special=False)**: Draws a key based on character value.
- **draw_space_key(surface, key)**: Draws space bar.
- **draw_back_key(surface, key)**: Draws back key.
- **draw_uppercase_key(surface, key)**: Draw uppercase switch key.
- **draw_special_char_key(surface, key)**: Draw special character switch key.
## Getting/Setting data
Several information can be retrieved from the keyboard:
```python
keyboard = VKeyboard(...)
# Get a pygame.Rect object in which the keyboard is included.
keyboard.get_rect()
# Get the current text.
keyboard.get_text()
# Set the current text (clear the existing one).
keyboard.set_text("Hello world!")
# Enable the keyboard, it will be displayed on next keyboard.draw() call.
keyboard.enable()
# Return True if the keyboard is enabled (thus displayed at screen).
keyboard.is_enabled()
# Disable and hide the keyboard (keyboard.update() and keyboard.draw() have no effect).
keyboard.disable()
```
## Run examples
Several examples are provided with the **pygame_vkeyboard** library.
To run the examples, simply execute these commands in a terminal:
```bash
python -m pygame_vkeyboard.examples.azerty
python -m pygame_vkeyboard.examples.numeric
python -m pygame_vkeyboard.examples.textinput
python -m pygame_vkeyboard.examples.resize
```
## Contributing
If you develop you own renderer please share it ! I will keep a collection of rendering class in this repository.
Don't hesitate to report bug, feedback, suggestion into the repository issues section.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共22个文件
py:11个
txt:5个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:pygame-vkeyboard-2.0.9.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
pygame-vkeyboard-2.0.9.tar.gz (22个子文件)
pygame-vkeyboard-2.0.9
MANIFEST.in 69B
PKG-INFO 8KB
setup.cfg 38B
pygame_vkeyboard.egg-info
PKG-INFO 8KB
requires.txt 7B
SOURCES.txt 606B
top_level.txt 17B
dependency_links.txt 1B
setup.py 1KB
pygame_vkeyboard
vtextinput.py 18KB
vrenderers.py 13KB
vkeyboard.py 27KB
DejaVuSans.ttf 703KB
__init__.py 208B
vkeys.py 11KB
examples
textinput.py 1KB
resize.py 2KB
__init__.py 63B
numeric.py 1KB
azerty.py 1KB
README.md 7KB
LICENSE.txt 11KB
共 22 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 华为云会议安装包适配华为擎云L410 -1060 ARM架构
- 清空redis缓存脚本指令
- STM8S微控制器-寄存器操作指南
- apk一键安装,获取apk签名信息
- opencv4.8.0+contrib+cuda文件
- 【全网最新最全】大气公司年度月度总结汇报PPT
- C语言实现低功耗STM32F411开发板(原理图+PCB源文件+官方例程+驱动等).zip
- C语言实现基于STM32 的联合调试侦听设备解决方案(原理图、PCB源文件、调试工具、视频).zip
- 【全网最全最酷】部门年度年终工作总结汇报PPT模板
- C语言实现基于STM32F103RC的电子相册(原理图、PCB源文件、程序源码及制作).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功