...keyboard codes...

This is a discussion on ...keyboard codes... within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I am having trouble getting ALT and CONTROL as well as arrow keys. I tried getch() but , as they ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670

    ...keyboard codes...

    I am having trouble getting ALT and CONTROL as well as arrow keys.

    I tried getch() but , as they say: "no workie"...


    I found a function for getting keys:

    Code:
    int GetKey() {
    union REGS regs;
    regs.h.ah = 0x00;
    int86(KEYBOARD, &regs, &regs);
    return regs.x.ax;
    }
    Problem is my lib doesn't list a function "int86()" much less the union type "REGS"....

    Any ideas?



    ITSA
    Socket Library!

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Include...

    #include<dos.h>

    most of ur problems should be solved.
    Help everyone you can

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    hmm, seems that I already did a search on the whole "includes" subfolder, but I will definately try again....

    Thanks for the advice Vsriharsha...



    ITSA
    Socket Library!

  4. #4
    Unregistered
    Guest
    Look at the c programming, this issue was frequently discussed there

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Code:
    #define KEYBDINT 0x09
    #define CTRLBREAKINT 0x23
    
    interrupt (*OldKeyHandler)(...)=0;
    char keys[128];
    
    //Installs KeyHandler on int 09h
    void InstallKeyHandler(void);
    
    //Removes/restores keyboard int 09h
    void UninstallKeyHandler(void);
    
    //Uninstalls all vectors for this process
    void UninstallVectors(void);
    
    //Installs all vectors for this process
    void InstallVectors(void);
    
    interrupt MyKeyboardHandler(...)
    {
      int key=inp(0x60);
    
      if (key>128)                            //0x80
      {
         keys[key-128]=0;
      } else keys[key]=1;
    
      //Don't call the old handler
    }
    
    interrupt CtrlBreakHandler(...)
    {
      UninstallVectors();     //Restor vector table 
      exit(0);                      //exit
      //no need to restore int 23h vector, DOS will do this 
    }
    
    void InstallKeyHandler(void)
    {
       //DOS Way to install vector
       OldKeyHandler=getvect(KEYBDINT);
       setvect(KEYBDINT,MyKeyboardHandler);
    }
    
    void UninstallKeyHandler(void)
    {
      setvect(KEYBDINT,OldKeyHandler);
    }
    
    void InstallBreakHandler(void)
    {
      setvect(CTRLBREAKINT,BreakHandler);
    }
    
    void UninstallVectors(void)
    {
      UninstallKeyHandler();
    
      //Any other vectors that need to be restored go here
    }
    
    void InstallVectors(void)
    {
      InstallBreakHandler();
      InstallKeyHandler();
    
      //any other handlers needing to be installed go here
    }

    Note that you do not have to explicitly restore the CTRL BREAK handler or the handler for INT 23h. DOS will do this for you. The main reason for the functions that install and uninstall the vectors is so that you can install all vectors and uninstall all vectors with one call. This is very handy for debugging because when you press CTRL BREAK, the vector table will be restored to its original state and you will be able to continue. You should also put in an atexit() function so that when exit(returncode) is issued, it will call atexit() which will call UninstallVectors. This will ensure that you will be able to write interrupt handlers, test them, and the system will remain stable.

    To use this code:

    if keys[scancode]
    {
    ..key is pressed
    } else ..key is not pressed

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    this would be probably not required since Bubba has already posted but i have used the bioskey() in bios.h for getting codes of keys

    int key;
    cout<<"PRESS A KEY..";
    key=bioskey(0); //you press enter
    cout<<key; //7181 is the output

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    unless bubba had already addressed it...

    a practical way to handle input is to have seperate routines for the scan/ascii codes of 'regular' input, and one for the shift statii... [shifts alts caps scroll whatchamacallits and whatnot]... both would require a 16-bit value [short, compiler / target dependant...] put the shift status in a short, and the scan and ascii codes in one short... and you have it...

    also, while i'm at it... checking for keystrokes and returning them only if availiable can help so you don't have to wait for input [like you would if using getch...]

    does anyone know the interrupts offhand for shift status'? currently mine is just a wrapper over _bios_keybrd (_NKEYBRD_SHIFTSTATUS) ... thanks... [will check RBIL... eventually Bubba... ]
    hasafraggin shizigishin oppashigger...

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    The shift status of keys can be trapped by checking certain bits in the return code. Each bit represents whether or not left shift, right shift, CTRL, ALT, NumLock, etc., are down or not.

    I believe that the bit pattern is identical to that of the BIOS keyboard interrupt.

  9. #9
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    oh sorry if i wasn't clear, when mentioning 'shift status'' i was refering to the entire control word... as i said my current usage is just a wrapper over a library function... which is the interrupt? thanks.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 03:24 AM
  2. Keyboard Codes
    By joeprogrammer in forum Game Programming
    Replies: 9
    Last Post: 02-22-2006, 06:15 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. : Difference between message queue and keyboard buffer...
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-21-2002, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21