Bind fun to key-press event of key if key is given, or to any key-press-event if no key is given. Arguments: fun -- a function with no arguments key -- a string: key (e.g. "a") or key-symbol (e.g. "space") In order to be able to register key-events, T
(self, fun, key=None)
| 1395 | self._onkeyrelease(fun, key) |
| 1396 | |
| 1397 | def onkeypress(self, fun, key=None): |
| 1398 | """Bind fun to key-press event of key if key is given, |
| 1399 | or to any key-press-event if no key is given. |
| 1400 | |
| 1401 | Arguments: |
| 1402 | fun -- a function with no arguments |
| 1403 | key -- a string: key (e.g. "a") or key-symbol (e.g. "space") |
| 1404 | |
| 1405 | In order to be able to register key-events, TurtleScreen |
| 1406 | must have focus. (See method listen.) |
| 1407 | |
| 1408 | Example (for a TurtleScreen instance named screen |
| 1409 | and a Turtle instance named turtle): |
| 1410 | |
| 1411 | >>> def f(): |
| 1412 | ... fd(50) |
| 1413 | ... lt(60) |
| 1414 | ... |
| 1415 | >>> screen.onkeypress(f, "Up") |
| 1416 | >>> screen.listen() |
| 1417 | |
| 1418 | Subsequently the turtle can be moved by repeatedly pressing |
| 1419 | the up-arrow key, or by keeping pressed the up-arrow key. |
| 1420 | consequently drawing a hexagon. |
| 1421 | """ |
| 1422 | if fun is None: |
| 1423 | if key in self._keys: |
| 1424 | self._keys.remove(key) |
| 1425 | elif key is not None and key not in self._keys: |
| 1426 | self._keys.append(key) |
| 1427 | self._onkeypress(fun, key) |
| 1428 | |
| 1429 | def listen(self, xdummy=None, ydummy=None): |
| 1430 | """Set focus on TurtleScreen (in order to collect key-events) |
no test coverage detected