def __init__(self, keyboardHandler=None, mouseHandler=None, joystickHandler=None, synHandler=None, unhandledHandler=None, wantKeyboard=True, wantMouse=True, wantJoystick=True): self.unhandledHandler = unhandledHandler self.streams = [] if wantKeyboard: keyboards = find_devices("kbd") for x in keyboards: self.streams.append(EventStream(x, "keyboard")) else: keyboards = [] print("keyboards =", keyboards) if wantMouse: mice = find_devices("mouse", butNot=keyboards) for x in mice: self.streams.append(EventStream(x, "mouse")) print("mice = ", mice) else: mice = [] if wantJoystick: joysticks = find_devices("js", butNot=keyboards + mice) for x in joysticks: self.streams.append(EventStream(x, "joystick")) print("joysticks =", joysticks) for x in self.streams: x.acquire_abs_info() self.handler = EventHandler.EventHandler(keyboardHandler, mouseHandler, joystickHandler, synHandler)
def grab_by_type(self, deviceType, deviceIndex=None, grab=True): """ Grab (or release) exclusive access to all devices of the given type. The devices are grabbed if grab is True and released if grab is False. If the deviceIndex is given, only that device is grabbed, otherwise all the devices of the same type are grabbed. All devices are grabbed to begin with. We might want to ungrab the keyboard for example to use it for text entry. While not grabbed, all key-down and key-hold events are filtered out, but that only works if the events are received and handled while the keyboard is still grabbed, and the loop may not have been running. So if we are grabbing a device, we call the handling loop first, so there are no outstanding events. Note that the filtering means that if you trigger the ungrab from a key-down event, the corrosponding key-up will be actioned before the subsequent grab, and you wont end up looping continuously. However it also means that you will see key-up events for all the text entry. Since it only affects a user-supplied key-handler, and key-ups do not usually trigger actions anyway, this is not likely to be a problem. If it is, you will have to filter them yourself. """ if grab: self.do_input_events() EventStream.grab_by_type(deviceType, deviceIndex, grab, self.streams)
def __init__(self, keyboardHandler=None, mouseHandler=None, joystickHandler=None, synHandler=None, unhandledHandler=None, wantKeyboard=True, wantMouse=True, wantJoystick=True): self.unhandledHandler = unhandledHandler self.streams = [ ] if wantKeyboard: keyboards = find_devices("kbd") self.streams += map(lambda x: EventStream(x, "keyboard"),keyboards) else: keyboards = [ ] print "keyboards =", keyboards if wantMouse: mice = find_devices("mouse", butNot=keyboards) print "mice = ", mice self.streams += map(lambda x: EventStream(x, "mouse"), mice) else: mice = [ ] if wantJoystick: joysticks = find_devices("js", butNot=keyboards+mice) print "joysticks =", joysticks js_streams = map(lambda x: EventStream(x, "joystick"), joysticks) self.streams += js_streams for x in self.streams: x.acquire_abs_info() self.handler = EventHandler.EventHandler( keyboardHandler, mouseHandler, joystickHandler, synHandler)
def do_input_events(self): """ Handle all events that have been triggered since the last call. """ for event in EventStream.allNext(self.streams): if self.handler.event(event) and self.unhandledHandler: self.unhandledHandler(event)
def do_input_events(self): """ Handle all events that have been triggered since the last call. """ for event in EventStream.allNext(self.streams): if event.eventType == None: self.streams.remove(event.stream) event.stream.release() continue if self.handler.event(event) and self.unhandledHandler: self.unhandledHandler(event)