- super(Mapping, self).__init__(**kwargs)
- self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
- self._keyboard.bind(on_key_down=self._on_keyboard_down)
- self.running = []
- self.wait_ids = {}
- Clock.schedule_interval(self.not_all_keys_ready, 1)
+ def on_enter_loading(self):
+ for key in self.keys:
+ key.reload()
+ self.success()
+
+ # Kivy events
+ def add_widget(self, widget, index=0):
+ if type(widget).__name__ == "Key" and widget not in self.keys:
+ self.keys.append(widget)
+ return super(Mapping, self).add_widget(widget, index)
+
+ def remove_widget(self, widget, index=0):
+ if type(widget).__name__ == "Key" and widget in self.keys:
+ self.keys.remove(widget)
+ return super(Mapping, self).remove_widget(widget, index)
+
+ def on_keyboard_closed(self):
+ self.keyboard.unbind(on_key_down=self.on_keyboard_down)
+ self.keyboard = None
+
+ def on_keyboard_down(self, keyboard, keycode, text, modifiers):
+ key = self.find_by_key_code(keycode)
+ if self.allowed_modifiers(modifiers) and key is not None:
+ modifiers.sort()
+ threading.Thread(name="MSKeyAction", target=key.run,
+ args=['-'.join(modifiers)]).start()
+ elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
+ self.stop_all_running()
+ for thread in threading.enumerate():
+ if thread.getName()[0:2] != "MS":
+ continue
+ thread.join()
+
+ sys.exit()
+ elif 'ctrl' in modifiers and keycode[0] == 114:
+ threading.Thread(name="MSReload", target=self.reload).start()
+ return True
+
+ # Helpers
+ def allowed_modifiers(self, modifiers):
+ allowed = []
+ return len([a for a in modifiers if a not in allowed]) == 0
+
+ def find_by_key_code(self, key_code):
+ if "Key_" + str(key_code[0]) in self.ids:
+ return self.ids["Key_" + str(key_code[0])]
+ return None