def get_keyboard_events(self):
        """Asks :mod:`pySDL2` for recent keyboard events and translates them into an array
        of events similar to what would be returned by :meth:`pinproc.PinPROC.get_events`."""
        # print event.key.keysym.sym <-- number
        # print sdl2.SDL_GetKeyName(event.key.keysym.sym) <-- name

        for event in sdl2.ext.get_events():
            EventManager.default().post(name=self.event_name_for_pygame_event_type(event.type), object=self, info=event)
            key_event = {}
            #print("Key: %s" % event.key.keysym.sym)
            if event.type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_LCTRL or event.key.keysym.sym == sdl2.SDLK_RCTRL:
                    self.ctrl = 1
                if event.key.keysym.sym == sdl2.SDLK_c:
                    if self.ctrl == 1:
                        key_event['type'] = self.exit_event_type
                        key_event['value'] = 'quit'
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    key_event['type'] = self.exit_event_type
                    key_event['value'] = 'quit'
                elif event.key.keysym.sym in self.key_map:
                    key_event['type'] = pinproc.EventTypeSwitchClosedDebounced
                    key_event['value'] = self.key_map[event.key.keysym.sym]
            elif event.type == sdl2.SDL_KEYUP:
                if event.key.keysym.sym == sdl2.SDLK_LCTRL or event.key.keysym.sym == sdl2.SDLK_RCTRL:
                    self.ctrl = 0
                elif event.key.keysym.sym in self.key_map:
                    key_event['type'] = pinproc.EventTypeSwitchOpenDebounced
                    key_event['value'] = self.key_map[event.key.keysym.sym]
            if len(key_event):
                self.key_events.append(key_event)
        e = self.key_events
        self.key_events = []
        return e
