home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / VISBUILD / CALCULAT / CPPOV13 / IMYKYBRD.CPP < prev    next >
Text File  |  1995-04-17  |  5KB  |  109 lines

  1. //****************************************************************************
  2. // ICaptureMyKeys Class - C++ Header File (imykybrd.hpp)                                                *
  3. //                                                                                                                              *
  4. // COPYRIGHT: Copyright (C) International Business Machines Corp., 1994,1995                      *
  5. //                                                                                                                              *
  6. // DISCLAIMER OF WARRANTIES:                                                                                     *
  7. //   The following [enclosed] code is sample code created by IBM                                       *
  8. //   Corporation.  This sample code is not part of any standard IBM product                          *
  9. //   and is provided to you solely for the purpose of assisting you in the                              *
  10. //   development of your applications.  The code is provided "AS IS",                                   *
  11. //   without warranty of any kind.  IBM shall not be liable for any damages                          *
  12. //   arising out of your use of the sample code, even if they have been                               *
  13. //   advised of the possibility of such damages.                                                                  *
  14. //****************************************************************************
  15. //NOTE: WE RECOMMEND USING A FIXED-SPACE FONT TO LOOK AT THE SOURCE.
  16. //
  17.  
  18. #include <os2.h>
  19. #include <istring.hpp>
  20. #include <ikeyevt.hpp>   // IKeyboardEvent
  21. #include <ikeyhdr.hpp>   // IKeyboardHandler
  22.  
  23. #ifndef _ITRACE_
  24. #include <itrace.hpp>
  25. #endif
  26.  
  27. #ifndef _IPUSHBUT_
  28. #include <ipushbut.hpp>
  29. #endif
  30.  
  31. #include "ikeypad.hpp"    // IKeypad class header
  32. #include "imykybrd.hpp"   // ICaptureMyKeys class header
  33.  
  34. //***************************************************************************
  35. // Override the characterKeyPress of the IKeyboardHandler class to
  36. // capture the + - * / = keys and pass the rest of the keys on
  37. // to be processed by someone else.
  38. //***************************************************************************
  39.  
  40. ICaptureMyKeys :: ICaptureMyKeys(IPushButton & ipluspb, IPushButton & isubpb,
  41.                                  IPushButton & imultpb, IPushButton & idivpb, 
  42.                                  IPushButton & iequalpb,IKeypad & ikeypad) :
  43.                                  PlusPB(ipluspb), MinusPB(isubpb), 
  44.                                  MultiplicationPB(imultpb), DivisionPB(idivpb), 
  45.                                  EqualPB(iequalpb),Keypad(ikeypad)
  46.  
  47. {}                                    // constructor
  48.  
  49. ICaptureMyKeys :: ~ICaptureMyKeys()   // destructor
  50. {}
  51.  
  52. Boolean  ICaptureMyKeys::characterKeyPress(IKeyboardEvent& keyevt)
  53. {
  54.   // valid keys are: 0-9,+,-,*,/,=
  55.   Boolean bRc = true;         // assume non-numeric key to start with
  56.   IString strChar = keyevt.mixedCharacter(); // assign key event to a local IString
  57.   IFUNCTRACE_DEVELOP();
  58.   ITRACE_DEVELOP("captured key: "+ IString(keyevt.character()));
  59.   if (strChar.length() == 1)       // single character ?
  60.     {
  61.     switch (keyevt.character())
  62.       {
  63.         case '0':
  64.         case '1':
  65.         case '2':
  66.         case '3':
  67.         case '4':
  68.         case '5':
  69.         case '6':
  70.         case '7':
  71.         case '8':
  72.         case '9': /* let cases for 0-9 fall through here */
  73.                   Keypad.setDigit(keyevt.character()); // set the digit in the keypad
  74.                                                        // this kicks off the notifications
  75.                                                        // and the rest of the calculator
  76.                                                        // code takes over the processing
  77.                                                        // of this keyevent
  78.                   keyevt.setResult(true);              // mark key as not processed
  79.                   ITRACE_DEVELOP("captured key: "+ IString(keyevt.character()));
  80.                   break;
  81.         case '+': keyevt.setResult(true);       /* mark key as processed */
  82.                   PlusPB.click();
  83.                   ITRACE_DEVELOP("captured key: "+ IString(keyevt.character()));
  84.                   break;
  85.         case '-': keyevt.setResult(true);     /*mark key as processed */
  86.                   MinusPB.click();
  87.                   ITRACE_DEVELOP("captured key: "+ IString(keyevt.character()));
  88.                   break;
  89.         case '*': keyevt.setResult(true);
  90.                   MultiplicationPB.click();
  91.                   ITRACE_DEVELOP("captured key: "+  IString(keyevt.character()));
  92.                   break;
  93.         case '/': keyevt.setResult(true);
  94.                   DivisionPB.click();
  95.                   break;
  96.         case '=': keyevt.setResult(true);
  97.                   EqualPB.click();
  98.                   ITRACE_DEVELOP("captured key: "+ IString(keyevt.character()));
  99.                   break;
  100.         default:  DosBeep(100, 100);        // inform the user and eat the key
  101.                   keyevt.setResult(false);   // pass event on
  102.                   ITRACE_DEVELOP("default=captured key: "+IString(keyevt.character()));
  103.                   break;
  104.       }
  105.     }
  106.     return bRc;
  107. }
  108.  
  109.