Kivy – VKeyboard


Kivy – VKeyboard



”;


The VKeyboard widget in Kivy library is especially useful for applications running on multitouch devices such as smartphones and tablets. VKeyboard is an on-screen keyboard. Its operation is intended to be transparent to the user.

The VKeyboard is used in two modes − docked and free mode. The free mode is suitable for multitouch devices, whereas the docked mode is enabled while using a computer like a tablet.

The VKeyboard class is defined in kivy.uix.vkeyboard module.


from kivy.uix.vkeyboard import VKeyboard
kb = VKeyboard(**kwargs)

A virtual keyboard is never used directly. Instead, it is controlled by the configuration. If the application has any widget (such as TextInput) that requires a keyboard, do not use the virtual keyboard directly, but prefer to use the best method available on the platform.

The VKeyboard class inherits the ScatterLayout. A button in the bottom right of the virtual keyboard widget lets you switch between available layouts.

The VKeyboard object has the following properties −

  • available_layouts − Dictionary of all available layouts. Keys are the layout ID, and the value is the JSON, defaults to {}.

  • callback − Callback can be set to a function that will be called if the VKeyboard is closed by the user.

  • docked − Indicate whether the VKeyboard is docked on the screen or not. If you change it, you must manually call setup_mode() otherwise it will have no impact.

  • key_margin − Key margin, used to create space between keys. The margin is composed of four values, in pixels −


key_margin = [top, right, bottom, left]

key_margin defaults to [2, 2, 2, 2]

  • target − Target widget associated with the VKeyboard. If set, it will be used to send keyboard events.

Example

In the following example, the initial constitution of a one-column grid layout shows a label and a TextInput widget. As the user generates a touch_down event by clicking inside the text box, a VKeyboard widget is added to the layout.


def ontouch(self, instance, value):
   self.kb = VKeyboard(
      on_key_up = self.vkbinput,
      pos_hint={''center_x'':.5},
      size_hint=(.8, None)
   )
   self.layout.add_widget(self.kb)

When any key on the keyboard is pressed, it generates on_key_up event. It is bound to a vkbinput() method. This method reads the keycode of the depressed key and updates the contents of the text box.


def vkbinput(self, keyboard, keycode, *args):
   text = self.text1.text

   if keycode == ''~'':
      self.layout.remove_widget(self.kb)
      return
   
   self.text1.text = f''{text}{keycode}''

Whenever the user presses the “~” key, the keyboard widget is removed from the layout.

The complete code is given below −


from kivy.app import App
from kivy.uix.vkeyboard import VKeyboard
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window

Window.size = (720, 400)

class MainApp(App):
   def build(self):
      self.layout = GridLayout(cols=1)
      self.text1 = TextInput(
         font_size=32,
         on_touch_down=self.ontouch
      )
      self.label = Label(
         text="Enter Text....",
         font_size=32,
         color=[.8, .6, .1]
      )
      self.layout.add_widget(self.label)
      self.layout.add_widget(self.text1)
      return self.layout
      
   def ontouch(self, instance, value):
      self.kb = VKeyboard(
         on_key_up=self.vkbinput,
         pos_hint={''center_x'': .5},
         size_hint=(.8, None)
      )
      self.layout.add_widget(self.kb)

   def vkbinput(self, keyboard, keycode, *args):
      text = self.text1.text
      if keycode == ''~'':
         self.layout.remove_widget(self.kb)
         return
      self.text1.text = f''{text}{keycode}''

MainApp().run()

Output

When the above code runs, the initial display of the app window is like this −


Kivy Vkeyboard

Click inside the textbox, and the virtual keyboard appears below it −


Kivy Vkeyboard Enter Text

You can now enter the text. Press “~” to remove the keyboard.


Kivy Vkeyboard Remove Text

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *