MATLAB + Mac Keyboard Recognition

MacOS Sonoma 14.0 (23A344) + MATLAB R2023b Update 1 (23.2.0.2380103) 64-bit (maci64)

My PI and I are designing an experiment on a new desktop Mac that takes over the whole screen. When it gets time for the participant to enter some information or press SPACE or ESC, MATLAB stops responding. I’m pretty sure it doesn’t recognize the keyboard, a common issue when running PTB scripts. We have to force quit whenever it gets stuck, so I don’t have an output to share.

We have restarted and updated MATLAB and the whole desktop. Does anyone have ideas for troubleshooting?

Thanks!

Yes, this is due to persistent macOS bugs with privacy and security settings. You MUST explicitly add MATLAB to Privacy & Security - I add it to Accessibility, Input Monitoring and Full Disk Access just to be sure. Every update or with any problems you must remove and readd it to these positions. Recently even that is not enough and so I must always start MATLAB via my Terminal – that must ALSO be added to Privacy and Security. To start MATLAB from terminal use matlab &. To test this has worked before your experiment you can use this snippet:

WaitSecs(0.5); [a,b,c] = KbWait; KbName(b)

This should wait until you press a key then echo that keyname back, if it does then your keyboard is working and your experiment should work.

2 Likes

Just a tiny and almost irrelevant note, but. Just running KbName without any input arguments waits one second and then calls KbCheck in a tight loop until a keypress is detected, reporting the key pressed. so you only have to invoke that.

2 Likes

What Ian and Dee said.

As a general comment, macOS 14 Sonoma is not yet officially supported, and won’t be for a while. I couldn’t even run or test it if I wanted, as Apple decided that my 2017 Intel MBP is not worthy of operating system upgrades anymore. But also, given the poor quality of macOS upgrades and the miserable quality control at Apple, the sane thing to do in any case is to wait for the better part of a year before upgrading to a new major macOS release.

And Apple Silicon Macs right now are almost certainly unfit for any data collection with any software right now, if visual stimulation with precise timing or other low level control is required, so I hope for you that your “new desktop Mac” is only used for designing the experiment, not for running it, if timing matters.

1 Like

Thank you so much for your thoughts. As far as timing goes, this is what we’re hoping the experiment would look like: We would enter the participant number and hit ENTER. The participant would hear an audio sample and see a set of images. The computer will then begin recording the participant’s audio response to the images. The participant will hit a button to stop the recording when finished. This will be a loop of about 20 trials. Overall, I don’t think that timing precision would be a huge issue, but we would love to have the keyboard communicate.

Thank you so much for your tips. I added MATLAB and Terminal to Accessibility, Input Monitoring, and Full Disk Access. I restarted the whole system.

However, when I opened Terminal, I was unable to interact with it by typing; it did not respond to any keystroke unless it was a keyboard shortcut. I wonder if I don’t have administrator privileges, if the keyboard just doesn’t like to communicate with certain systems, or I just don’t know how to use Terminal.

I instead used CMD + Shift + N and entered matlab & into the New Command window. I clicked Run and the output was this:
[Command not found: matlab]
[Could not create a new process and open a pseudo-tty.]

When I ticked the box and clicked Run, this was the output:
[Command not found: /bin/false]
[Could not create a new process and open a pseudo-tty.]

I also repeated these methods using the file name “MATLAB_R2023b” with the ampersand, box unticked. The outputs were similar:

[Command not found: MATLAB_R2023b]
[Could not create a new process and open a pseudo-tty.]
and
[Command not found: /bin/false]
[Could not create a new process and open a pseudo-tty.]

I tested whether MATLAB recognizes keystrokes using the snippet you shared:

It works after 1 second, just as @dcnieho suggested might happen. Does that mean that MATLAB does recognize my keyboard? Any additional thoughts from anyone? Thanks in advance.

I didn’t know that, very cool, thanks Dee!!!

There is something seriously wrong with your system if you can’t type text in Terminal. Check if you have some other software installed that takes over the keyboard (perhaps your school is using some sort of restricted computing management system, or you have malware or something). Try to create a new user account and check there.

You mean you get the name back of the key you typed? If so then it is working, no need for Terminal, though the fact Terminal doesn’t work should still be resolved…

Regarding timing, the point is you want to run your experiment in the most reliable way possible. I totally love my Macbook Air M2 overall (16hr battery life, faster and smoother than my Dell workstation for many tasks), but I would never use it for PTB data collection (experimental data is priceless, I do not want to risk artifacts). You could use a DAQ to record a photodiode, microphone and PTB trigger signals so that you have an independent time confirmation, then you have a groundtruth…

1 Like

What Ian says, with the comment that if errors in your visual presentation timing, response collection etc. on the order of 50 - 100 milliseconds don’t matter, then probably even the Apple Silicon machine would do. With the disclaimer that I have never seen a Apple Silicon machine in the flesh, so those are just my untested working assumptions about the current state of things, based on 2nd hand reports on the forum.

-mario