Cam wrote:
[snip]
> I need a non-platform specific function that will accept a single keypress
> and return the character code for the key pressed to allow the placement if
> individual characters into an array.
[snip]
>
> An alternative would be REALLY appreciated.
>
> Thanks to all,
>
> Cheers,
>
> Cam
>
>
Sorry, but there is no standard function for scanning a keyboard
to determine when a key has been pressed or to get the code of
the key that was pressed without buffering.
The problem is that not all platforms are required to have
keyboards. Of those that do have a keyboard, the operating
system will buffer the input so that a user can modify the
input before sending it to a program. This was to spare the
applications from having to process "input editing keystrokes".
Some operating systems allow users to change these settings.
However, you can have your program call a generic function,
such as "Get_Keypress". Inside this function will be a
platform specific call. Create one version for each platform.
Each version will be in a separate file (translation unit).
Have your linking phase include the appropriate file for
the platform you want. This is called creating a "wrapper".
It allows the rest of your program to use one function call
instead having to change the platform specific call everywhere
in your code.
An alternative, is to use preprocessor conditional statements
to isolate the function:
#if PLATFORM_MSDOS
#include <conio.h>
int Get_Keypress(void)
{
return getche();
}
#elseif PLATFORM_UNIX
//...
//...
#else
#include <exceptions>
int Get_Keypress(void)
{
throw std::runtime_error("Platform does not support keypress.");
}
#endif.
So to compile for MS-DOS platform, you would define PLATFORM_MSDOS
in the compiler's command line, such as "-DPLATFORM_MSDOS"
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book