Inconsistent/Random Kb Errors?

Hi everyone,
I’m a research assistant, and I’m fairly new to coding, so I apologize if this question has a more straightforward answer that I am not understanding.
The problem is primarily one of inconsistency. I have a code that works, but it only works 80-90% of the time; there is no difference in the computer, the inputs I give it, or the monitor that it is attached to, it can work one moment and the next give me the error. Typically, closing and re-opening Matlab resolves the issue, but on one occasion I have found it nessacary to restart the computer itself.

Here is the error I am getting:

Error using KbQueueCreate Keyboard queue for device NaN already in use by GetChar() et al. Use of GetChar/CharAvail/FlushEvents and keyboard queues is mutually exclusive!
Error in MIST_True_Beta>MIST_MAST_runs (line 755) KbQueueCreate();
Error in MIST_True_Beta (line 164) MIST_MAST_runs(sn,run,calib,dataFile);

What is odd about this to me, at least, is that if the error is to be taken as always true, the program should never run, yet it does most of the time! I understand I am using incompatible methods, however I am working on editing a 1700 line script, and resolving every instance to be the same is an incredibly duanting task. Furthermore, I tried to comment out instances of"GetChar/CharAvail/FlushEvents " and I still got the same error. I can provide specific code segments if that would be helpful, however it seems like the error is more general about using incompatible methods.

I am using a windows computer, a X1 Carbon ThinkPad, and I am coding in MatLab R2023b. Please let me know what other information would be helpful to include.
Thanks in advance!

ListenChar will also reserve the queue for exclusive use. ListenChar(0); after another ListenChar, CharAvail, FlushEvents, GetChar will release it. If one just wants to suppress keyboard output to the Matlab console, one can use ListenChar(-1), which should suppress output, but not block the keyboard queue.

The whole behavior of these legacy functions which only exist for backwards compatibility with old code is highly dependent on operating system, Matlab vs. Octave, gui vs. non-gui, Matlab/Octave/OS version, that one simply should not mix them with keyboard queues. Stick to the old or use the new. ListenChar(-1) is the only exception atm. to suppress output.

1 Like

Hi, thanks so much for getting back to me, this is a great direction to start figuring out my issue! I am a bit confused about how this would be implemented, will I need to back through my code and replace each instance of the Kb-related methods with these new listenChar ones? I apologize again for my unfamiliarity, I’m editing existing code and I have very little coding background. Thanks again!

No. The KbQueue functions are the new ones, the other ones are the old legacy ones. You either use KbQueue functions + ListenChar(-1) or ListenChar(0) to suppress or enable output of key presses into the Matlab window. Or you use ListenChar, CharAvail, GetChar, FlushEvents as the old legacy functions. If you mix old and new, great care has to be taken to prevent conflicts.