Keyintr.cpp
上传用户:junwei_58
上传日期:2007-05-15
资源大小:79k
文件大小:3k
源码类别:

其他智力游戏

开发平台:

Visual C++

  1. #ifndef KEYINTR_CPP
  2. #define KEYINTR_CPP
  3. //#pragma inline
  4. #include <stdio.h>
  5. #include <dos.h>
  6. #include <conio.h>
  7. #include "keyintr.h"
  8. void installKeyFunc(){
  9. inGame=0;
  10. oldkey=getvect(INTR_KEY);
  11. setvect(INTR_KEY,NewKey);
  12. keyboard.status=READY;
  13. keytable[0]=0L;
  14. keytable[1]=0L;
  15. keytable[2]=0L;
  16. keytable[3]=0L;
  17. }
  18. void uninstallKeyFunc(){setvect(INTR_KEY,oldkey);}
  19. void interrupt NewKey(__CPPFLAG){
  20. // char theKey;
  21. if(inGame&KEYNEW){
  22. //inGame^=KEYNEW;
  23. //if(keyboard.status!=READY)goto ENDi;
  24. asm{
  25. sti //;if i do not open interrupt it runs better.
  26.  // read the key in ah and al
  27. in al,0x60
  28. mov ah,al
  29. push ax
  30. in   al,0x61
  31. mov  ah,al
  32. or   al,0x80
  33. out  0x61,al
  34. xchg ah,al
  35. out  0x61,al
  36. pop  ax
  37. cmp byte ptr keyboard.status,READY
  38. jnz END
  39. and al,0x7f //mov theKey,al
  40. // if(!keytable[theKey>>5]&(1<<(theKey&0x0f))){
  41. mov bl,al
  42. shr bl,3
  43. xor bh,bh  //calculate index of keytable
  44. mov cl,al
  45. and cl,0x07
  46. test ah,0x80;jnz short KEY_RELEASED
  47. mov ah,1
  48. shl ah,cl
  49.  // push down
  50. test keytable[bx],ah;jnz short END
  51. or  keytable[bx],ah //keytable[theKey>>4]|=1<<(theKey&0x0f);
  52. mov byte ptr keyboard.status,PRESS //keyboard.status=PRESS;
  53. mov byte ptr keyboard.recentPressKey,al //keyboard.recentKey=theKey;
  54. jmp short END
  55. }KEY_RELEASED:
  56. asm{
  57. mov ah,1
  58. shl ah,cl
  59. //released
  60. test keytable[bx],ah;jz short END
  61. not ah
  62. and keytable[bx],ah //keytable[theKey>>4]|=1<<(theKey&0x0f);
  63. mov byte ptr keyboard.status,RELEASE //keyboard.status=RELEASE;
  64. mov byte ptr keyboard.recentReleaseKey,al //keyboard.recentKey=theKey;
  65. }
  66. END:asm{
  67. cli //;i have not start the interrupt (sti),so it isn't needed
  68. mov al,0x20
  69. out 0x20,al
  70. }
  71. //inGame^=KEYNEW;
  72. }else oldkey();
  73. }
  74. int getch_Key(){
  75. uchar key=1;
  76. while(key){
  77. while(keyboard.status==READY);
  78. key=keyboard.recentPressKey;
  79. keyboard.status=READY;
  80. while(keyboard.status==READY);
  81. key-=keyboard.recentReleaseKey;
  82. keyboard.status=READY;
  83. }
  84. return key;
  85. }
  86. int wait_Key(){
  87. if(keyboard.status==READY)return 1;
  88. else {
  89. keyboard.status=READY;
  90. while(keyboard.status==READY);
  91. keyboard.status=READY;
  92. return 0;
  93. }
  94. }
  95. void keyLock(){
  96. keyboard.savestatus=keyboard.status;
  97. keyboard.status=WAIT;
  98. }
  99. void keyUnlock(){
  100. keyboard.status=keyboard.savestatus;
  101. }
  102. void inputString(char *str,int x,int y,int length,STRMode mode){
  103. int i=0;
  104. char a[2];
  105. a[1]='';
  106. inGame=NONE;
  107. while((a[0]=getch())!=0x0d){
  108. if(i&&a[0]=='b'){
  109. textxy(x+i*9,y," ");
  110. i--;
  111. }
  112. if(i<length){
  113. if(a[0]>='a'&&a[0]<='z'&&(mode&SMLCHARA))goto ADD;
  114. if(a[0]>='A'&&a[0]<='Z'&&(mode&CAPCHARA))goto ADD;
  115. if(a[0]>='0'&&a[0]<='9'&&(mode&NUMBER))goto ADD;
  116. if(a[0]>=' '&&a[0]<='/'&&(mode&NORMSYMBLES))goto ADD;
  117. if(a[0]>=':'&&a[0]<='@'&&(mode&SPECSYMBLES))goto ADD;
  118. if((a[0]>='['&&a[0]<=''')||(a[0]>='{'&&a[0]<='~')&&(mode&SPECSYMBLES))goto ADD;
  119. continue;
  120.  ADD:
  121. str[i++]=a[0];
  122. textxy(x+i*9,y,a);
  123. }
  124. }
  125. str[i]=a[1];
  126. inGame=KEYNEW;
  127. }
  128. #endif