home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / intuition / mouse_keyboard / rawkey.e < prev   
Text File  |  1977-12-31  |  6KB  |  150 lines

  1. -> rawkey.e - How to correctly convert from RAWKEY to keymapped ASCII
  2.  
  3. MODULE 'console',
  4.        'devices/inputevent',
  5.        'devices/timer',
  6.        'exec/io',
  7.        'intuition/intuition'
  8.  
  9. ENUM ERR_NONE, ERR_DEV, ERR_WIN
  10.  
  11. RAISE ERR_DEV IF OpenDevice()<>0,
  12.       ERR_WIN IF OpenWindowTagList()=NIL
  13.  
  14. -> A buffer is created for RawKeyConvert() to put its output. BUFSIZE is the
  15. -> size of the buffer in bytes.  NOTE that this program starts out with a
  16. -> buffer size of 2.  This is only to show how the buffer is automatically
  17. -> increased in size by this example!  In an application, start with a much
  18. -> larger buffer and you will probably never have to increase its size. 128
  19. -> bytes or so should do the trick, but always be able to change the size if
  20. -> required.
  21. CONST BUFSIZE=2
  22.  
  23. PROC main() HANDLE
  24.   DEF win=NIL, ioreq:iostd, ievent=NIL:PTR TO inputevent,
  25.       buffer=NIL, bufsize=BUFSIZE
  26.   -> Open the console device just to do keymapping. (unit -1 means any unit)
  27.   OpenDevice('console.device', -1, ioreq, 0)
  28.   consoledevice:=ioreq.device
  29.   -> Allocate the initial character buffer used by deadKeyConvert() and
  30.   -> RawKeyConvert() for returning translated characters.  If the characters
  31.   -> generated by these routines cannot fit into the buffer, the application
  32.   -> must pass a larger buffer.  This is done in this code by freeing the old
  33.   -> buffer and allocating a new one.
  34.   buffer:=NewR(bufsize)
  35.   NEW ievent
  36.   win:=OpenWindowTagList(NIL,
  37.               [WA_WIDTH, 300,
  38.                WA_HEIGHT, 50,
  39.                WA_FLAGS, WFLG_DEPTHGADGET OR WFLG_CLOSEGADGET OR WFLG_ACTIVATE,
  40.                WA_IDCMP, IDCMP_CLOSEWINDOW OR IDCMP_RAWKEY,
  41.                WA_TITLE, 'Raw Key Example',
  42.                NIL])
  43.   WriteF('Press keyboard keys to see ASCII conversion from rawkey\n')
  44.   WriteF('Unprintable characters will be shown as \c\n\n', $7F)
  45.   process_window(win, ievent, {buffer}, {bufsize})
  46.  
  47. EXCEPT DO
  48.   IF win THEN CloseWindow(win)
  49.   -> E-Note: don't need to free any memory -- automatically done
  50.   IF consoledevice THEN CloseDevice(ioreq)
  51.   SELECT exception
  52.   CASE ERR_DEV; WriteF('Error: Failed to open console device.\n')
  53.   CASE ERR_WIN; WriteF('Error: Failed to open window.\n')
  54.   CASE "MEM";   WriteF('Error: Ran out of memory.\n')
  55.   ENDSELECT
  56. ENDPROC
  57.  
  58. -> Convert RAWKEYs into VANILLAKEYs, also shows special keys like HELP, Cursor
  59. -> Keys, FKeys, etc.  It returns:
  60. ->   -1 if not enough room in the buffer, try again with a bigger buffer.
  61. ->   otherwise, returns the number of characters placed in the buffer.
  62. PROC deadKeyConvert(kbuffer, kbsize, kmap, ievent:PTR TO inputevent)
  63.   ievent.class:=IECLASS_RAWKEY
  64.   ievent.code:=MsgCode()
  65.   ievent.qualifier:=MsgQualifier()
  66.   ievent.eventaddress:=MsgIaddr()
  67.   RETURN RawKeyConvert(ievent, kbuffer, kbsize, kmap)
  68. ENDPROC
  69.  
  70. -> print_qualifiers() - Print out the values found in the qualifier bits of
  71. -> the message. This will print out all of the qualifier bits set.
  72. PROC print_qualifiers(qual)
  73.   WriteF('Qual: ')
  74.   IF qual AND IEQUALIFIER_LSHIFT         THEN WriteF('LShft, ')
  75.   IF qual AND IEQUALIFIER_RSHIFT         THEN WriteF('RShft, ')
  76.   IF qual AND IEQUALIFIER_CAPSLOCK       THEN WriteF('CapLok, ')
  77.   IF qual AND IEQUALIFIER_CONTROL        THEN WriteF('Ctrl, ')
  78.   IF qual AND IEQUALIFIER_LALT           THEN WriteF('LAlt, ')
  79.   IF qual AND IEQUALIFIER_RALT           THEN WriteF('RAlt, ')
  80.   IF qual AND IEQUALIFIER_LCOMMAND       THEN WriteF('LCmd, ')
  81.   IF qual AND IEQUALIFIER_RCOMMAND       THEN WriteF('RCmd, ')
  82.   IF qual AND IEQUALIFIER_NUMERICPAD     THEN WriteF('NumPad, ')
  83.   IF qual AND IEQUALIFIER_REPEAT         THEN WriteF('Rpt, ')
  84.   IF qual AND IEQUALIFIER_INTERRUPT      THEN WriteF('Intrpt, ')
  85.   IF qual AND IEQUALIFIER_MULTIBROADCAST THEN WriteF('Multi Broadcast, ')
  86.   IF qual AND IEQUALIFIER_MIDBUTTON      THEN WriteF('MidBtn, ')
  87.   IF qual AND IEQUALIFIER_RBUTTON        THEN WriteF('RBtn, ')
  88.   IF qual AND IEQUALIFIER_LEFTBUTTON     THEN WriteF('LBtn, ')
  89.   IF qual AND IEQUALIFIER_RELATIVEMOUSE  THEN WriteF('RelMouse, ')
  90. ENDPROC
  91.  
  92. -> doKeys() - Show what keys were pressed.
  93. PROC doKeys(ievent, buffer:PTR TO LONG, bufsize:PTR TO LONG) HANDLE
  94.   DEF char_pos, numchars, realc, c
  95.  
  96.   -> deadKeyConvert() returns -1 if there was not enough space in the buffer to
  97.   -> convert the string. Here, the routine increases the size of the buffer on
  98.   -> the fly... Set the return code to FALSE on failure.
  99.   numchars:=deadKeyConvert(buffer[], bufsize[]-1, NIL, ievent)
  100.   WHILE (numchars=-1) AND buffer[]
  101.   -> Conversion failed, buffer too small. Try to double the size of the buffer.
  102.     Dispose(buffer[])
  103.     bufsize[]:=bufsize[]*2
  104.     WriteF('Increasing buffer size to \d\n', bufsize[])
  105.  
  106.     buffer[]:=NewR(bufsize[])
  107.     numchars:=deadKeyConvert(buffer[], bufsize[]-1, NIL, ievent)
  108.   ENDWHILE
  109.  
  110.   -> numchars contains the number of characters placed within the buffer.  Key
  111.   -> up events and key sequences that do not generate any data for the program
  112.   -> (like deadkeys) will return zero.  Special keys (like HELP, the cursor
  113.   -> keys, FKeys, etc.) return multiple characters that have to then be parsed
  114.   -> by the application.
  115.  
  116.   -> If high bit set, then this is a key up otherwise this is a key down
  117.   IF MsgCode() AND $80
  118.     WriteF('Key Up:   ')
  119.   ELSE
  120.     WriteF('Key Down: ')
  121.   ENDIF
  122.  
  123.   print_qualifiers(MsgQualifier())
  124.   WriteF(' rawkey #\d maps to \d ASCII character\s\n',
  125.          $7F AND MsgCode(), numchars, IF numchars<>1 THEN 's' ELSE '')
  126.   FOR char_pos:=0 TO numchars-1
  127.     realc:=(c:=buffer[][char_pos])
  128.     IF (c<=$1F) OR ((c>=$80) AND (c<$A0)) THEN c:=$7F
  129.     WriteF('  \d[3] ($\z\h[2]) = \c\n', realc, realc, c)
  130.   ENDFOR
  131. EXCEPT DO
  132.   RETURN exception=ERR_NONE
  133. ENDPROC
  134.  
  135. -> process_window() - Simple event loop.  Note that the message is not replied
  136. -> to until the end of the loop so that it may be used in the doKeys() call.
  137. -> E-Note: we use WaitIMessage() so a lot of this and above is simplified
  138. PROC process_window(win, ievent, buffer, bufsize)
  139.   DEF going=TRUE, class
  140.   WHILE going
  141.     class:=WaitIMessage(win)
  142.     SELECT class
  143.     CASE IDCMP_CLOSEWINDOW
  144.       going:=FALSE
  145.     CASE IDCMP_RAWKEY
  146.       going:=doKeys(ievent, buffer, bufsize)
  147.     ENDSELECT
  148.   ENDWHILE
  149. ENDPROC
  150.