Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parallel Keyboardinput 1

Status
Not open for further replies.

luckSpray

Programmer
Sep 12, 2004
3
0
0
AT
I'm trying to program an input system where several keys at once can be pressed and the program takes notice of each key without conflicts.

I wanted to try the getkey() function but my borland compiler spits out warnings about that.

It would be very nice if somebody tells my how i get the getkey function running or even tells me how to realise my input system.

Mode: Dos Console
Compiler: Borland C++ 5.5 free
 
What do you mean parralel kb input? If it is simply character input without waiting, then kbhit() and getch():

Code:
#include <conio.h>
#include <stdio.h>

int main()
{
unsigned char ch;

   ch=0;

   printf("Press Esc to stop\n");

   while(ch!=27)
   {
      if(kbhit())
      {
         ch=getch();
         printf("%x\n", ch);
      }
   }

return(0);
}

But if, like Salem supposes, you want manage key pressing and releasing - hooking of kb interrupt vector and managing of keyboard scan codes is necessary. It is dangerous, but - I'm surprised - it works under Windows:

Code:
/*-----------------------------*/
#include <stddef.h>
#include <stdio.h>
#include <dos.h>


/*-----------------------------*/
#define KB_VCT      0x24     /* kb interrupt vector */
#define KB_PORT     0x60     /* kb port */
#define KB_INIT     0x80     /* init kb command */
#define INT_PORT    0x20     /* interrupt controller port */
#define INT_ENABLE  0x20     /* interrupt enable command */
#define KB_BUF_SIZE 100


/*-----------------------------*/
/* for saving of pointer to original interrupt vector routine */
void interrupt (*KbSav)()=NULL;

/* kb buffer */
unsigned char volatile KbBuf[KB_BUF_SIZE];

/* pointers to kb buffer */
unsigned char volatile *TopPtr;
const unsigned char volatile *BotPtr;


/*-----------------------------*/
void interrupt KbInt(void)
{
unsigned char ch;
unsigned char cc;
unsigned char *buf_ptr;

   buf_ptr=(unsigned char *)TopPtr;

/* get scan code */
   ch=inportb(KB_PORT);

/* put it to buffer */
   *(buf_ptr++)=ch;

/* manage buffer pointers */
   if(buf_ptr>=KbBuf+KB_BUF_SIZE) buf_ptr=(unsigned char *)KbBuf;

/* buffer not yet full? */
   if(buf_ptr!=BotPtr) TopPtr=buf_ptr;
   else
   {
/* BIOS usually beeps here */
   }

/* release kb port */
   cc=inportb(KB_PORT+1);
   outportb(KB_PORT+1, cc|KB_INIT);
   outportb(KB_PORT+1, cc);

/* enable next interrupts */
   outportb(INT_PORT, INT_ENABLE);
}


/*-----------------------------*/
int main()
{
unsigned char ch;

   printf("Test of keyboard scan-codes, Esc to exit\n");

/* init kb buffer pointers */
   TopPtr=KbBuf;
   BotPtr=KbBuf;

/* store original kb vector, set new */
   KbSav=getvect(KB_VCT/4);
   setvect(KB_VCT/4, KbInt);

   do
   {
/* wait for character ready */
      while(TopPtr==BotPtr);

/* get character from buffer, manage pointers */
      ch=(*BotPtr++);
      if(BotPtr>=KbBuf+KB_BUF_SIZE) BotPtr=KbBuf;

      printf("%x\n", ch);

   } while(ch!=0x81);

/* restore original keyboard vector */
   setvect(KB_VCT/4, KbSav);

return(0);
}
 
gee thanks mingis!

i've one question. have you compiled it?
my compilers (borland, ms) are spitting errors and to be honest this code is te "high" for me to modify it... i'm a rookie..
 
borland errors:

Code:
>bcc32 -v- -w -O1 help.c
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
help.c:
Error E2293 help.c 18: ) expected
Error E2449 help.c 29: Size of 'interrupt' is unknown or zero
Error E2141 help.c 29: Declaration syntax error
*** 4 errors in Compile ***
>Exit code: 1
 
Sorry for late responce.
I have it compiled - using ancient Borland C++ Version 3.1 of year 1992 or something, one of the latest Borland things from "Turbo" series.
It seems, that your version of compiler does not recognize keyword "interrupt" - of copurse, 32-bit compilers do not dig in such 16-bit DOS things. Don't know, what to advice - look what Borland says. May be look for real DOS-like 16-bit compiler.

> this code is te "high" for me to modify

It is not so difficult. Read something about calling differences between usual and interrupt routines in some Intel Assembler manuals. Here is nothing to modify - without interrupt style of calling/returning there is no possibility to hook interrupts. Or may be is - to insert additional assembler instructions POP/POPF or IRET at the end of the function - it needs further investigation, usig debugger in disassembling mode.
 
If it's a win32 console environment then you need to use the functions discussed in the URL I originally posted.

All this "interrupt" stuff in OLD DOS compilers is not going to get you anywhere with a new compiler for a new OS.


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top