Repository: psychopy/psychopy

Issue:
MacOS: Errors with each Keyboard Backend in v2022.1.1
With PsychoPy v2022.1.1, I can't even get a very simple dummy paradigm (with only a single text component and a single keyboard component) to run without error. The same dummy paradigm runs fine in PsychoPy v2021.2.3 (which I have temporarily restored from a Time Machine backup on the same machine).

The issue(s) seem to be related to the new "Input -> Keyboard Backend" setting in v2022.1.1:

With its default setting "ioHub", I get a Python crash report (see [Crash Report with ioHub.txt](https://github.com/psychopy/psychopy/files/8140336/Crash.Report.with.ioHub.txt)) and the following traceback messages in PsychoPy:
```
Traceback (most recent call last):
ioHub Server Process Completed With Code:  Negsignal.SIGSEGV
  File "/Users/bjoern/Desktop/2022-02-25_PsychoPy_minimal_Test/CrashTest_lastrun.py", line 87, in <module>
    ioServer = io.launchHubServer(window=win, **ioConfig)
  File "/Applications/-science/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/contrib/lazy_import.py", line 118, in __call__
    return obj(*args, **kwargs)
  File "/Applications/-science/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/iohub/client/connect.py", line 256, in launchHubServer
    return ioHubConnection(iohub_config)
  File "/Applications/-science/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/iohub/client/__init__.py", line 296, in __init__
    raise RuntimeError('Error starting ioHub server: {}'.format(self.iohub_status))
RuntimeError: Error starting ioHub server: ioHub startup failed.
```

If I change "Input -> Keyboard Backend" to  "Pyglet", I no longer get the crash report and instead get the following traceback:
```
Traceback (most recent call last):
  File "/Users/bjoern/Desktop/2022-02-25_PsychoPy_minimal_Test/CrashTest_lastrun.py", line 161, in <module>
    if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
  File "/Applications/-science/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/hardware/keyboard.py", line 323, in getKeys
    name = event.getKeys(keyList, modifiers=False, timeStamped=False)
NameError: name 'event' is not defined
```

If I change "Input -> Keyboard Backend" to  "PsychToolbox", I also don't get a crash report and instead get the following traceback:
```
Traceback (most recent call last):
  File "/Users/bjoern/Desktop/2022-02-25_PsychoPy_minimal_Test/CrashTest_lastrun.py", line 204, in <module>
    if eyetracker:
NameError: name 'eyetracker' is not defined
```

Additional details:
- Big Sur 11.6.4 (20G417)
- MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
- connected to 4k display, external keyboard, and mouse

Please let me know if I can provide any additional useful information.


Discussion:
Do you have any eyetracking components in your experiment? ioHub handles both keyboard/mouse input and eyetracker input, so the code for setting up ioHub (which also creates the variable `eyetracker`) is written only if you're using the ioHub backend for keyboard/mouse OR there are parameters changed in the Eyetracking tab in Experiment Settings.
No, there was no eyetracking component in that dummy paradigm and "Experiment Settings -> Eyetracking -> Eyetracker Device" was set to "None".

Here are the minimal steps that allow me to reproduce the issue with "PsychToolbox" as the keyboard backend:
1. "View -> Open Builder View" (to create a new untitled.psyexp)
2. Change "Input -> Keyboard Backend" to "PsychToolbox" in Experiment Settings
3. Add a keyboard component
4. Run the experiment (and save it when prompted)
5. Press 'Return' in the experiment info window
6. Press 'Space'

Then you get:
```
  File "/Users/bjoern/Desktop/2022-03-10_PsychoPyTests/untitled_lastrun.py", line 185, in <module>
    if eyetracker:
NameError: name 'eyetracker' is not defined
```

Looking at "untitled_lastrun.py", the issue seems to be that the following two lines of code are always added near the end of the file:
```
if eyetracker:
    eyetracker.setConnectionState(False)
```
but `eyetracker = None` is only added to the file when the keyboard backend is set to "ioHub".
Ah, I see the problem - the code to end the experiment assumes the existance of `eyetracker` even if the backend isn't `ioHub` and there's no eyetracking used. Should be fixable, will get a fix in for the next bug fix release (2022.1.2)

Resolving patch:
diff --git a/psychopy/experiment/components/settings/__init__.py b/psychopy/experiment/components/settings/__init__.py
--- a/psychopy/experiment/components/settings/__init__.py
+++ b/psychopy/experiment/components/settings/__init__.py
@@ -1253,7 +1253,7 @@ def writeIohubCode(self, buff):
                 buff.writeIndentedLines(code % inits)
         else:
             code = (
-                "ioSession = ioServer = None"
+                "ioSession = ioServer = eyetracker = None"
             )
             buff.writeIndentedLines(code % inits)
 


Test patch: