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)
| 1293 | self._onkeyrelease(fun, key) |
| 1294 | |
| 1295 | def onkeypress(self, fun, key=None): |
| 1296 | """Bind fun to key-press event of key if key is given, |
| 1297 | or to any key-press-event if no key is given. |
| 1298 | |
| 1299 | Arguments: |
| 1300 | fun -- a function with no arguments |
| 1301 | key -- a string: key (e.g. "a") or key-symbol (e.g. "space") |
| 1302 | |
| 1303 | In order to be able to register key-events, TurtleScreen |
| 1304 | must have focus. (See method listen.) |
| 1305 | |
| 1306 | Example (for a TurtleScreen instance named screen |
| 1307 | and a Turtle instance named turtle): |
| 1308 | |
| 1309 | >>> def f(): |
| 1310 | ... fd(50) |
| 1311 | ... lt(60) |
| 1312 | ... |
| 1313 | >>> screen.onkeypress(f, "Up") |
| 1314 | >>> screen.listen() |
| 1315 | |
| 1316 | Subsequently the turtle can be moved by repeatedly pressing |
| 1317 | the up-arrow key, or by keeping pressed the up-arrow key. |
| 1318 | consequently drawing a hexagon. |
| 1319 | """ |
| 1320 | if fun is None: |
| 1321 | if key in self._keys: |
| 1322 | self._keys.remove(key) |
| 1323 | elif key is not None and key not in self._keys: |
| 1324 | self._keys.append(key) |
| 1325 | self._onkeypress(fun, key) |
| 1326 | |
| 1327 | def listen(self, xdummy=None, ydummy=None): |
| 1328 | """Set focus on TurtleScreen (in order to collect key-events) |
no test coverage detected