Previous topic

Encryption

Next topic

psychopy.filters - helper functions for creating filters

This Page

Quick links

psychopy.event - for keypresses and mouse clicks

class psychopy.event.Mouse(visible=True, newPos=None, win=None)

Easy way to track what your mouse is doing.

It needn’t be a class, but since Joystick works better as a class this may as well be one too for consistency

Create your visual.Window before creating a Mouse.

Parameters:
visible
: True or False

makes the mouse invisible if necessary

newPos
: None or [x,y]

gives the mouse a particular starting position (pygame Window only)

win
: None or Window

the window to which this mouse is attached (the first found if None provided)

clickReset(buttons=(0, 1, 2))

Reset a 3-item list of core.Clocks use in timing button clicks.

The pyglet mouse-button-pressed handler uses their clock.getLastResetTime() when a button is pressed so the user can reset them at stimulus onset or offset to measure RT. The default is to reset all, but they can be reset individually as specified in buttons list

getPos()

Returns the current position of the mouse, in the same units as the Window (0,0) is at centre

getPressed(getTime=False)

Returns a 3-item list indicating whether or not buttons 0,1,2 are currently pressed.

If getTime=True (False by default) then getPressed will return all buttons that have been pressed since the last call to mouse.clickReset as well as their time stamps:

buttons = mouse.getPressed()
buttons, times = mouse.getPressed(getTime=True)

Typically you want to call mouse.clickReset() at stimulus onset, then after the button is pressed in reaction to it, the total time elapsed from the last reset to click is in mouseTimes. This is the actual RT, regardless of when the call to getPressed() was made.

getRel()

Returns the new position of the mouse relative to the last call to getRel or getPos, in the same units as the Window.

getVisible()

Gets the visibility of the mouse (1 or 0)

getWheelRel()

Returns the travel of the mouse scroll wheel since last call. Returns a numpy.array(x,y) but for most wheels y is the only value that will change (except Mac mighty mice?)

isPressedIn(shape, buttons=(0, 1, 2))

Returns True if the mouse is currently inside the shape and one of the mouse buttons is pressed. The default is that any of the 3 buttons can indicate a click; for only a left-click, specifiy buttons=[0]:

if mouse.isPressedIn(shape):
if mouse.isPressedIn(shape, buttons=[0]):  # left-clicks only

Ideally, shape can be anything that has a .contains() method, like ShapeStim or Polygon. Not tested with ImageStim.

mouseMoveTime()
mouseMoved(distance=None, reset=False)

Determine whether/how far the mouse has moved.

With no args returns true if mouse has moved at all since last getPos() call, or distance (x,y) can be set to pos or neg distances from x and y to see if moved either x or y that far from lastPos, or distance can be an int/float to test if new coordinates are more than that far in a straight line from old coords.

Retrieve time of last movement from self.mouseClock.getTime().

Reset can be to ‘here’ or to screen coords (x,y) which allows measuring distance from there to mouse when moved. If reset is (x,y) and distance is set, then prevPos is set to (x,y) and distance from (x,y) to here is checked, mouse.lastPos is set as current (x,y) by getPos(), mouse.prevPos holds lastPos from last time mouseMoved was called.

next()
setExclusive(exclusivity)

Binds the mouse to the experiment window. Only works in Pyglet.

In multi-monitor settings, or with a window that is not fullscreen, the mouse pointer can drift, and thereby PsychoPy might not get the events from that window. setExclusive(True) works with Pyglet to bind the mouse to the experiment window.

Note that binding the mouse pointer to a window will cause the pointer to vanish, and absolute positions will no longer be meaningful getPos() returns [0, 0] in this case.

setPos(newPos=(0, 0))

Sets the current position of the mouse, in the same units as the Window. (0,0) is the center.

Parameters:
newPos
: (x,y) or [x,y]

the new position on the screen

setVisible(visible)

Sets the visibility of the mouse to 1 or 0

NB when the mouse is not visible its absolute position is held at (0, 0) to prevent it from going off the screen and getting lost! You can still use getRel() in that case.

units

The units for this mouse (will match the current units for the Window it lives in)

psychopy.event.clearEvents(eventType=None)

Clears all events currently in the event buffer.

Optional argument, eventType, specifies only certain types to be cleared.

Parameters:
eventType
: None, ‘mouse’, ‘joystick’, ‘keyboard’

If this is not None then only events of the given type are cleared

psychopy.event.waitKeys(maxWait=inf, keyList=None, modifiers=False, timeStamped=False, clearEvents=True)

Same as ~psychopy.event.getKeys, but halts everything (including drawing) while awaiting input from keyboard.

Parameters:
maxWait
: any numeric value.

Maximum number of seconds period and which keys to wait for. Default is float(‘inf’) which simply waits forever.

keyList
: None or []

Allows the user to specify a set of keys to check for. Only keypresses from this set of keys will be removed from the keyboard buffer. If the keyList is None, all keys will be checked and the key buffer will be cleared completely. NB, pygame doesn’t return timestamps (they are always 0)

modifiers
: False or True

If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, modifiers). The modifiers are a dict of keyboard modifier flags keyed by the modifier name (eg. ‘shift’, ‘ctrl’).

timeStamped
: False, True, or Clock

If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, time). If a core.Clock is given then the time will be relative to the Clock‘s last reset.

clearEvents
: True or False

Whether to clear the keyboard event buffer (and discard preceding keypresses) before starting to monitor for new keypresses.

Returns None if times out.

psychopy.event.getKeys(keyList=None, modifiers=False, timeStamped=False)

Returns a list of keys that were pressed.

Parameters:
keyList
: None or []

Allows the user to specify a set of keys to check for. Only keypresses from this set of keys will be removed from the keyboard buffer. If the keyList is None, all keys will be checked and the key buffer will be cleared completely. NB, pygame doesn’t return timestamps (they are always 0)

modifiers
: False or True

If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, modifiers). The modifiers are a dict of keyboard modifier flags keyed by the modifier name (eg. ‘shift’, ‘ctrl’).

timeStamped
: False, True, or Clock

If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, time). If a core.Clock is given then the time will be relative to the Clock‘s last reset.

Author:
  • 2003 written by Jon Peirce
  • 2009 keyList functionality added by Gary Strangman
  • 2009 timeStamped code provided by Dave Britton
  • 2016 modifiers code provided by 5AM Solutions
psychopy.event.xydist(p1=(0.0, 0.0), p2=(0.0, 0.0))

Helper function returning the cartesian distance between p1 and p2