Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. from kivy.core.window.window_sdl2 import WindowSDL
  2. from kivy.uix.floatlayout import FloatLayout
  3. from kivy.properties import StringProperty
  4. from kivy.core.window import Window
  5. from kivy.app import runTouchApp
  6. from kivy.logger import Logger
  7. from kivy.lang import Builder
  8. from kivy.clock import Clock
  9. from time import time
  10.  
  11. K_TAB = 9
  12. def patch_window():
  13. def new_release_keyboard(self, *largs):
  14. super(WindowSDL, self).release_keyboard(*largs)
  15. if self._system_keyboard.widget:
  16. self._win.hide_keyboard()
  17. self._sdl_keyboard = None
  18. return True
  19. WindowSDL.release_keyboard = new_release_keyboard
  20. Logger.warning('window_patch: patched kivy Window.release_keyboard')
  21.  
  22. class RootWidget(FloatLayout):
  23. scheduled_testing = False
  24. lbltext = StringProperty('Text from Window.on_textinput event:\n')
  25. last_text = StringProperty()
  26. last_key = StringProperty()
  27. instructions = StringProperty(
  28. 'What is this supposed to do?: \n'
  29. '0: last_textinput label should be displaying your last input text\n'
  30. '1: text from window widget should display all input text\n'
  31. '\n\n'
  32. 'Instructions:\n\n0: Press a few letters, '
  33. 'notice that everything works\n'
  34. '1: Press TAB twice to toggle '
  35. 'focus on TextInput widget\n'
  36. '2: Press a few more letters, '
  37. 'notice that sdl2 on_textinput event is no longer received\n'
  38. '3: Press ctrl to apply window patch and toggle TextInput focus again\n'
  39. '4: Press a few letters, notice that everything is fixed'
  40. )
  41.  
  42. def __init__(self, **kwargs):
  43. super(RootWidget, self).__init__(**kwargs)
  44. Window.bind(on_textinput=self.on_textinput)
  45. Window.bind(on_key_down=self.on_key_down)
  46.  
  47. def on_textinput(self, _, text):
  48. Logger.info('RootWidget: on_textinput(%s)' % (text))
  49. self.lbltext += text
  50. self.last_text = text
  51.  
  52. def on_key_down(self, _, k, *args):
  53. Logger.info('RootWidget: on_key_down(%s)' % (k))
  54. self.last_key = str(k)
  55. if k in (305, 306):
  56. patch_window()
  57. if k == K_TAB:
  58. tw = self.ids.textinput
  59. tw.focus = not tw.focus
  60.  
  61.  
  62. runTouchApp(Builder.load_string('''
  63. <AppLabel@Label>:
  64. markup: True
  65. text_size: self.width, None
  66. halign: 'left'
  67. valign: 'top'
  68. background_color: 0.2, 0.2, 0.2, 1
  69. canvas.before:
  70. Color:
  71. rgba: self.background_color
  72. Rectangle:
  73. pos: self.pos
  74. size: self.size
  75.  
  76. RootWidget:
  77. AppLabel:
  78. size_hint: 1, 0.5
  79. pos: 0, root.top - self.height
  80. text_size: self.size
  81. text: root.instructions
  82.  
  83. AppLabel:
  84. text: 'Last textinput: %s' % (root.last_text)
  85. size_hint: 0.5, 0.1
  86. pos_hint: {'center_x': 0.25, 'center_y': 0.45}
  87. background_color: 0.4, 0.2, 0.2, 1
  88.  
  89. AppLabel:
  90. text: 'Last key_down: %s' % (root.last_key)
  91. size_hint: 0.5, 0.1
  92. pos_hint: {'center_x': 0.75, 'center_y': 0.45}
  93. background_color: 0.2, 0.4, 0.2, 1
  94.  
  95. AppLabel:
  96. text: root.lbltext
  97. size_hint: 1, 0.1
  98. pos_hint: {'center_x': 0.5, 'center_y': 0.35}
  99. text_size: self.size
  100.  
  101. TextInput:
  102. id: textinput
  103. size_hint: 1, 0.3
  104. background_active: ''
  105. background_normal: ''
  106. background_disabled_normal: ''
  107. background_color: (0.7, 0.3, 0.3, 1) if self.focus else (0.9, 0.9, 0.9, 1)
  108. pos_hint: {'center_x': 0.5, 'center_y': 0.15}
  109. hint_text: 'TextInput widget'
  110. '''))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement