home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ISC366.ZIP / DOSFREE / EXAMPLES / KBD.CPP < prev    next >
C/C++ Source or Header  |  1993-09-01  |  3KB  |  73 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //                                                                       //
  3. //            File: Kbd.cpp                                              //
  4. //            started on: 28/7/93                                        //
  5. //                                                                       //
  6. ///////////////////////////////////////////////////////////////////////////
  7. //                                                                       //
  8. //  KBD allows you to hook a hotkey. Just pass the constructor your      //
  9. //  hotkey specifications and the hotkey function will be called         //
  10. //  whenever that hotkey is pressed.                                     //
  11. //                                                                       //
  12. ///////////////////////////////////////////////////////////////////////////
  13. //                                                                       //
  14. //                    by Ofer Laor (AKA LeucroTTA)                       //
  15. //                                                                       //
  16. ///////////////////////////////////////////////////////////////////////////
  17.  
  18. #include "kbd.h"; // KBD.
  19. #include <dos.h>; // inportb, outportb, MK_FP.
  20.  
  21. // constructor remembers hotkey params.
  22. KBD::KBD(unsigned char scan_code, unsigned char controls, int kill_key)
  23.  
  24. {
  25.     // remember hotkey params.
  26.     bScanCode= scan_code;
  27.     bControls= controls;
  28.     fKillKey= kill_key;
  29.  
  30.     // set shift state pointer to it's bios location.
  31.     pShift= (unsigned char far *)MK_FP(0, 0x417);
  32.  
  33.     // hook into keyboard.
  34.     activate(0x9);
  35. }
  36.  
  37. // keyboard interrupt routine. Check for hotkey. Call hotkey func. Swallow
  38. // key if fKillKey is active.
  39. void KBD::isr()
  40. {
  41.     unsigned char scan= inportb(0x60);   // get key.
  42.     unsigned char ctrl= *pShift& 0xf;    // ignore high states (insert state, etc.).
  43.  
  44.     // except for Shifts (which are ORed), control states are ANDed.
  45.  
  46.     // scan code && shift state.
  47.     if ((bControls== (bControls& ctrl)) ||
  48.        (((bControls& KBS_SHIFT)== KBS_SHIFT) && (ctrl& KBS_SHIFT)))
  49.        if (scan== bScanCode) {
  50.  
  51.           if (kill_key()) {
  52.  
  53.              // swallow key.
  54.  
  55.              unsigned char old_state= inportb(0x61);
  56.              outportb(0x61, old_state& 0x80);
  57.              outportb(0x61, old_state);
  58.  
  59.              // eoi.
  60.              outportb (0x20, 0x20);
  61.              hotkey();
  62.  
  63.              return;
  64.           }
  65.  
  66.           else
  67.             hotkey(); // run hotkey without swallowing the key.
  68.     }
  69.  
  70.     // hook back interrupt.
  71.     old_vect();
  72. }
  73.