home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / ranish / SOURCES.ZIP / KEYCODES.C < prev    next >
C/C++ Source or Header  |  1998-01-24  |  3KB  |  125 lines

  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. #include "conio.h"
  5.  
  6. void key_codes(void);
  7.  
  8. void main(void)
  9. {
  10.  struct event ev;
  11.  
  12.  conio_init();
  13.  cursor_size(16,14);
  14.  show_mouse();
  15.  
  16.  write_string(  Green, 10, ScreenHeight, "Use arrows to move window.  Press ESC twice to exit." );
  17.  
  18.  key_codes();
  19.  
  20.  hide_mouse();
  21.  cursor_size(14,16);
  22. }/* main */
  23.  
  24.  
  25.  
  26. void key_codes(void)
  27. {
  28.  char *WindowBuff, *event, temp1[40], temp2[40];
  29.  int last_key=0;
  30.  int flag=1;
  31.  static int x=20, y=6, w=25, h=9,  px, py;
  32.  int Attr1=Yellow+BakRed;
  33.  int Attr2=BrCyan+BakRed;
  34.  struct event ev;
  35.  
  36.  if( (WindowBuff=(char*)malloc(w*h*2))==0 ) return;
  37.  
  38.  get_event(&ev,EV_NONBLOCK);
  39.  
  40.  while(1)
  41.     {
  42.      if( flag==1 )
  43.        {
  44.         px=x;
  45.         py=y;
  46.         save_window( x,y,w,h, WindowBuff );
  47.         clear_window(  Attr1, x,y,w,h );
  48.         border_window(  Attr1, x,y,w,h, Border22f );
  49.         flag=0;
  50.        }
  51.  
  52.      event="NONE";
  53.      if( ev.ev_type & EV_TIMER ) event="(EV_TIMER)";
  54.      if( ev.ev_type & EV_MOUSE ) event="(EV_MOUSE)";
  55.      if( ev.ev_type & EV_SHIFT ) event="(EV_SHIFT)";
  56.      if( ev.ev_type & EV_KEY ) 
  57.        {
  58.         event="(EV_KEY)  ";
  59.  
  60.         if( ev.key!=0 )  sprintf(temp1,"Ascii code: 0x%02X ('%c')",ev.key,ev.key);
  61.                  else    sprintf(temp1,"Ascii code: 0x%02X      ",ev.key);
  62.                          sprintf(temp2,"Scan  code: 0x%04X    ",ev.scan);
  63.        }
  64.      else
  65.        {
  66.         sprintf(temp1,"Ascii code: undefined ");
  67.         sprintf(temp2,"Scan  code: undefined ");
  68.        }
  69.  
  70.      write_string(Attr2,x+2,y+2,temp1);
  71.      write_string(Attr2,x+2,y+3,temp2);
  72.  
  73.      sprintf(temp1,"Event: %2d %10s", ev.ev_type,event);
  74.      write_string(Attr2,x+2,y+1,temp1);
  75.      
  76.      sprintf(temp1,"Shifts status:   %04X",ev.shift); write_string(Attr2,x+2,y+4,temp1);
  77.      sprintf(temp1,"Shifts changed:  %04X",ev.shiftX); write_string(Attr2,x+2,y+5,temp1);
  78.  
  79.   if( MouseInstalled )
  80.     {
  81.      sprintf(temp1,"Mouse X: %2d",ev.x); write_string(Attr2,x+2,y+6,temp1);
  82.      sprintf(temp1,"Mouse Y: %2d",ev.y); write_string(Attr2,x+2,y+7,temp1);
  83.      sprintf(temp1,"Left : %d",ev.left); write_string(Attr2,x+15,y+6,temp1);
  84.      sprintf(temp1,"Right: %d",ev.right); write_string(Attr2,x+15,y+7,temp1);
  85.     }
  86.   else
  87.     {
  88.      write_string(Attr2,x+2,y+7,"Mouse is not detected");
  89.     }
  90.  
  91.      ev.timer=0x100;
  92.      
  93.      get_event( &ev, EV_KEY | EV_SHIFT | EV_MOUSE | EV_TIMER );
  94.  
  95.      if( ev.ev_type & EV_KEY )
  96.        {
  97.         if( ev.key==0x1B && last_key==0x1B ) break;
  98.  
  99.         last_key=ev.key;
  100.  
  101.         switch(ev.scan&0xFF00)
  102.             {
  103.              case 0x4800: if( y>1 ) y--;           /* UP */
  104.                                   break;
  105.              case 0x4B00: if( x>1 ) x--;           /* LEFT */
  106.                                   break;
  107.              case 0x4D00: if( x+w-1 < ScreenWidth ) x++;    /* RIGHT */
  108.                                   break;
  109.              case 0x5000: if( y+h-1 < ScreenHeight ) y++;    /* DOWN */
  110.                                    break;
  111.              default    : break;
  112.             }
  113.        }/* EV_KEY */
  114.        
  115.      if( x!=px || y!=py )
  116.        {
  117.         load_window( px,py,w,h, WindowBuff );
  118.         flag=1;
  119.        }
  120.     }/* while(1) */
  121.  
  122.  load_window( x,y,w,h, WindowBuff );
  123.  free(WindowBuff);
  124. }/* key_codes */
  125.