# getChar for Win

**URL:** <https://psychtoolbox.discourse.group/t/getchar-for-win/1035>\
**Category:** Uncategorized\
**Created:** [April 20, 2001, 3:29pm UTC](https://psychtoolbox.discourse.group/t/getchar-for-win/1035 "2001-04-20T15:29:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [April 20, 2001, 3:29pm UTC](https://psychtoolbox.discourse.group/t/getchar-for-win/1035/1 "2001-04-20T15:29:15Z")

</div>

Hi,  
  
I've been looking at the PsychToolbox over the last week or so and I'm   
 very interested in using it for running my experiments. The only   
problem is that I'm using windows.  
  
I've come to the conclusion that the functions avilable are adequate   
for my experiments, assuming that GetChar works. On the usergroup   
website I noticed that 'on earlier versions of MATLAB, GetChar misses   
some key responses'. Could someone tell me exactly when it does this,   
 to what degree, and perhaps some timescale as to when it might be   
fixed please? I'm using MATLAB 5.3.  
  
Also, does anyone know of some code to be able to use a button box   
with the toolbox and windows.   
  
Thanks

---

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [April 23, 2001, 4:29pm UTC](https://psychtoolbox.discourse.group/t/getchar-for-win/1035/2 "2001-04-23T16:29:14Z")

</div>

[labott1@...](mailto:labott1@...) wrote:  
  

> \>  
> \> I've come to the conclusion that the functions avilable are adequate  
> \> for my experiments,

Recently, Wednesday and Thursday (4/18/01 & 4/19/01), I updated the list at  
[http://color.psych.ucsb.edu/psychtoolbox/winlist.html.](http://color.psych.ucsb.edu/psychtoolbox/winlist.html.) of currently working  
functions in Psychtoolbox for Windows. See that page for the latest info.  
  

> \> assuming that GetChar works. On the usergroup  
> \> website I noticed that 'on earlier versions of MATLAB, GetChar misses  
> \> some key responses'.

I would assume that the problem occurs with all versions of Matlab, however I've  
only confirmed it on 5.2 and 6.0.  
  
I think that remark which you quote is out of context and wasn't meant to imply  
that GetChar catches all keypresses when used with later versions of Matlab, but  
rather that there is an additional problem in using it with a later version,  
6.0. That problem is that 6.0 hangs when GetChar is called, but we now have a  
work around for that, see:  
[http://groups.yahoo.com/group/psychtoolbox/message/421](http://groups.yahoo.com/group/psychtoolbox/message/421)  
[http://groups.yahoo.com/group/psychtoolbox/message/422](http://groups.yahoo.com/group/psychtoolbox/message/422)  
  
  

> \> Could someone tell me exactly when it does this,  
> \> to what degree,

When executing a .m file Matlab interleaves keyboard scans between expressions in  
the .m file. Matlab therefore only steals characters from the event queue before  
or after a call to GetChar, not within the call as GetChar awaits a keypress.  
  
The upshot is:  
  
- If CharAvail reports that a keypress is available, subsequent calls to GetChar  
could fail to return the keypress because between your calls to CharAvail and  
GetChar Matlab has stolen the keypress event. The idiom:  
  
if CharAvail  
 myChar = GetChar;  
end  
  
is a convenient way of scanning for characters without waiting for them, but it  
won't be available in Windows until GetChar is fixed. It's likely that you  
could make due with KbCheck until GetChar is fixed.  
  
- In the situation where you prompt the user for an input, and then call GetChar  
to wait for a keypress, there probably won't be any trouble. The short interval  
during which Matlab steals keypresses is probably well within the reaction time  
of the subject. The bug makes  
  
fprintf('Press a key\n');  
myChar = GetChar;  
  
intermittently equivalent to:  
  
fprintf('Press a key\n');  
FlushEvents('KeyDown');  
myChar = GetChar;  
  
Conceptually that's bad; after you ask the subject to press a key, you then erase  
all record of any keypress, before looking for keypresses. However, in  
practice, there won't be trouble because the subject isn't going to press a key  
between when you ask him to do so and when you erase keypresses, unless its an  
anticipatory response, and for most purposes those can be reasonably excluded.  
  
  

> \> and perhaps some timescale as to when it might be  
> \> fixed please? I'm using MATLAB 5.3.

Because the problem arises from Matlab's behavior, and Matlab is a black box  
--closed source and distributed as a binary executable,-- it's difficult to  
predict exactly what will work to fix the problem.  
  
I'm trying alternative implementations of GetChar, one using DirectInput and one  
using hooks and talking to people at Mathworks.  
  

> \>  
> \>  
> \> Also, does anyone know of some code to be able to use a button box  
> \> with the toolbox and windows.

What code you would need depends on how your button box interfaces to your  
computer, so more detail here would be helpful.  
  
If you would like to use a game pad as a button box then I could look into  
providing Windows psychtoolbox functions to read a game pad.  
  
  
Allen
