Beispiel #1
0
 def handle_event(self, event):
     self.event_buffer.append(event)
     logging.debug("caught key, new buffer: %s",
         [(Translator.key_to_name(key), key.keystate) for key in self.event_buffer])
     # a bit dirty, yet so
     if self.reset_expression(self.event_buffer)[0] is Expression.state_accept:
         self.flush_buffer()
         return
     keep = False
     result = Expression.state_reject
     for expression, handler in self.expressions:
         result, extracted = expression(self.event_buffer)
         if result is Expression.state_accept:
             logging.info("valid expression of '%s' is caught", expression)
             try:
                 logging.info("handler '%s' is in charge, params: %s",
                     handler, extracted)
                 handler(extracted)
             except Exception as e:
                 logging.error("error during handler execution: %s", e)
             keep = False
             break
         elif result is Expression.state_partial:
             keep = True
         elif result is Expression.state_reject:
             pass
         else:
             raise AttributeError("Unexpected expression reply")
     logging.debug("reply: %s", ["accept", "reject", "partial"][result])
     if not keep:
         self.flush_buffer()
Beispiel #2
0
 def test_char_to_code(self):
     Translator.set_device_type("key")
     matches = {"a": 30, "A": 30, "ENTER": 28, "NONE": None}
     for key, result in matches.items():
         self.assertEqual(Translator.char_to_code(key), result)
     Translator.set_device_type("button")
     self.assertEqual(Translator.char_to_code("TOUCH"), 330)
Beispiel #3
0
 def test_code_to_chat(self):
     Translator.set_device_type("key")
     matches = {"a": 30, "\r": 28}
     for key, code in matches.items():
         self.assertEqual(Translator.code_to_char(code), key)
     Translator.set_device_type("button")
     self.assertEqual(Translator.code_to_char(330), "<TOUCH>")
Beispiel #4
0
def listen_device(device):
    sleep(1)
    main = InputDevice("/dev/input/event0")
    while True:
        r,w,x = select([device, main], [], [])
        events = {}
        for dev in r:
            events[dev] = [event for event in dev.read()
                  if event.type == ecodes.EV_KEY
                    and KeyEvent(event).keystate is KeyEvent.key_down]
        if events.has_key(main):
            break
        for event in events[device]:
            name = Translator.key_to_name(KeyEvent(event)).upper()
            if name:
                print name