| 1 |
Mouse input |
| 2 |
=========== |
| 3 |
|
| 4 |
All pyglet windows can receive input from a 3 button mouse with a |
| 5 |
2 dimensional scroll wheel. The mouse pointer is typically drawn by the |
| 6 |
operating system, but you can override this and request either a different |
| 7 |
cursor shape or provide your own image or animation. |
| 8 |
|
| 9 |
.. _guide_mouse_events: |
| 10 |
|
| 11 |
Mouse events |
| 12 |
------------ |
| 13 |
|
| 14 |
All mouse events are dispatched by the window which receives the event from |
| 15 |
the operating system. Typically this is the window over which the mouse |
| 16 |
cursor is, however mouse exclusivity and drag operations mean this is not |
| 17 |
always the case. |
| 18 |
|
| 19 |
The coordinate space for the mouse pointer's location is relative to the |
| 20 |
bottom-left corner of the window, with increasing Y values approaching the top |
| 21 |
of the screen (note that this is "upside-down" compared with many other |
| 22 |
windowing toolkits, but is consistent with the default OpenGL projection in |
| 23 |
pyglet). |
| 24 |
|
| 25 |
.. figure:: img/mouse_coordinates.png |
| 26 |
|
| 27 |
The coordinate space for the mouse pointer. |
| 28 |
|
| 29 |
The most basic mouse event is :py:meth:`~pyglet.window.Window.on_mouse_motion` |
| 30 |
which is dispatched every time the mouse moves:: |
| 31 |
|
| 32 |
def on_mouse_motion(x, y, dx, dy): |
| 33 |
pass |
| 34 |
|
| 35 |
The `x` and `y` parameters give the coordinates of the mouse pointer, relative |
| 36 |
to the bottom-left corner of the window. |
| 37 |
|
| 38 |
The event is dispatched every time the operating system registers a mouse |
| 39 |
movement. This is not necessarily once for every pixel moved -- the operating |
| 40 |
system typically samples the mouse at a fixed frequency, and it is easy to |
| 41 |
move the mouse faster than this. Conversely, if your application is not |
| 42 |
processing events fast enough you may find that several queued-up mouse events |
| 43 |
are dispatched in a single :py:meth:`~pyglet.window.Window.dispatch_events` |
| 44 |
call. There is no need to concern yourself with either of these issues; |
| 45 |
the latter rarely causes problems, and the former can not be avoided. |
| 46 |
|
| 47 |
Many games are not concerned with the actual position of the mouse cursor, |
| 48 |
and only need to know in which direction the mouse has moved. For example, |
| 49 |
the mouse in a first-person game typically controls the direction the player |
| 50 |
looks, but the mouse pointer itself is not displayed. |
| 51 |
|
| 52 |
The `dx` and `dy` parameters are for this purpose: they give the distance the |
| 53 |
mouse travelled along each axis to get to its present position. This can be |
| 54 |
computed naively by storing the previous `x` and `y` parameters after every |
| 55 |
mouse event, but besides being tiresome to code, it does not take into account |
| 56 |
the effects of other obscuring windows. It is best to use the `dx` and `dy` |
| 57 |
parameters instead. |
| 58 |
|
| 59 |
The following events are dispatched by the Window when a mouse button is |
| 60 |
pressed or released, or the mouse is moved while any button is held down:: |
| 61 |
|
| 62 |
def on_mouse_press(x, y, button, modifiers): |
| 63 |
pass |
| 64 |
|
| 65 |
def on_mouse_release(x, y, button, modifiers): |
| 66 |
pass |
| 67 |
|
| 68 |
def on_mouse_drag(x, y, dx, dy, buttons, modifiers): |
| 69 |
pass |
| 70 |
|
| 71 |
The `x`, `y`, `dx` and `dy` parameters are as for the |
| 72 |
:py:meth:`~pyglet.window.Window.on_mouse_motion` event. |
| 73 |
The press and release events do not require `dx` and `dy` parameters as they |
| 74 |
would be zero in this case. The `modifiers` parameter is as for the keyboard |
| 75 |
events, see :ref:`guide_keyboard-input`. |
| 76 |
|
| 77 |
The `button` parameter signifies which mouse button was pressed, and is one of |
| 78 |
the following constants:: |
| 79 |
|
| 80 |
pyglet.window.mouse.LEFT |
| 81 |
pyglet.window.mouse.MIDDLE |
| 82 |
pyglet.window.mouse.RIGHT |
| 83 |
|
| 84 |
The `buttons` parameter in :py:meth:`~pyglet.window.Window.on_mouse_drag` |
| 85 |
is a bitwise combination of all the mouse buttons currently held down. |
| 86 |
For example, to test if the user is performing a drag gesture with the |
| 87 |
left button:: |
| 88 |
|
| 89 |
from pyglet.window import mouse |
| 90 |
|
| 91 |
def on_mouse_drag(x, y, dx, dy, buttons, modifiers): |
| 92 |
if buttons & mouse.LEFT: |
| 93 |
pass |
| 94 |
|
| 95 |
When the user begins a drag operation (i.e., pressing and holding a mouse |
| 96 |
button and then moving the mouse), the window in which they began the drag |
| 97 |
will continue to receive the :py:meth:`~pyglet.window.Window.on_mouse_drag` |
| 98 |
event as long as the button is held down. |
| 99 |
This is true even if the mouse leaves the window. |
| 100 |
You generally do not need to handle this specially: it is a convention |
| 101 |
among all operating systems that dragging is a gesture rather than a direct |
| 102 |
manipulation of the user interface widget. |
| 103 |
|
| 104 |
There are events for when the mouse enters or leaves a window:: |
| 105 |
|
| 106 |
def on_mouse_enter(x, y): |
| 107 |
pass |
| 108 |
|
| 109 |
def on_mouse_leave(x, y): |
| 110 |
pass |
| 111 |
|
| 112 |
The coordinates for :py:meth:`~pyglet.window.Window.on_mouse_leave` will |
| 113 |
lie outside of your window. These events are not dispatched while a drag |
| 114 |
operation is taking place. |
| 115 |
|
| 116 |
The mouse scroll wheel generates the |
| 117 |
:py:meth:`~pyglet.window.Window.on_mouse_scroll` event:: |
| 118 |
|
| 119 |
def on_mouse_scroll(x, y, scroll_x, scroll_y): |
| 120 |
pass |
| 121 |
|
| 122 |
The `scroll_y` parameter gives the number of "clicks" the wheel moved, with |
| 123 |
positive numbers indicating the wheel was pushed forward. The `scroll_x` |
| 124 |
parameter is 0 for most mice, however some new mice such as the Apple Mighty |
| 125 |
Mouse use a ball instead of a wheel; the `scroll_x` parameter gives the |
| 126 |
horizontal movement in this case. The scale of these numbers is not known; it |
| 127 |
is typically set by the user in their operating system preferences. |
| 128 |
|
| 129 |
Changing the mouse cursor |
| 130 |
------------------------- |
| 131 |
|
| 132 |
The mouse cursor can be set to one of the operating system cursors, a custom |
| 133 |
image, or hidden completely. The change to the cursor will be applicable only |
| 134 |
to the window you make the change to. To hide the mouse cursor, call |
| 135 |
:py:meth:`~pyglet.window.Window.set_mouse_visible`:: |
| 136 |
|
| 137 |
win = pyglet.window.Window() |
| 138 |
win.set_mouse_visible(False) |
| 139 |
|
| 140 |
This can be useful if the mouse would obscure text that the user is typing. |
| 141 |
If you are hiding the mouse cursor for use in a game environment, consider |
| 142 |
making the mouse exclusive instead; see :ref:`guide_mouse-exclusivity`, below. |
| 143 |
|
| 144 |
Use :py:meth:`~pyglet.window.Window.set_mouse_cursor` to change the appearance |
| 145 |
of the mouse cursor. A mouse cursor is an instance of |
| 146 |
:py:class:`~pyglet.window.MouseCursor`. You can obtain the operating |
| 147 |
system-defined cursors with |
| 148 |
:py:meth:`~pyglet.window.Window.get_system_mouse_cursor`:: |
| 149 |
|
| 150 |
cursor = win.get_system_mouse_cursor(win.CURSOR_HELP) |
| 151 |
win.set_mouse_cursor(cursor) |
| 152 |
|
| 153 |
The cursors that pyglet defines are listed below, along with their typical |
| 154 |
appearance on Windows and Mac OS X. The pointer image on Linux is dependent |
| 155 |
on the window manager. |
| 156 |
|
| 157 |
.. list-table:: |
| 158 |
:header-rows: 1 |
| 159 |
:stub-columns: 1 |
| 160 |
:class: images |
| 161 |
|
| 162 |
* - Constant |
| 163 |
- Windows XP |
| 164 |
- Mac OS X |
| 165 |
* - `CURSOR_DEFAULT` |
| 166 |
- .. image:: img/cursor_win_default.png |
| 167 |
- .. image:: img/cursor_mac_default.png |
| 168 |
* - `CURSOR_CROSSHAIR` |
| 169 |
- .. image:: img/cursor_win_crosshair.png |
| 170 |
- .. image:: img/cursor_mac_crosshair.png |
| 171 |
* - `CURSOR_HAND` |
| 172 |
- .. image:: img/cursor_win_hand.png |
| 173 |
- .. image:: img/cursor_mac_hand.png |
| 174 |
* - `CURSOR_HELP` |
| 175 |
- .. image:: img/cursor_win_help.png |
| 176 |
- .. image:: img/cursor_mac_default.png |
| 177 |
* - `CURSOR_NO` |
| 178 |
- .. image:: img/cursor_win_no.png |
| 179 |
- .. image:: img/cursor_mac_no.png |
| 180 |
* - `CURSOR_SIZE` |
| 181 |
- .. image:: img/cursor_win_size.png |
| 182 |
- .. image:: img/cursor_mac_default.png |
| 183 |
* - `CURSOR_SIZE_DOWN` |
| 184 |
- .. image:: img/cursor_win_size_up_down.png |
| 185 |
- .. image:: img/cursor_mac_size_down.png |
| 186 |
* - `CURSOR_SIZE_DOWN_LEFT` |
| 187 |
- .. image:: img/cursor_win_size_nesw.png |
| 188 |
- .. image:: img/cursor_mac_default.png |
| 189 |
* - `CURSOR_SIZE_DOWN_RIGHT` |
| 190 |
- .. image:: img/cursor_win_size_nwse.png |
| 191 |
- .. image:: img/cursor_mac_default.png |
| 192 |
* - `CURSOR_SIZE_LEFT` |
| 193 |
- .. image:: img/cursor_win_size_left_right.png |
| 194 |
- .. image:: img/cursor_mac_size_left.png |
| 195 |
* - `CURSOR_SIZE_LEFT_RIGHT` |
| 196 |
- .. image:: img/cursor_win_size_left_right.png |
| 197 |
- .. image:: img/cursor_mac_size_left_right.png |
| 198 |
* - `CURSOR_SIZE_RIGHT` |
| 199 |
- .. image:: img/cursor_win_size_left_right.png |
| 200 |
- .. image:: img/cursor_mac_size_right.png |
| 201 |
* - `CURSOR_SIZE_UP` |
| 202 |
- .. image:: img/cursor_win_size_up_down.png |
| 203 |
- .. image:: img/cursor_mac_size_up.png |
| 204 |
* - `CURSOR_SIZE_UP_DOWN` |
| 205 |
- .. image:: img/cursor_win_size_up_down.png |
| 206 |
- .. image:: img/cursor_mac_size_up_down.png |
| 207 |
* - `CURSOR_SIZE_UP_LEFT` |
| 208 |
- .. image:: img/cursor_win_size_nwse.png |
| 209 |
- .. image:: img/cursor_mac_default.png |
| 210 |
* - `CURSOR_SIZE_UP_RIGHT` |
| 211 |
- .. image:: img/cursor_win_size_nesw.png |
| 212 |
- .. image:: img/cursor_mac_default.png |
| 213 |
* - `CURSOR_TEXT` |
| 214 |
- .. image:: img/cursor_win_text.png |
| 215 |
- .. image:: img/cursor_mac_text.png |
| 216 |
* - `CURSOR_WAIT` |
| 217 |
- .. image:: img/cursor_win_wait.png |
| 218 |
- .. image:: img/cursor_mac_wait.png |
| 219 |
* - `CURSOR_WAIT_ARROW` |
| 220 |
- .. image:: img/cursor_win_wait_arrow.png |
| 221 |
- .. image:: img/cursor_mac_default.png |
| 222 |
|
| 223 |
Alternatively, you can use your own image as the mouse cursor. Use |
| 224 |
:py:func:`pyglet.image.load` to load the image, then create an |
| 225 |
:py:class:`~pyglet.window.ImageMouseCursor` with |
| 226 |
the image and "hot-spot" of the cursor. The hot-spot is the point of the |
| 227 |
image that corresponds to the actual pointer location on screen, for example, |
| 228 |
the point of the arrow:: |
| 229 |
|
| 230 |
image = pyglet.image.load('cursor.png') |
| 231 |
cursor = pyglet.window.ImageMouseCursor(image, 16, 8) |
| 232 |
win.set_mouse_cursor(cursor) |
| 233 |
|
| 234 |
You can even render a mouse cursor directly with OpenGL. You could draw a |
| 235 |
3-dimensional cursor, or a particle trail, for example. To do this, subclass |
| 236 |
:py:class:`~pyglet.window.MouseCursor` and implement your own draw method. |
| 237 |
The draw method will be called with the default pyglet window projection, |
| 238 |
even if you are using another projection in the rest of your application. |
| 239 |
|
| 240 |
.. _guide_mouse-exclusivity: |
| 241 |
|
| 242 |
Mouse exclusivity |
| 243 |
----------------- |
| 244 |
|
| 245 |
It is possible to take complete control of the mouse for your own application, |
| 246 |
preventing it being used to activate other applications. This is most useful |
| 247 |
for immersive games such as first-person shooters. |
| 248 |
|
| 249 |
When you enable mouse-exclusive mode, the mouse cursor is no longer available. |
| 250 |
It is not merely hidden -- no amount of mouse movement will make it leave your |
| 251 |
application. Because there is no longer a mouse cursor, the `x` and `y` |
| 252 |
parameters of the mouse events are meaningless; you should use only the `dx` |
| 253 |
and `dy` parameters to determine how the mouse was moved. |
| 254 |
|
| 255 |
Activate mouse exclusive mode with |
| 256 |
:py:meth:`~pyglet.window.Window.set_exclusive_mouse`:: |
| 257 |
|
| 258 |
win = pyglet.window.Window() |
| 259 |
win.set_exclusive_mouse(True) |
| 260 |
|
| 261 |
You should activate mouse exclusive mode even if your window is full-screen: |
| 262 |
it will prevent the window "hitting" the edges of the screen, and behave |
| 263 |
correctly in multi-monitor setups (a common problem with commercial |
| 264 |
full-screen games is that the mouse is only hidden, meaning it can |
| 265 |
accidentally travel onto the other monitor where applications are still |
| 266 |
visible). |
| 267 |
|
| 268 |
Note that on Linux setting exclusive mouse also disables Alt+Tab and other |
| 269 |
hotkeys for switching applications. No workaround for this has yet been |
| 270 |
discovered. |