function [ch, when] = GetChar(getExtendedData, getRawCode) % [ch, when] = GetChar([getExtendedData], [getRawCode]) % % Wait for a typed character and return it. If a character was typed % before calling GetChar then GetChar will return that character immediately. % Characters flushed by FlushEvents are all ignored by GetChar. Characters % are returned in the first return argument "ch". % % Please read the 'help ListenChar' carefully to understand various % limitations and caveats of this function, and to learn about - often % better - alternatives. Also read the "international keyboard" section in % 'help KbEventGet' for limitations, caveats, or possibly needed extra % setup in some cases when using a keyboard with a non-US layout. % % CAUTION: Do not rely on the keypress timestamps returned by GetChar % without fully reading and understanding this help text. Run your own % timing tests on GetChar and KbCheck to verify that the timing is good % enough and avoid GetChar for timed keypresses if possible at all. Use % KbWait and KbCheck instead. % % The main purpose of GetChar is to reliably collect keyboard input in the % background while your experiment script is occupied with performing other % operations, e.g., Matlab computations, sound output or visual stimulus % drawing. After an initial call to ListenChar, the operating system will % record all keyboard input into an internal queue. GetChar removes % characters from that queue, one character per invocation of GetChar. You % can empty that queue any time by calling FlushEvents('keyDown'). % % If you want to check the current state of the keyboard, e.g., for % triggering immediate actions in response to a key press, waiting for a % subjects response, synchronizing to keytriggers (e.g., fMRI machines) or % if you require high timing precision then use KbCheck instead of GetChar. % % GetChar should work on all platforms, but its specific functionality, % beyond simply returning simple typed characters, will vary depending on % OS type and version, the type of desktop GUI in use, or if you use Matlab % or Octave, and if you use Matlab with or without Java based GUI active. % For portability it is therefore best to ignore all info returned beyond % the character code. Composed characters, e.g., putting accents on top of % letters, do not always work on all setups. In general, only Matlab with % Java based desktop GUI enabled on Linux and macOS will get optimal % international keyboard and composed character handling, at reduced timing % precision, whereas anything on MS-Windows, and on Linux with KDE desktop, % and on Octave, and Matlab in -nodesktop mode, will use an implementation % based on KbQueues, which has better timing and flexibility, but worse % handling of international keyboards and composed characters at the moment. % % "when" is a struct. It (used to) return the time of the keypress, the "adb" % address of the input device, and the state of all the modifier keys % (shift, control, command, option, alphaLock) and the mouse button. % "when.secs" is an estimate, of what GetSecs would have been. Since it's % derived sometime from a timebase different from the timebase of GetSecs, % times returned by GetSecs are not directly comparable to when.secs. % % By setting getExtendedData to 0, all extended timing/modifier information % will not be collected and "when" will be returned empty. This speeds up % calls to this function. If omitted or set to 1, the "when" data % structure is filled. getRawCode set to 1 will set "ch" to be the integer % ASCII code of the available character. If omitted or set to 0, "ch" will % be in char format. When running under Linux in "matlab -nojvm" mode or on % Octave, "when" will be returned empty. When running on any other % operating system under Octave or in "matlab -nojvm" mode, or on Windows % Vista and later versions of the Windows OS, when will only contain a % valid timestamp, but all other fields will be meaningless. % % GetChar and CharAvail are character-oriented (and slow), whereas KbCheck % and KbWait are keypress-oriented (and fast). If only a meta key (like %