home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / genhdrs / keybd / hexkeybd.hpp next >
Text File  |  1996-10-29  |  2KB  |  77 lines

  1. #ifndef _HEXKEYBD_
  2. #define _HEXKEYBD_
  3. //*********************************************************
  4. // Reusable Handlers - Keyboard Handler
  5. //
  6. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  7. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  8. // All Rights Reserved.
  9. //*********************************************************
  10. #include <ibase.hpp>         // For IC_PM/IC_WIN.
  11. #ifdef IC_PM
  12.   #define INCL_WINDIALOGS    // For WinAlarm.
  13.   #define INCL_WININPUT      // For KC_CHAR, VK_DELETE.
  14.   #include <os2.h>
  15. #else
  16.   #include <windows.h>
  17. #endif
  18.  
  19. #include <ikeyhdr.hpp>
  20. #include <istring.hpp>
  21. #include <iwindow.hpp>
  22.  
  23. // This keyboard handler restricts character input to
  24. // hexadecimal digits (0-9, a-f, A-F).
  25. class HexKeyboardHandler : public IKeyboardHandler {
  26. protected:
  27. virtual Boolean
  28.   characterKeyPress ( IKeyboardEvent& event )
  29.   {
  30.     Boolean badKey = true;
  31.     Boolean dontPassOn = true;
  32.  
  33.     IString strChar = event.mixedCharacter();
  34.     if ( strChar.isSBCS() )
  35.     {                        // The character is single-byte.
  36.        if ( strChar.isHexDigits() )
  37.        {                     // '0'-'9', 'A'-'F', 'a'-'f'.
  38.           badKey = false;    // Valid hexadecimal digit.
  39.           dontPassOn = false;  // Pass the event to the window.
  40.        }
  41.        else if ( strChar == " " )
  42.        {                     // Space bar is pressed.
  43.           badKey = false;    // Replace it with the Delete key.
  44. #ifdef IC_PM
  45.           IEventParameter1
  46.             param1( event.parameter1().number1() & ~KC_CHAR,
  47.                     event.parameter1().char3(), 0 );
  48.           (*event.window())
  49.            .sendEvent( IWindow::character,
  50.                        param1,
  51.                        IEventParameter2( 0, VK_DELETE ) );
  52.                // Discard scan code and character data.
  53. #else
  54.           (*event.window())
  55.            .sendEvent( IWindow::character,
  56.                        IEventParameter1( 0, 1 ),
  57.                        IEventParameter2( VK_DELETE ) );
  58.                // Discard scan code and character data.
  59. #endif
  60.        }
  61.        // Throw away any other character key.
  62.     }                        // End single-byte input.
  63.  
  64.     if ( badKey )
  65.     {
  66. #ifdef IC_PM
  67.        WinAlarm( IWindow::desktopWindow()->handle(),
  68.                  WA_WARNING );
  69. #else
  70.        Beep( 100, 100 );
  71. #endif
  72.     }
  73.     return dontPassOn;
  74.   }
  75. }; // HexKeyboardHandler
  76. #endif // _HEXKEYBD_
  77.