[kivy-users] Keyboard events on_key_up (down) parameters

315 views
Skip to first unread message

Mr. Mog

unread,
Aug 13, 2021, 1:13:44 AM8/13/21
to kivy-...@googlegroups.com
Hello,

What are the types for the keyboard events on_key_up & on_key_down parameters (keycode, internal, modifiers)?

Robert

unread,
Aug 13, 2021, 3:32:15 AM8/13/21
to Kivy users support
Try for example:   print(type(keycode))

Mr. Mog

unread,
Aug 13, 2021, 5:14:50 AM8/13/21
to kivy-...@googlegroups.com
How do I even use the Vkeyboard?
My try:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from plyer import vibrator
from kivy.uix.vkeyboard import VKeyboard
from kivy.uix.textinput import TextInput




class MyWid(BoxLayout):
pass


class MyKBoard(VKeyboard):
c = 1
def on_key_up(self, *largs):
for i in largs:
print(f"[count {self.c}] {i} is of {type(i)} type")
self.c += 1
super().on_key_up(largs)



class MyApp(App):
def build(self):
box = MyWid(orientation="vertical")
kb = MyKBoard(size_hint=[.8, .5])
input = TextInput(keyboard=kb)
box.add_widget(input)
box.add_widget(kb)
return box
MyApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/43fb882a-230d-466f-870c-fe6eac933468n%40googlegroups.com.

Elliot Garbus

unread,
Aug 13, 2021, 9:54:15 AM8/13/21
to kivy-...@googlegroups.com

Mr. Mog

unread,
Aug 14, 2021, 9:24:36 AM8/14/21
to kivy-...@googlegroups.com
Please can you help me with a minimum example?

Elliot Garbus

unread,
Aug 14, 2021, 2:53:41 PM8/14/21
to kivy-...@googlegroups.com

The example below, uses the Config object to bring up the virtual keyboard.  Clicking on the left side of the screen outside of the TextInput will cause the VKeyboard to close.  Clicking on the text input or the right side of the screen will cause the virtual keyboard to open.  Pressing spacebar, up, left or right, will cause the appropriate section on the right side of the screen to highlight.

 

Note I have set the ids on the KeyLabels to match the keycodes to simplify the code.

 

from kivy.config import Config
# must be before kivy.app loads, this will use the kivy virtual & system keyboard
Config.set('kivy', 'keyboard_mode', 'systemanddock')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import NumericProperty


kv =
"""
<KeyLabel>:
    canvas:
        Color:
            rgb: .5, .5, .5
        Line:
            width: 2
            rectangle: (*self.pos, *self.size)
    canvas:
        Color:
            rgba: .3, .3, .3, root.down_opacity
        Rectangle:
            pos: self.pos
            size: self.size
   
BoxLayout:
    RelativeLayout:
        orientation: 'vertical'
        TextInput:
            hint_text: 'Enter Name Here'
            size_hint: .7, None
            height: 30
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    RightSideLayout:
        orientation: 'vertical'
        Label:
            text: 'Click on this side to capture keys'
            size_hint_y: None
            height: 48
        GridLayout:
            padding: 10
            size_hint_y: None
            height: 48 * 2
            cols: 3
            rows: 2
            KeyLabel:
            KeyLabel:
                id: up
                text: 'Up'
            KeyLabel:
            KeyLabel:
                id: left
                text: 'Left'
            KeyLabel:
                id: spacebar
                text: 'Spacebar'
            KeyLabel:
                id: right
                text: 'Right'
        Label:
"""


class KeyLabel(Label):
    down_opacity = NumericProperty()


class RightSideLayout(BoxLayout):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self._keyboard = None

    def
on_touch_down(self, touch):
       
if self.collide_point(*touch.pos):
           
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
           
self._keyboard.bind(on_key_down=self._on_keyboard_down)
           
self._keyboard.bind(on_key_up=self._on_keyboard_up)
           
print('Keyboard Opened')
           
return True
        return False

    def
_keyboard_closed(self):
       
print('Keyboard Closed')
       
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
       
self._keyboard.unbind(on_key_up=self._on_keyboard_up)
       
self._keyboard = None

    def
_on_keyboard_down(self, keyboard, keycode, text, modifiers):
        key = keycode[
1]
       
print(f'{key} pressed')
       
if key in ['left', 'right', 'up', 'spacebar']:
            app = App.get_running_app()
            app.root.ids[key].down_opacity =
.5

   
def _on_keyboard_up(self, keyboard, keycode):
        key = keycode[
1]
       
print(f'{key} released')
       
if key in ['left', 'right', 'up', 'spacebar']:
            app = App.get_running_app()
            app.root.ids[key].down_opacity =
0


class KeysApp(App):
   
def build(self):
       
return Builder.load_string(kv)


KeysApp().run()
Reply all
Reply to author
Forward
0 new messages