delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/06/01:03:36

From: "0/0" <nsousbpoanmer AT naooslp DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: keyboard question
Date: 6 Jun 1999 03:48:52 GMT
Organization: Value Net Internetwork Services Inc.
Lines: 110
Message-ID: <01beafcf$4d264f80$fc4484ce@sub>
References: <VXj63.24$Et6 DOT 13292 AT newsgate DOT direct DOT ca>
NNTP-Posting-Host: fgna-252.value.net
X-Newsreader: Microsoft Internet News 4.70.1162
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Daniel Eaton <deaton AT direct DOT ca> wrote in article
<VXj63.24$Et6 DOT 13292 AT newsgate DOT direct DOT ca>...
> I'm writing a side scrolling game and am wondering how to prevent the
> beeping when the user holds down a movement key? How do you dequeue all
the
> waiting keys (I'm assuming that the beeping is coming from something like
a
> keyboard buffer overload).
> Thanks,
> Daniel Eaton

Your problem is that you need to create your own keyboard interrupt
handler, since I'm feeling in a good mood right now, I think I'll just post
my old keyboard code here...

#include <pc.h>
#include <dos.h>
#include <go32.h>
#include <dpmi.h>

byte LastKeyPressed[3]={0,0,0};
byte LastKeyReleased[1]={0};
volatile byte key[129]={0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0};

static _go32_dpmi_seginfo dosmem; /* DOS (conventional) memory buffer */

static _go32_dpmi_seginfo oldkbisr_rm;    /* original real mode IRQ */
static _go32_dpmi_registers rm_regs;
static _go32_dpmi_seginfo rm_si;        /* real mode interrupt segment info
*/

static _go32_dpmi_seginfo oldkbisr_pm;    /* original prot-mode IRQ */
static _go32_dpmi_seginfo pm_si;        /* prot-mode interrupt segment info
*/

int kbisr(void){
    byte rawkey,temp;

    rawkey=(byte)inportb(0x60);
    temp=(byte)inportb(0x61)|0x82;
    outportb(0x61,temp);
    outportb(0x61,temp&0x7f);
    outportb(0x20,0x20);
    if(rawkey<128){
        key[rawkey]=1;
        LastKeyPressed[2]=LastKeyPressed[1];
        LastKeyPressed[1]=LastKeyPressed[0];
        LastKeyPressed[0]=(byte)rawkey;
        key[0]++;
    }else{
        key[rawkey-128]=0;
        LastKeyReleased[0]=(byte)(rawkey-128);
        key[0]--;
    }
    enable();
    return 0;
}

void StartKeyboard(){
    int ret;

    rm_si.pm_offset=(int)kbisr;
    ret=_go32_dpmi_allocate_real_mode_callback_iret(&rm_si,&rm_regs);
    disable();
    // Install the real mode interrupt handler
    _go32_dpmi_get_real_mode_interrupt_vector(0x9,&oldkbisr_rm);
    _go32_dpmi_set_real_mode_interrupt_vector(0x9,&rm_si);
    // Install the protected mode interrupt handler
    _go32_dpmi_get_protected_mode_interrupt_vector(0x9,&oldkbisr_pm);
    pm_si.pm_offset=(int)kbisr;
    pm_si.pm_selector=_go32_my_cs();
    _go32_dpmi_chain_protected_mode_interrupt_vector(0x9,&pm_si);
    enable();
}

void StopKeyboard(){
    disable();
    // Restore original real mode interrupt handler
    _go32_dpmi_set_real_mode_interrupt_vector(0x9,&oldkbisr_rm);
    _go32_dpmi_free_real_mode_callback(&rm_si);
    // Restore original protected mode interrupt handler
    _go32_dpmi_set_protected_mode_interrupt_vector(0x9,&oldkbisr_pm);
    enable();
}

To use it you have to call the function StartKeyboard(), when your done
call StopKeyboard()... Now any time (between those 2 function calls) you
want to check to see if a key is pressed, you check the appropriate
scancode location in the key buffer, so if you wanted to see if the left
arrow key is pressed you simply check to see if 75 (0x4b) has a 1in it or
if it is not pressed it has a 0 in it...

if(key[75]==1){
    //do your `left arrow pressed' code here
}

If you have any questions ask...

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019