You are viewing unmaintained documentation for an older version of OpenSesame. Click here to view the current documentation. Your version of OpenSesame:

Test the next generation of OpenSesame: 2.9.0 Hesitant Heisenberg!

Mouse functions

The mouse class is used to collect mouse input. For example, the following script lets you move around a fixation dot with the mouse until a button is clicked:

from openexp.mouse import mouse
from openexp.canvas import canvas
my_mouse = mouse(exp)
my_canvas = canvas(exp)
while True:
	button, position, timestamp = my_mouse.get_click(timeout=20)
	if button != None:
		break
	pos, time = my_mouse.get_pos()
	my_canvas.clear()
	my_canvas.fixdot(pos[0], pos[1])
	my_canvas.show()

Important note: When using a mouse all coordinates are specified relative to the top-left of the display, and not, as in sketchpads, relative to the display center. For example, the following script will determine the deviation of a mouse click relative to the display center.

from openexp.mouse import mouse
from math import sqrt
# Determine coordinates of display center
xc = self.get('width')/2
yc = self.get('height')/2
# Create a mouse object and collect a click
my_mouse = mouse(exp, visible=True)
button, position, timestamp = my_mouse.get_click()
# Unpack the position tuple into x and y coordinates
x, y = position
# Use Pythagoras to determine the distance to the display center
click_err = sqrt( (x-xc)**2 + (y-yc)**2 )
exp.set('click_err', click_err)

For OpenSesame 2.8.3

class mouse

Function overview:
__init__
flush
get_click
get_pos
get_pressed
set_buttonlist
set_pos
set_timeout
set_visible

mouse.__init__(experiment, buttonlist=None, timeout=None, visible=False)
Intializes the mouse object.

Arguments:
experiment → An instance of libopensesame.experiment.experiment.

Keyword arguments:
buttonlist → A list of buttons that are accepted or None to accept all input (default = None).
timeout → An integer value specifying a timeout in milliseconds or None for no timeout (default = None).
visible → A Boolean indicating the visibility of the cursor (default=False).

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)

mouse.flush()
Clears all pending input, not limited to the mouse.

Returns:
True if a button had been clicked (i.e., if there was something to flush) and False otherwise.

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
my_mouse.flush()
button, position, timestamp = my_mouse.get_click()

mouse.get_click(buttonlist=None, timeout=None, visible=None)
Waits for mouse input.

Keyword arguments:
buttonlist → A list of buttons that are accepted or None to use the default. This parameter does not change the default keylist (default=None).
timeout → An integer value specifying a timeout in milliseconds or None to use the default. This parameter does not change the default timeout (default=None).
visible → A Boolean indicating the visibility of the target or None to use the default. This parameter does not change the default visibility (default=False).

Returns:
A (button, position, timestamp) tuple. The button and position are None if a timeout occurs. Position is an (x, y) tuple in screen coordinates.

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
button, position, timestamp = my_mouse.get_click()
if button == None:
	print('A timeout occurred!')

mouse.get_pos()
Returns the current location of the cursor.

Returns:
A (position, timestamp) tuple.

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
position, timestamp = my_mouse.get_pos()
x, y = position
print('The cursor was at (%d, %d)' % (x, y))

mouse.get_pressed()
Returns the current state of the mouse buttons. A True value means the button is currently being pressed.

Returns:
A (button1, button2, button3) tuple.

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
buttons = my_mouse.get_pressed()
b1, b2, b3 = buttons
print('Currently pressed mouse buttons: (%d, %d, %d)' % (b1, b2, b3))

mouse.set_buttonlist(buttonlist = None)
Sets a list of accepted buttons.

Keyword arguments:
buttonlist → A list of buttons that are accepted or None to accept all input (default=None).

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
my_mouse.set_buttonlist( [1,2] )

mouse.set_pos(pos=(0,0))
Sets the mouse position.

Keyword arguments:
pos → A (x,y) tuple for the new mouse coordinates (default = (0,0))

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
my_mouse.set_pos(pos=(0,0))

mouse.set_timeout(timeout=None)
Sets a timeout.

Keyword arguments:
timeout → An integer value specifying a timeout in milliseconds or None for no timeout (default=None).

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
my_mouse.set_timeout(2000)

mouse.set_visible(visible=True)
Sets the visibility of the cursor.

Keyword arguments:
visible → A Boolean indicating the visibility of the cursor (default=True).

Example:
from openexp.mouse import mouse
my_mouse = mouse(exp)
my_mouse.set_visible()