Beispiel #2
0
	def get_keyboard_events(self):
		"""Asks :mod:`pygame` for recent keyboard events and translates them into an array
		of events similar to what would be returned by :meth:`pinproc.PinPROC.get_events`."""
		#self.key_events = []
		for event in pygame.event.get():
			EventManager.default().post(name=self.event_name_for_pygame_event_type(event.type), object=self, info=event)
			key_event = {}
			if event.type == pygame.locals.KEYDOWN:
				if event.key == pygame.locals.K_RCTRL or event.key == pygame.locals.K_LCTRL:
					self.ctrl = 1
				if event.key == pygame.locals.K_c:
					if self.ctrl == 1:
						key_event['type'] = self.exit_event_type
						key_event['value'] = 'quit'
				elif (event.key == pygame.locals.K_ESCAPE):
					key_event['type'] = self.exit_event_type
					key_event['value'] = 'quit'
				elif event.key in self.key_map:
					key_event['type'] = pinproc.EventTypeSwitchClosedDebounced
					key_event['value'] = self.key_map[event.key]
			elif event.type == pygame.locals.KEYUP:
				if event.key == pygame.locals.K_RCTRL or event.key == pygame.locals.K_LCTRL:
					self.ctrl = 0
				elif event.key in self.key_map:
					key_event['type'] = pinproc.EventTypeSwitchOpenDebounced
					key_event['value'] = self.key_map[event.key]
			if len(key_event):
				self.key_events.append(key_event)
		e = self.key_events
		self.key_events = []
		return e
 def get_keyboard_events(self):
     """Asks :mod:`pygame` for recent keyboard events and translates them into an array
     of events similar to what would be returned by :meth:`pinproc.PinPROC.get_events`."""
     key_events = []
     for event in pygame.event.get():
         EventManager.default().post(name=self.event_name_for_pygame_event_type(event.type), object=self, info=event)
         key_event = {}
         if event.type == pygame.locals.KEYDOWN:
             if event.key == pygame.locals.K_RCTRL or event.key == pygame.locals.K_LCTRL:
                 self.ctrl = 1
             if event.key == pygame.locals.K_c:
                 if self.ctrl == 1:
                     key_event['type'] = self.exit_event_type
                     key_event['value'] = 'quit'
             elif (event.key == pygame.locals.K_ESCAPE):
                 key_event['type'] = self.exit_event_type
                 key_event['value'] = 'quit'
             elif event.key in self.key_map:
                 key_event['type'] = pinproc.EventTypeSwitchClosedDebounced
                 key_event['value'] = self.key_map[event.key]
         elif event.type == pygame.locals.KEYUP:
             if event.key == pygame.locals.K_RCTRL or event.key == pygame.locals.K_LCTRL:
                 self.ctrl = 0
             elif event.key in self.key_map:
                 key_event['type'] = pinproc.EventTypeSwitchOpenDebounced
                 key_event['value'] = self.key_map[event.key]
         if len(key_event):
             key_events.append(key_event)
     return key_events
    def get_keyboard_events(self):
        """Asks :mod:`pySDL2` for recent keyboard events and translates them into an array
        of events similar to what would be returned by :meth:`pinproc.PinPROC.get_events`."""
        # print event.key.keysym.sym <-- number
        # print sdl2.SDL_GetKeyName(event.key.keysym.sym) <-- name

        for event in sdl2.ext.get_events():
            EventManager.default().post(
                name=self.event_name_for_pygame_event_type(event.type),
                object=self,
                info=event)
            key_event = {}
            #print("Key: %s" % event.key.keysym.sym)
            if event.type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_LCTRL or event.key.keysym.sym == sdl2.SDLK_RCTRL:
                    self.ctrl = 1
                if event.key.keysym.sym == sdl2.SDLK_c:
                    if self.ctrl == 1:
                        key_event['type'] = self.exit_event_type
                        key_event['value'] = 'quit'
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    key_event['type'] = self.exit_event_type
                    key_event['value'] = 'quit'
                elif event.key.keysym.sym in self.key_map:
                    key_event['type'] = pinproc.EventTypeSwitchClosedDebounced
                    key_event['value'] = self.key_map[event.key.keysym.sym]
            elif event.type == sdl2.SDL_KEYUP:
                if event.key.keysym.sym == sdl2.SDLK_LCTRL or event.key.keysym.sym == sdl2.SDLK_RCTRL:
                    self.ctrl = 0
                elif event.key.keysym.sym in self.key_map:
                    key_event['type'] = pinproc.EventTypeSwitchOpenDebounced
                    key_event['value'] = self.key_map[event.key.keysym.sym]
            if len(key_event):
                self.key_events.append(key_event)
        e = self.key_events
        self.key_events = []
        return e
Beispiel #5
0
	def setUp(self):
		self.events = EventManager()
		self.events.add_event_handler(name=TEST_EVENT, object=None, handler=self.handler_no_obj)
		self.events.add_event_handler(name=TEST_EVENT, object=self, handler=self.handler_obj)
		self.flag = 0
Beispiel #6
0
class EventsTest(unittest.TestCase):

	def setUp(self):
		self.events = EventManager()
		self.events.add_event_handler(name=TEST_EVENT, object=None, handler=self.handler_no_obj)
		self.events.add_event_handler(name=TEST_EVENT, object=self, handler=self.handler_obj)
		self.flag = 0

	def handler_no_obj(self, event):
		self.flag += 1
	def handler_obj(self, event):
		self.flag += 2
	
	def test_setup(self):
		self.assertEqual(self.flag, 0)

	def test_unknown_name(self):
		self.events.post(name=TEST_EVENT+'blah', object=None)
		self.assertEqual(self.flag, 0)

	def test_None_obj(self):
		self.events.post(name=TEST_EVENT, object=None)
		self.assertEqual(self.flag, 2)

	def test_obj_set(self):
		self.events.post(name=TEST_EVENT, object=self)
		self.assertEqual(self.flag, 3)

	def test_obj_other(self):
		self.events.post(name=TEST_EVENT, object='1234')
		self.assertEqual(self.flag, 1)

	def test_remove(self):
		self.events.remove_event_handler(handler=self.handler_obj)
		self.events.post(name=TEST_EVENT, object=self)
		self.assertEqual(self.flag, 1)