[comp.os.msdos.programmer] Reading extended keys

Bartholomew_Siemens@mindlink.bc.ca (Bartholomew Siemens) (06/14/91)

In msg. <RN.1320@mts.ucs.UAlberta.CA>
userRPCP@mts.ucs.UAlberta.CA (Eugene Mah)
writes:

> I wonder if anyone out there can help me.  I'm using Borland Turbo C++ v1.01
> and I need to be able to detect when an extended key is pressed ie. F1-F10,
> Shift F1-F10, etc.
> 
> Can anyone out there tell me how to do this?  It's easy enough in Borland's
> Turbo Pascal.  Thanks a bunch in advance.

It's also quite easy in any of the Borland C products -- whether that be Turbo
C 2.0 or Borland C++ 2.0. Check out BIOSKEY in your manual. In MSC5.0 check out
_BIOS_KEYBRD. If you're doing it with some other compiler with MS-DOS use a DOS
interrupt function. I've not worked in any other environment, so I have no idea
about anything else.


bart (not simpson)
--
-----------------------------------------------------------------------
Disclaimer: navahoe -- I am exactly who I am and I speak that!
rsoft!mindlink!Bartholomew_Siemens@van-bc.UUCP
-----------------------------------------------------------------------

userRPCP@mts.ucs.UAlberta.CA (Junior Physicist) (06/14/91)

I wonder if anyone out there can help me.  I'm using Borland
Turbo C++ v1.01 and I need to be able to detect when an 
extended key is pressed ie. F1-F10, Shift F1-F10, etc.
 
Can anyone out there tell me how to do this?  It's easy enough
in Borland's Turbo Pascal.  Thanks a bunch in advance. 
 
Replies can be e-mailed to me.  I don't want to take up
a lot of space on the net.
Eugene Mah, Junior Physicist
 
INTERNET: userRPCP@mts.ucs.ualberta.ca
BITNET: userRPCP@ualtamts

KQS@psuvm.psu.edu (Kevin Sullivan) (06/15/91)

I took this out of the C language groups, since it is not appropriate there...

The code returned when a key is pressed has to be in the range 0-255.
However, there are more than 255 different key-combinations.  To get
around this, IBM uses a special code to flag special keys.  Whenever
a special key (F1, for example) is pressed, two numbers are put into
the keyboard buffer.  The first is a 0, the second is a number representing
the key that was pressed.

Therefore, if getchar() returns a '\0', you know that:
    1) A special key has been pressed and
    2) You must call getchar() again to find out which special key was
       pressed.

I don't have a list of what the various key codes are with me, but it is
trivial to write a program to show the codes and write them down.

     Kevin Sullivan                       BITNET: KQS@PSUVM
     Student Consultant - CAC           INTERNET: kqs@vm.psu.edu
     Penn State University
                                        Old programmers don't die...they
I don't speak for Penn State...         just branch to a new address.
Penn State doesn't speak for me.

s9100202@giaea.gi.oz (Lee Hollingworth) (06/19/91)

In article <RN.1320@mts.ucs.UAlberta.CA> userRPCP@mts.ucs.UAlberta.CA (Junior Physicist) writes:
>I wonder if anyone out there can help me.  I'm using Borland
>Turbo C++ v1.01 and I need to be able to detect when an
>extended key is pressed ie. F1-F10, Shift F1-F10, etc.
>
>Can anyone out there tell me how to do this?  It's easy enough
>in Borland's Turbo Pascal.  Thanks a bunch in advance.
>
>Replies can be e-mailed to me.

I tried but they bounced...

I don't use TC++ or BC++, so I'm not sure if these compilers come with the
functions getch() & getche().  Which are functions which allow you to get a
single character without, or with echo, without having to press the return
or enter key. (Function getchar() requires the user to press enter after the
entered key).

These functions are available with Microsoft C, prototyped in <conio.h>.

I have included an assembly listing, as they are extremely easy to write,
just incase your compiler(s) do not have the functions available.  Of course
you need to assemble the files and then link them with your C source.

To test a function key, or indeed any extended key code, you need to make
one call to see if the value returned by getch() or getche() is 0, if it is
then an extended scan code has been entered, and you must call the function
again to see what the actual scan code is.

A sample C listing is shown below, making use of the assembly code listing
which follows.


 (Start of C source code)
/* ------------------------------------------------------------------------- */

#include <stdio.h>

extern int getch(void);        /* in file getc.asm (assembly code) */
extern int getche(void);       /* in file getc.asm (assembly code) */


void main()
{
    int key;

    printf("Press any key: ");
    key = getch();

    if(key == 0){
        key = getch();
        printf("\nExtended key scan code is %d\n", key);
    }
    else
        printf("\nYou pressed the %c key\n", key);
}
/* -----------------------------------------------------------------------*/
 (End of C source code)

(Start of assembly code)
;---------------------------------------------------------------------------

;---------------------------------------------------------------------------;
; getc.asm                                                                  ;
; Note: All functions are written to make use of the Microsoft C calling    ;
;       convention which does not require scratch pad registers to be       ;
;       saved or restored.                                                  ;
;---------------------------------------------------------------------------;
      .MODEL     small
      .CODE

      PUBLIC    _getch
;---------------------------------------------------------------------------;
; This function gets a single character input without echo                  ;
; returns in ax                                                             ;
;---------------------------------------------------------------------------;
_getch         PROC
      mov      ah,8
      int      21h
      xor      ah,ah
      ret
_getch         ENDP

      PUBLIC   _getche
;---------------------------------------------------------------------------;
; This function gets a single character input with echo                     ;
; returns in ax                                                             ;
;---------------------------------------------------------------------------;
_getche        PROC
      mov      ah,1
      int      21h
      xor      ah,ah
      ret
_getche        ENDP

               END

;-----------------------------------------------------------------------------
(end of source code)

Hope that is of help to you.

Lee Hollingworth  s9100202@giae.oz.au