title: Python (kivy) - Continuous key press causes a loop?
body: <p>My code works fine when I press the spacebar once at a time. However, when continuously pressed it creates a loop of some sort and crashes.
  What might be the reason and how can I fix this? Thanks!!</p>
  
  <pre><code>import kivy
  import csv
  import time
  # import xlsxwriter
  import sys
  
  kivy.require('1.0.8')
  from kivy.core.window import Window
  from kivy.uix.widget import Widget
  from kivy.uix.button import Button
  from kivy.app import App
  from kivy.uix.widget import Widget
  from kivy.uix.floatlayout import FloatLayout
  from kivy.core.window import Window, Keyboard
  from kivy.graphics import *
  from kivy.uix.label import Label
  from kivy.uix.textinput import TextInput
  from kivy.properties import StringProperty, ObjectProperty
  from scipy.spatial import distance
  from kivy.clock import Clock
  import numpy
  
  Window.size = (1280, 1024)
  Window.fullscreen = True
  
  a = 78.3
  b = 156.6
  c = 234.9
  q = 30
  L = 250
  R = 1015
  O = 630
  
  stim_list = [1, a, q, c, q, b, q, b, q, c, q, c, q, c, q, a, q, b, q, b, q, c, q, a, q, a, q, b, q, a, q, a, q, b, q, c,
  
               q, a, q, b, q, a, q, c, q, c, q, a, q, b, q, b, q, c, q, a, q, a, q, b, q, b, q, a, q, c, q, c, q, c, q, b,
  
               q, b, q, c, q, c, q, a, q, b, q, a, q, b, q, b, q, b, q, a, q, c, q, a, q, c, q, c, q, c, q, b, q, b, q, b,
  
               q, c, q, b, q, c, q, a, q, a, q, a, q]
  
  side_list = [1, R, O, L, O, L, O, R, O, R, O, R, O, L, O, R, O, L, O, R, O, L, O, R, O, L, O, R, O, R, O, L, O, L, O, R,
  
               O, R, O, R, O, L, O, L, O, R, O, R, O, R, O, L, O, L, O, L, O, L, O, L, O, R, O, L, O, L, O, L, O, R, O, L,
  
               O, L, O, R, O, R, O, L, O, L, O, L, O, L, O, R, O, R, O, R, O, L, O, R, O, L, O, R, O, L, O, R, O, R, O, R,
  
               O, L, O, L, O, R, O, L, O, R, O, R, O]
  
  side_list_b = side_list[::-1]
  stim_list_b = stim_list[::-1]
  i = 0
  stimwidth = 45
  initialx = 650
  
  # workbook = xlsxwriter.Workbook('MT.xlsx')
  # worksheet = workbook.add_worksheet()
  # row = 0
  # col = 0
  
  class MyKeyboardListener(Widget):
      def __init__(self, **kwargs):
  
          super(MyKeyboardListener, self).__init__(**kwargs)
          keyboard = Window.request_keyboard(self._keyboard_released, self)
          self._keyboard = keyboard
          keyboard.bind(on_key_down=self._keyboard_on_key_down)
          self._keyboard = Window.request_keyboard(
              self._keyboard_closed, self, 'text')
          if self._keyboard.widget:
              # If it exists, this widget is a VKeyboard object which you can use
              # to change the keyboard layout.    
              pass    
          # self._keyboard.bind(on_key_down=self._on_keyboard_down)    
          self._keyboard.bind(on_key_up=self._on_keyboard_up)    
          self.startTime = time.time()    
          self.endTime = time.time()    
          self.stage = 0    
          self.firstTouch = []    
          self.isPressed = False
          self.stimPresented = time.time()    
          self.fileName = sys.argv[1]    
          f = open(self.fileName, 'w+')    
          f.close()    
  
      def _keyboard_closed(self):
          print('My keyboard have been closed!')
          self._keyboard.unbind(on_key_down=self._on_keyboard_down)
          self._keyboard = None
  
      # def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
      #     print('The key', keycode[0], 'have been pressed')
      #     # print(' - text is %r' % text)
      #     # print(' - modifiers are %r' % modifiers)
      #     if (keycode[0] == 32):
      #         print("sapce is printed")
  
      def _on_keyboard_up(self, keyboard, keycode):
          print('The key', keycode[0], 'have been pressed')
          # print(' - text is %r' % text)
          # print(' - modifiers are %r' % modifiers)
          #space key is up
          if (keycode[0] == 32):
              print("sapce is released")
              self.stage = 1
              self.startTime = time.time()
              self.add_diff_row(self.startTime,self.stimPresented)
              self.isPressed = False
  
      def on_touch_down(self, touch):
          # if touch.is_double_tap:
          print("touch")
          touch.multitouch_sim = False
  
          if (self.stage == 2):
              # print(touch.pos)
              # dst = distance.euclidean(self.firstTouch, touch.pos)
              # print(dst)
              a = numpy.array(touch.pos)
              b = numpy.array(self.firstTouch)
              dist = numpy.linalg.norm(a - b)
              print(dist)
              self.add_diff_row(dist*0.02554, 0)
              self.stage = 3
  
          # screen first press
          if (self.stage == 1):
              self.stage = 2
              self.endTime = time.time()
              self.add_diff_row(self.endTime, self.startTime)
              #print(touch.pos)
              self.firstTouch = touch.pos
  
      ### Make the keyboard operatble
      def _keyboard_released(self):
          self.focus = False
          ### replace stim with the next one when the keyboard is pressed
  
  
      def present_stim(self, keyboard, keycode, text, modifiers):
          self.keycode = 36
          initialy = 400 - (stim_list_b[i] / 2)
          xlab = Label(text='X', pos=(side_list_b[i], 650))
          self.remove_widget(xlab)
          stimPresented = time.time()
          with self.canvas:
              self.canvas.clear()
              self.add_widget(xlab)
              Color(1., 1, 1)
              Rectangle(pos=(initialx, initialy), size=(stimwidth, stim_list_b[i]))
  
                  #see above
              self.add_diff_row(self.endTime,self.startTime)
          ###
  
      def _keyboard_on_key_down(self, keyboard, keycode, text, modifiers):
          global i
          if(keycode[0] == 32 and self.isPressed):
              i = i + 1
              Clock.schedule_once(lambda dt: self.present_stim(keyboard, keycode, text, modifiers), 3)
  
          # the space key is first down
          if (keycode[0] == 32 and not(self.isPressed)):
              self.isPressed = True
              # add row of time different
              # self.add_diff_row(self.endTime,self.startTime)
  
      ###
  
  
      def add_diff_row(self, value1, value2):
          data = [value1 - value2]
          with open(self.fileName, 'a') as f:
              print (data)
              writer = csv.writer(f)
              writer.writerow(data)
  
      ###Move the stim togetehr with fingers
      def on_touch_move(self, touch):
          initialy = 400 - (stim_list_b[i] / 2)
          self.canvas.clear()
          self.myWidget = Widget(size=(100, 100))
          self.add_widget(self.myWidget)
  
          # calculating fingers space only for stage 1.
          # if (self.stage == 2):
          #     #self.add_diff_row(touch.pos[0],0)
          #     print(touch.pos)
          #     self.stage = 3
          #
          # if (self.stage == 3):
          #     print(touch.pos)
          #     self.stage = 4
          #adding differenc in rows.
          #print touch.pos[1]
          if (touch.pos[1] &gt; initialy + stim_list_b[i] + 70) or (touch.pos[1] &lt; initialy - 20):
              with self.myWidget.canvas:
                  self.add_widget(Label(text='X', pos=(side_list_b[i], 650)))
                  Rectangle(pos=(initialx, initialy), size=(stimwidth, stim_list_b[i]))
          else:
              goodposx = touch.pos[0]
              goodposy = initialy
              with self.myWidget.canvas:
                  self.add_widget(Label(text='X', pos=(side_list_b[i], 650)))
                  Rectangle(pos=((goodposx - (stimwidth / 2)), initialy), size=(stimwidth, stim_list_b[i]))
  
                  # if 'multitouch_sim' in touch.profile:
                  #     print ('multi touch pressed ')
                  #     touch.multitouch_sim = False
                  #     self.endTime = datetime.datetime.now()
                  #     data = [self.startTime,self.endTime]
                  #     with open(r'res.csv', 'a') as f:
                  #         writer = csv.writer(f)
                  #         writer.writerow(data)
  
  # workbook.close()
  
  if __name__ == '__main__':
      from kivy.base import runTouchApp
      runTouchApp(MyKeyboardListener())
  </code></pre>