vkeyboard: enhance textinput tests + release_all_keyboards() method in window.

This commit is contained in:
Mathieu Virbel 2011-10-21 18:03:19 +02:00
parent daa8f33f48
commit 09e36f8ee4
2 changed files with 73 additions and 11 deletions

View File

@ -1,9 +1,59 @@
'''
Textinput tests
===============
This test is used to demonstrate virtual keyboard according to current
configuration.
Run this test as::
# use dock virtual keyboard (one instance)
python textinput.py -c kivy:keyboard_mode:dock
# use multi users virtual keyboard (multiples instance)
python textinput.py -c kivy:keyboard_mode:multi
# use system keyboard (one instance)
python textinput.py -c kivy:keyboard_mode:system
# use automatic detection from current platform
python textinput.py -c kivy:keyboard_mode:
'''
import kivy
kivy.require('1.0.8')
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.config import Config
from kivy.base import runTouchApp
if __name__ == '__main__':
root = Scatter(size_hint=(None, None))
root.add_widget(TextInput(size_hint=(None, None), size=(100, 50)))
root = FloatLayout()
# create a button to release everything
def release_all_keyboard(*l):
Window.release_all_keyboards()
btn = Button(text='Release\nall\nkeyboards', size_hint=(None, None),
halign='center')
btn.bind(on_release=release_all_keyboard)
root.add_widget(btn)
# show current configuration
lbl = 'Configuration keyboard_mode is %r, keyboard_layout is %r' % (
Config.get('kivy', 'keyboard_mode'),
Config.get('kivy', 'keyboard_layout'))
label = Label(text=lbl, size_hint_y=None, height=50, pos_hint={'top': 1})
root.add_widget(label)
s = Scatter(size_hint=(None, None), pos=(300, 300))
s.add_widget(TextInput(size_hint=(None, None), size=(100, 50)))
root.add_widget(s)
s = Scatter(size_hint=(None, None), pos=(400, 300), rotation=45)
s.add_widget(TextInput(size_hint=(None, None), size=(100, 50)))
root.add_widget(s)
runTouchApp(root)

View File

@ -223,6 +223,7 @@ class WindowBase(EventDispatcher):
self._size = (0, 0)
self._rotation = 0
self._clearcolor = [0, 0, 0, 0]
self._vkeyboard_cls = None
# event subsystem
self.register_event_type('on_draw')
@ -730,7 +731,18 @@ class WindowBase(EventDispatcher):
Set the VKeyboard class to use. If None set, it will use the
:class:`kivy.uix.vkeyboard.VKeyboard`.
'''
self.vkeyboard_cls = cls
self._vkeyboard_cls = cls
def release_all_keyboards(self):
'''.. versionadded:: 1.0.8
This will ensure that no virtual keyboard / system keyboard are actually
requested. All will be closed.
'''
for key in self._keyboards.keys()[:]:
keyboard = self._keyboards[key]
if keyboard:
keyboard.release()
def request_keyboard(self, callback, target):
'''.. versionadded:: 1.0.4
@ -774,14 +786,14 @@ class WindowBase(EventDispatcher):
# late import
global VKeyboard
if VKeyboard is None and self.vkeyboard_cls is None:
if VKeyboard is None and self._vkeyboard_cls is None:
from kivy.uix.vkeyboard import VKeyboard
self.vkeyboard_cls = VKeyboard
self._vkeyboard_cls = VKeyboard
# if the keyboard doesn't exist, create it.
key = 'single' if self.single_vkeyboard else target
if key not in self._keyboards:
vkeyboard = self.vkeyboard_cls()
vkeyboard = self._vkeyboard_cls()
keyboard = Keyboard(widget=vkeyboard, window=self)
vkeyboard.bind(
on_key_down=keyboard._on_vkeyboard_key_down,
@ -791,8 +803,8 @@ class WindowBase(EventDispatcher):
keyboard = self._keyboards[key]
# configure vkeyboard
keyboard.target = target
keyboard.callback = callback
keyboard.target = keyboard.widget.target = target
keyboard.callback = keyboard.widget.callback = callback
# add to the window
self.add_widget(keyboard.widget)
@ -827,8 +839,8 @@ class WindowBase(EventDispatcher):
callback()
keyboard.target = None
self.remove_widget(keyboard.widget)
if key != 'single':
self._keyboards.remove(key)
if key != 'single' and key in self._keyboards:
del self._keyboards[key]
elif self._system_keyboard.callback:
# this way will prevent possible recursion.
callback = self._system_keyboard.callback