About Site Map Submit Contact Us Log in | Create an account
Create an account Log In
Average Rating
User Rating:
Visitors Rating:
My rating:

Write review
  • Last update: 5 years ago
  • Total downloads: 95
  • Operating system: Linux, Mac OS X, Win All, BSD, Solaris
  • Publisher: Amanda (2 other programs)
See full specifications

scripts default iconNon-wait detection of keyboard input (including MEX implementation) (Scripts) Publisher's description

These functions retrieve the last key pressed in the command window without having to explicitly wait for input

These functions retrieve the last key pressed in the command window without having to explicitly wait for input. Useful for situations where code is executing and you want to poll for keyboard input but you don't want hidden figures. For example, mexKbhit can be used to interrupt a MEX function while it's executing.

The code taps into the command window's key press callback function. Each time a key is pressed, a small piece of m code is run to save the key event. A call to kbhit (or mexKbhit in MEX code) will return information about the key event, such as the ascii code and the related character, and whether Control, Alt and/or Shift was also entered.

Warning : mexKbhit calls the matlab function kbhit. While CTRL+C doesn't interrupt MEX functions, it will interrupt matlab functions called by a MEX function. Therefore, the key combo CTRL+C may cause problems when your MEX function is executing mexKbhit.

Big acknowledgements to Yair Altman in the MATLAB newsreader thread "java: add keyListener to command window", for providing the exact code needed to set up the key press callback.

kbhit.m - matlab function
mexKbhit.h - MEX c file header
mexKBhit.c - MEX c file, including instructions for building the library
mexKbhitDemo.c - demonstration code for using the MEX library.

Example use of Matlab function :

fprintf(1, 'Five seconds to type something ...');
key = kbhit; fprintf(1, 'Character : %cn', key);
key = kbhit('struct'); fprintf(1, 'Key struct :n'); disp(key)
[key, ctrlc] = kbhit('event'); fprintf(1, 'Key event :n'); disp(key)
fprintf(1, 'Ctrl+c pressed ? %dn', ctrlc);

Example use of MEX library :

clock_t t;
struct key k;
t = clock();
// poll for 5 seconds
mexPrintf("Wait 5 seconds ... ");
while ((clock()-t)/CLOCKS_PER_SEC < 5) {
k = mexKbhit();
if (k.character == 'C' && k.ctrl == 1) {
mexPrintf("interrupted by Ctrl+c! ");

System Requirements:

MATLAB 7.7 (R2008b)
Program Release Status: New Release
Program Install Support: Install and Uninstall

Non-wait detection of keyboard input (including MEX implementation) (Scripts) Tags:

Click on a tag to find related softwares

Is Non-wait detection of keyboard input (including MEX implementation) (Scripts) your software?

Manage your software

Most Popular

scripts default icon ASK, OOK, FSK, BPSK, QPSK, 8PSK modulation 1.1
ASK, OOK, FSK, BPSK, QPSK, 8PSK modulation contain several functions for digital modulation simulation
scripts default icon Simulink Communication Labs 1.1
Simulink Communication Labs allows you to learn communication systems in greater depth.
scripts default icon M-QAM modulation and demodulation 1.1
M-QAM modulation and demodulation is the QAM modulation and demodulation tech.
scripts default icon LZW Compression/Decompression 1.1
LZW Compression/Decompression - Updated LZW compressor and decompressor with reasonable performance
scripts default icon InSPIRE utility to plot a 2D displacement field (Scripts) 1.0
This program plots the deformation field (displace vectors) contained in vector.txt.