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, TurtleSc
(self, fun, key=None)
| 1422 | self._onkeyrelease(fun, key) |
| 1423 | |
| 1424 | def onkeypress(self, fun, key=None): |
| 1425 | """Bind fun to key-press event of key if key is given, |
| 1426 | or to any key-press-event if no key is given. |
| 1427 | |
| 1428 | Arguments: |
| 1429 | fun -- a function with no arguments |
| 1430 | key -- a string: key (e.g. "a") or key-symbol (e.g. "space") |
| 1431 | |
| 1432 | In order to be able to register key-events, TurtleScreen |
| 1433 | must have focus. (See method listen.) |
| 1434 | |
| 1435 | Example (for a TurtleScreen instance named screen |
| 1436 | and a Turtle instance named turtle): |
| 1437 | |
| 1438 | >>> def f(): |
| 1439 | ... fd(50) |
| 1440 | ... lt(60) |
| 1441 | ... |
| 1442 | >>> screen.onkeypress(f, "Up") |
| 1443 | >>> screen.listen() |
| 1444 | |
| 1445 | Subsequently the turtle can be moved by repeatedly pressing |
| 1446 | the up-arrow key, or by keeping pressed the up-arrow key. |
| 1447 | consequently drawing a hexagon. |
| 1448 | """ |
| 1449 | if fun is None: |
| 1450 | if key in self._keys: |
| 1451 | self._keys.remove(key) |
| 1452 | elif key is not None and key not in self._keys: |
| 1453 | self._keys.append(key) |
| 1454 | self._onkeypress(fun, key) |
| 1455 | |
| 1456 | def listen(self, xdummy=None, ydummy=None): |
| 1457 | """Set focus on TurtleScreen (in order to collect key-events) |
no test coverage detected