function KbDemo %% KbDemo % Shows how to detect when the user has pressed a key. % See KbCheck, KbName, KbWait, GetChar, CharAvail. % % The KbXXX functions are low-level and go after the state of the keyboard. % The GetChar/CharAvail interface pulls characters out of the event queue. % The advantage of the KbXXX functions is that if you can sit and wait on % characters in a tight loop, you'll get less of a lag. But if the key % goes up and down in between when your code checks the keyboard state, you'll % miss it. % % The advantage of GetChar/CharAvail is that they may be used % asychronously - the OS will pick up the character whether your code % is looking for it when it comes. % % This demo works fine the with Keyspan Digital Media Remote: % http://www.keyspan.com/products/usb/remote/ % However, the Digital Media Remote is sluggish (max alternating key rate of 2 Hz), % suggesting that it may not be well suited for measuring reaction time. % % Normally, characters typed creep through and appear in the command window or % file being edited, depending on which has focus. You can prevent Matlab % from receiving typed characters by calling ListenChar(2); at the % beginning of you script and calling ListenChar(0); at the end of your % script. Caution: Make sure to always call ListenChar(0) at normal exit and in all % error handling routines, otherwise Matlab will be left with a dead % keyboard and you'll be unable to type any commands into the Matlab % window! Pressing CTRL+C will reenable the keyboard in such a case. % David Brainard, January, 1997 % Allen Ingling, February, 1998 % 01/22/97 dhb Wrote demos 2 & 3, as KeyTimingDemo. % 03/06/97 dhb Change from TIMER to modern routines. % 03/08/97 dgp Polished the instructions. % 08/02/97 dgp Call FlushEvents('keyDown') after getting each % key press, to start fresh each time. % Explain difference between key presses and characters. % 01/28/98 dgp Explain effect of CapsLock key. % 02/28/98 dgp Mention new capability of KbCheck. % 02/18/98 awi Wrote demos 1 & 4. % 02/19/98 dgp Folded in awi's KbExplore. % 02/28/98 dgp Folded in dhb's KeyTimingDemo. % 03/02/98 dgp Cosmetic. % 03/09/98 dhb Change case of screen to Screen. % Force 8-bit depth. DefaultClut. % 03/10/98 dgp Allow all depths. Use new WhiteIndex and BlackIndex. % 03/12/98 dgp Make sure Backgrounding is off. % 03/19/99 dgp Use KbName. We no longer use GetChar, so Backgrounding no longer matters. % 06/06/00 dgp Use arrow and escape keys instead of a, s and q to allow use with % the Keyspan Digital Media Remote, which has no letter keys. % http://www.keyspan.com/products/usb/remote/ % 02/11/01 awi Added platform conditionals for font names. % Conditionally call flushevents only on OS9. % 04/03/02 awi Restored flushevents under Windows % 04/13/02 dgp Use Arial, eliminate conditionals. % 09/23/06 rhh Ported part four of the demo to Mac OS X. % 10/11/06 dhb Imported subfunctions into single file, use cell syntax. % 10/11/06 dhb Added comment comparing to GetChar/CharAvail. % 10/16/06 mk Add call 'UnifyKeyNames' -> Same keynames on all systems. % 10/17/06 mk Remove calls to FlushEvents. They are meaningless for KbWait and KbCheck, % would only affect CharAvail and GetChar. % 06/11/16 mk Make sure to not crash on multiple key press. % Enable unified mode of KbName, so KbName accepts identical key names on % all operating systems: KbName('UnifyKeyNames'); if IsWayland % We need a onscreen window to collect key input on Wayland: w = Screen('OpenWindow', 0, [255 255 0], [0 0 300 300]); DrawFormattedText(w, 'Type in this window', 'center', 'center', [0 0 255]); Screen('Flip', w); end %% Runs 4 demos in sequence % Report keyboard key number for any pressed keys, including % modifier keys such as , , and