Table of contents
1.
Introduction
2.
VKeyboard
3.
Request Keyboard
4.
Implementation of the Approach
4.1.
Output
5.
Frequently Asked Question
5.1.
Is Kivy suitable for mobile apps?
5.2.
Is Kivy free?
5.3.
In Kivy, what is a canvas?
5.4.
What is a float layout in Kivy?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Virtual keyboard in kivy

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is a multi-platform Python GUI development library that runs on iOS, Android, Windows, OS X, and GNU/Linux. It aids in the development of apps that make use of cutting-edge multi-touch user interfaces. Kivy's core concept allows developers to create an app once and deploy it across all devices, making code reusable and deployable and enabling quick and easy interaction design and prototyping. 

In this article, with the help of an example, we'll look at a Virtual keyboard in Kivy Python. This post assumes you've done some Python programming before. If that is not the case, you can return here after reading our Basics of Python article.

VKeyboard

VKeyboard is a Kivy onscreen keyboard. Its works are intended to be transparent to the user. Its layout is completely adjustable, and a button in the widget's bottom right allows you to swap between different layouts. Using the widget directly is NOT recommended.

Modes

This virtual keyboard has two mode:

  • docked mode (VKeyboard.docked = True)When only one person is utilizing the computer, such as a tablet or personal computer, Docker mode is commonly used.
  • free mode: (VKeyboard.docked = False) Typically used on multi touch surfaces. Multiple virtual keyboards can be utilized on the screen in this mode.

You must explicitly call VKeyboard.setup mode() if the docked mode changes; otherwise, the change will have no effect. The VKeyboard, which is constructed on top of a Scatter, will change the scatter's behavior and position the keyboard near the target during that call (if target and docked mode are set).

Request Keyboard

The configuration is in charge of the virtual keyboard's instantiation. In the Configuration object, look for keyboard mode and keyboard layout.

If you want to make a widget that needs a keyboard, you should use the best way available on the platform rather than the virtual keyboard. In the Window, look at the request_keyboard() method.

You should write like this  (from 1.8.0, numeric.json can be in the same directory in which main.py is)when you want custom layout when you request the keyboard :

keyboard = Window.request_keyboard(
    self._keyboard_close, self)
if keyboard.widget:
    vkeyboard = self._keyboard.widget
    vkeyboard.layout = 'numeric.json'

 

Basic Approach:
1) import kivy
2) import kivyApp
3) import vkeyboard
4) set kivy version (optional)
5) Create the Vkeyboard class
6) Create the App class
7) return the vkeyboard class
8) Run the App

Implementation of the Approach

Now we'll look at a sample python application code that uses Kivy to show how the approach described above for vkeyboard works.
.py file

# to import kivy module
import kivy
    
# this is to restrict the kivy version i.e
# below this kivy version we cannot
# use the app
kivy.require("1.9.1")
    
# app:always used to refer to your application's instance
from kivy.app import App
 
# VKeyboard is an onscreen keyboard
# for Kivy. Its operation is intended
# to be transparent to the user.
from kivy.uix.vkeyboard import VKeyboard
 
# to create the vkeyboard
class Test(VKeyboard):
    user = VKeyboard()
 
# Create the App class
class VkeyboardKivyApp(App):
    def build(self):
        return Test()
 
# run the App
if __name__ == '__main__':
    VkeyboardKivyApp().run()

Output

Frequently Asked Question

Is Kivy suitable for mobile apps?

Kivy is a great option if you want people to be able to access your mobile app on multiple devices while maintaining a consistent appearance and feel.

 

Is Kivy free?

Kivy is a free and open-source Python framework for developing mobile apps and application software with a natural user interface (NUI).

 

In Kivy, what is a canvas?

The Canvas is the root object that a Widget can use to draw on. By default, every Widget in Kivy has a Canvas. You may create all of the drawing instructions while constructing a widget.

 

What is a float layout in Kivy?

Float layout groups widgets with proportional coordinates with the size_hint and pos_hint attributes. The values are numbers ranging from 0 to 1, showing a proportional relationship to the window size.

Conclusion

The Virtual keyboard in kivy and its implementation in Python have been fully studied in this article. You may learn and use a variety of Python frameworks and tools for application development.

We hope that this blog has helped you learn more about kivy's virtual keyboard and if you're interested in learning more, see our articles on  Application DevelopmentModules & Packages in Python, and Best Python ProjectsApplications Development Using Python. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Live masterclass