def __init__(self, device=-1, bufferSize=10000, waitForStart=False, clock=None): """Create the device (default keyboard or select one) Parameters ---------- device: int or dict On Linux/Mac this can be a device index or a dict containing the device info (as from :func:`getKeyboards`) or -1 for all devices acting as a unified Keyboard bufferSize: int How many keys to store in the buffer (before dropping older ones) waitForStart: bool (default False) Normally we'll start polling the Keyboard at all times but you could choose not to do that and start/stop manually instead by setting this to True """ self.status = NOT_STARTED # Initiate containers for storing responses self.keys = [] # the key(s) pressed self.corr = 0 # was the resp correct this trial? (0=no, 1=yes) self.rt = [] # response time(s) self.time = [] # Epoch if clock: self.clock = clock else: self.clock = psychopy.clock.Clock() if havePTB: # get the necessary keyboard buffer(s) if sys.platform=='win32': self._ids = [-1] # no indexing possible so get the combo keyboard else: allInds, allNames, allKBs = hid.get_keyboard_indices() if device==-1: self._ids = allInds elif type(device) in [list, tuple]: self._ids = device else: self._ids = [device] self._buffers = {} self._devs = {} for devId in self._ids: # now we have a list of device IDs to monitor if devId==-1 or devId in allInds: buffer = _keyBuffers.getBuffer(devId, bufferSize) self._buffers[devId] = buffer self._devs[devId] = buffer.dev if not waitForStart: self.start()
def __init__(self, device=-1, bufferSize=10000, waitForStart=False, clock=None): """Create the device (default keyboard or select one) Parameters ---------- device: int or dict On Linux/Mac this can be a device index or a dict containing the device info (as from :func:`getKeyboards`) or -1 for all devices acting as a unified Keyboard bufferSize: int How many keys to store in the buffer (before dropping older ones) waitForStart: bool (default False) Normally we'll start polling the Keyboard at all times but you could choose not to do that and start/stop manually instead by setting this to True """ self.status = NOT_STARTED # Initiate containers for storing responses self.keys = [] # the key(s) pressed self.corr = 0 # was the resp correct this trial? (0=no, 1=yes) self.rt = [] # response time(s) self.time = [] # Epoch if clock: self.clock = clock else: self.clock = psychopy.clock.Clock() if havePTB: # get the necessary keyboard buffer(s) if sys.platform=='win32': self._ids = [-1] # no indexing possible so get the combo keyboard else: allInds, allNames, allKBs = hid.get_keyboard_indices() if device==-1: self._ids = allInds elif type(device) in [list, tuple]: self._ids = device else: self._ids = [device] self._buffers = {} self._devs = {} for devId in self._ids: # now we have a list of device IDs to monitor if devId==-1 or devId in allInds: buffer = _keyBuffers.getBuffer(devId, bufferSize) self._buffers[devId] = buffer self._devs[devId] = buffer.dev if not waitForStart: self.start()
def __init__(self, bufferSize, kb_id): self.bufferSize = bufferSize self._evts = deque() # create the PTB keyboard object and corresponding queue allInds, names, keyboards = hid.get_keyboard_indices() self._keys = deque() self._keysStillDown = deque() if kb_id == -1: self.dev = hid.Keyboard() # a PTB keyboard object else: self.dev = hid.Keyboard(kb_id) # a PTB keyboard object self.dev._create_queue(bufferSize)
def __init__(self, bufferSize, kb_id): self.bufferSize = bufferSize self._evts = deque() # create the PTB keyboard object and corresponding queue allInds, names, keyboards = hid.get_keyboard_indices() self._keys = [] self._keysStillDown = [] if kb_id == -1: self.dev = hid.Keyboard() # a PTB keyboard object else: self.dev = hid.Keyboard(kb_id) # a PTB keyboard object self.dev._create_queue(bufferSize)
def getKeyboards(): """Get info about the available keyboards. Only really useful on Mac/Linux because on these the info can be used to select a particular physical device when calling :class:`Keyboard`. On Win this function does return information correctly but the :class:Keyboard can't make use of it. Returns ---------- A list of dicts USB Info including with name, manufacturer, id, etc for each device """ indices, names, keyboards = hid.get_keyboard_indices() return keyboards
def getKeyboards(): """Get info about the available keyboards. Only really useful on Mac/Linux because on these the info can be used to select a particular physical device when calling :class:`Keyboard`. On Win this function does return information correctly but the :class:Keyboard can't make use of it. Returns ---------- A list of dicts USB Info including with name, manufacturer, id, etc for each device """ indices, names, keyboards = hid.get_keyboard_indices() return keyboards
def __init__(self, device=-1, bufferSize=10000, waitForStart=False, clock=None, backend=None): """Create the device (default keyboard or select one) Parameters ---------- device: int or dict On Linux/Mac this can be a device index or a dict containing the device info (as from :func:`getKeyboards`) or -1 for all devices acting as a unified Keyboard bufferSize: int How many keys to store in the buffer (before dropping older ones) waitForStart: bool (default False) Normally we'll start polling the Keyboard at all times but you could choose not to do that and start/stop manually instead by setting this to True """ global havePTB if self._backend is None and backend in ['iohub', 'ptb', 'event', '']: Keyboard._backend = backend if self._backend is None: Keyboard._backend = '' if backend and self._backend != backend: logging.warning("keyboard.Keyboard already using '%s' backend. Can not switch to '%s'" % (self._backend, backend)) self.status = NOT_STARTED # Initiate containers for storing responses self.keys = [] # the key(s) pressed self.corr = 0 # was the resp correct this trial? (0=no, 1=yes) self.rt = [] # response time(s) self.time = [] # Epoch if clock: self.clock = clock else: self.clock = psychopy.clock.Clock() if Keyboard._backend in ['', 'iohub']: from psychopy.iohub.client import ioHubConnection from psychopy.iohub.devices import Computer if not ioHubConnection.getActiveConnection() and Keyboard._backend == 'iohub': # iohub backend was explicitly requested, but iohub is not running, so start it up # setting keyboard to use standard psychopy key mappings from psychopy.iohub import launchHubServer launchHubServer(Keyboard=dict(use_keymap='psychopy')) if ioHubConnection.getActiveConnection() and Keyboard._iohubKeyboard is None: Keyboard._iohubKeyboard = ioHubConnection.getActiveConnection().getDevice('keyboard') Keyboard._iohubOffset = Computer.global_clock.getLastResetTime() Keyboard._backend = 'iohub' if Keyboard._backend in ['', 'ptb'] and havePTB: Keyboard._backend = 'ptb' Keyboard._ptbOffset = self.clock.getLastResetTime() # get the necessary keyboard buffer(s) if sys.platform == 'win32': self._ids = [-1] # no indexing possible so get the combo keyboard else: allInds, allNames, allKBs = hid.get_keyboard_indices() if device == -1: self._ids = allInds elif type(device) in [list, tuple]: self._ids = device else: self._ids = [device] self._buffers = {} self._devs = {} for devId in self._ids: # now we have a list of device IDs to monitor if devId == -1 or devId in allInds: buffer = _keyBuffers.getBuffer(devId, bufferSize) self._buffers[devId] = buffer self._devs[devId] = buffer.dev # Is this right, waiting if waitForStart=False?? if not waitForStart: self.start() if Keyboard._backend in ['', 'event']: global event from psychopy import event Keyboard._backend = 'event' logging.info('keyboard.Keyboard is using %s backend.' % Keyboard._backend)
def getKeyboards(): """Get info about the available keyboards""" indices, names, keyboards = hid.get_keyboard_indices() return keyboards
# The test will be subject to change without notice, it is throwaway wip code. # # (c) 2019 Jon Peirce - Licensed under MIT license. # (c) 2018 Mario Kleiner - Licensed under MIT license. from psychtoolbox import hid import psychtoolbox as ptb print("Devices:") for dev in hid.devices(): print(" %(index)s: %(product)s " "usagePageValue=%(usagePageValue)s, usageValue=%(usageValue)s" % dev) print("Keyboards:") kbIDs, kbNames, kbDevices = hid.get_keyboard_indices() for dev in kbDevices: print(" %(index)s: %(product)s" % dev) def run(): dev = kbDevices[0] # KbQueueTest ptb.WaitSecs('YieldSecs', 1) keyboard = hid.Keyboard(dev['index']) # keyboard.queue_create(num_slots=10000) # done implicitly but can redo # keyboard.start_trapping() # stops keys going to stdout but sketchy! keyboard.queue_start()