Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233109
  • 博文数量: 35
  • 博客积分: 659
  • 博客等级: 上士
  • 技术积分: 357
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 21:16
文章分类
文章存档

2012年(12)

2011年(23)

分类: C/C++

2012-06-20 14:42:00

    在使用DarkGDK编写游戏时,时常要用键盘和玩家进行交互,我们可以通过dbKeyState()函数来让玩家按下特定的键,去执行特定的代码。
    
  1. #include "DarkGDK.h"

  2. void DarkGDK()
  3. {
  4.     dbSyncOn();            //从现在开始由程序主动控制屏幕刷新
  5.     dbSyncRate(60);        //设置刷新频率为60次每秒
  6.     while(LoopGDK() && !dbKeyState(0x11)) {    //执行游戏循环,当用户按下w键时推出永久循环
  7.         ;        //空语句
  8.     }
  9. }
  dbKeyState(int KeyBorad_ID);  只要找到每个键对应的ID就可以完成键盘相应。
  每个键对应的ID可以再DirectixSDK下的include/dinput.h文件中找到。
  列出一些键盘的对应ID(其实就是一些宏)

点击(此处)折叠或打开

  1. /****************************************************************************
  2.  *
  3.  * DirectInput keyboard scan codes
  4.  *
  5.  ****************************************************************************/
  6. #define DIK_ESCAPE 0x01
  7. #define DIK_1 0x02
  8. #define DIK_2 0x03
  9. #define DIK_3 0x04
  10. #define DIK_4 0x05
  11. #define DIK_5 0x06
  12. #define DIK_6 0x07
  13. #define DIK_7 0x08
  14. #define DIK_8 0x09
  15. #define DIK_9 0x0A
  16. #define DIK_0 0x0B
  17. #define DIK_MINUS 0x0C /* - on main keyboard */
  18. #define DIK_EQUALS 0x0D
  19. #define DIK_BACK 0x0E /* backspace */
  20. #define DIK_TAB 0x0F
  21. #define DIK_Q 0x10
  22. #define DIK_W 0x11
  23. #define DIK_E 0x12
  24. #define DIK_R 0x13
  25. #define DIK_T 0x14
  26. #define DIK_Y 0x15
  27. #define DIK_U 0x16
  28. #define DIK_I 0x17
  29. #define DIK_O 0x18
  30. #define DIK_P 0x19
  31. #define DIK_LBRACKET 0x1A
  32. #define DIK_RBRACKET 0x1B
  33. #define DIK_RETURN 0x1C /* Enter on main keyboard */
  34. #define DIK_LCONTROL 0x1D
  35. #define DIK_A 0x1E
  36. #define DIK_S 0x1F
  37. #define DIK_D 0x20
  38. #define DIK_F 0x21
  39. #define DIK_G 0x22
  40. #define DIK_H 0x23
  41. #define DIK_J 0x24
  42. #define DIK_K 0x25
  43. #define DIK_L 0x26
  44. #define DIK_SEMICOLON 0x27
  45. #define DIK_APOSTROPHE 0x28
  46. #define DIK_GRAVE 0x29 /* accent grave */
  47. #define DIK_LSHIFT 0x2A
  48. #define DIK_BACKSLASH 0x2B
  49. #define DIK_Z 0x2C
  50. #define DIK_X 0x2D
  51. #define DIK_C 0x2E
  52. #define DIK_V 0x2F
  53. #define DIK_B 0x30
  54. #define DIK_N 0x31
  55. #define DIK_M 0x32
  56. #define DIK_COMMA 0x33
  57. #define DIK_PERIOD 0x34 /* . on main keyboard */
  58. #define DIK_SLASH 0x35 /* / on main keyboard */
  59. #define DIK_RSHIFT 0x36
  60. #define DIK_MULTIPLY 0x37 /* * on numeric keypad */
  61. #define DIK_LMENU 0x38 /* left Alt */
  62. #define DIK_SPACE 0x39
  63. #define DIK_CAPITAL 0x3A
  64. #define DIK_F1 0x3B
  65. #define DIK_F2 0x3C
  66. #define DIK_F3 0x3D
  67. #define DIK_F4 0x3E
  68. #define DIK_F5 0x3F
  69. #define DIK_F6 0x40
  70. #define DIK_F7 0x41
  71. #define DIK_F8 0x42
  72. #define DIK_F9 0x43
  73. #define DIK_F10 0x44
  74. #define DIK_NUMLOCK 0x45
  75. #define DIK_SCROLL 0x46 /* Scroll Lock */
  76. #define DIK_NUMPAD7 0x47
  77. #define DIK_NUMPAD8 0x48
  78. #define DIK_NUMPAD9 0x49
  79. #define DIK_SUBTRACT 0x4A /* - on numeric keypad */
  80. #define DIK_NUMPAD4 0x4B
  81. #define DIK_NUMPAD5 0x4C
  82. #define DIK_NUMPAD6 0x4D
  83. #define DIK_ADD 0x4E /* + on numeric keypad */
  84. #define DIK_NUMPAD1 0x4F
  85. #define DIK_NUMPAD2 0x50
  86. #define DIK_NUMPAD3 0x51
  87. #define DIK_NUMPAD0 0x52
  88. #define DIK_DECIMAL 0x53 /* . on numeric keypad */
  89. #define DIK_OEM_102 0x56 /* <> or \| on RT 102-key keyboard (Non-U.S.) */
  90. #define DIK_F11 0x57
  91. #define DIK_F12 0x58
  92. #define DIK_F13 0x64 /* (NEC PC98) */
  93. #define DIK_F14 0x65 /* (NEC PC98) */
  94. #define DIK_F15 0x66 /* (NEC PC98) */
  95. #define DIK_KANA 0x70 /* (Japanese keyboard) */
  96. #define DIK_ABNT_C1 0x73 /* /? on Brazilian keyboard */
  97. #define DIK_CONVERT 0x79 /* (Japanese keyboard) */
  98. #define DIK_NOCONVERT 0x7B /* (Japanese keyboard) */
  99. #define DIK_YEN 0x7D /* (Japanese keyboard) */
  100. #define DIK_ABNT_C2 0x7E /* Numpad . on Brazilian keyboard */
  101. #define DIK_NUMPADEQUALS 0x8D /* = on numeric keypad (NEC PC98) */
  102. #define DIK_PREVTRACK 0x90 /* Previous Track (DIK_CIRCUMFLEX on Japanese keyboard) */
  103. #define DIK_AT 0x91 /* (NEC PC98) */
  104. #define DIK_COLON 0x92 /* (NEC PC98) */
  105. #define DIK_UNDERLINE 0x93 /* (NEC PC98) */
  106. #define DIK_KANJI 0x94 /* (Japanese keyboard) */
  107. #define DIK_STOP 0x95 /* (NEC PC98) */
  108. #define DIK_AX 0x96 /* (Japan AX) */
  109. #define DIK_UNLABELED 0x97 /* (J3100) */
  110. #define DIK_NEXTTRACK 0x99 /* Next Track */
  111. #define DIK_NUMPADENTER 0x9C /* Enter on numeric keypad */
  112. #define DIK_RCONTROL 0x9D
  113. #define DIK_MUTE 0xA0 /* Mute */
  114. #define DIK_CALCULATOR 0xA1 /* Calculator */
  115. #define DIK_PLAYPAUSE 0xA2 /* Play / Pause */
  116. #define DIK_MEDIASTOP 0xA4 /* Media Stop */
  117. #define DIK_VOLUMEDOWN 0xAE /* Volume - */
  118. #define DIK_VOLUMEUP 0xB0 /* Volume + */
  119. #define DIK_WEBHOME 0xB2 /* Web home */
  120. #define DIK_NUMPADCOMMA 0xB3 /* , on numeric keypad (NEC PC98) */
  121. #define DIK_DIVIDE 0xB5 /* / on numeric keypad */
  122. #define DIK_SYSRQ 0xB7
  123. #define DIK_RMENU 0xB8 /* right Alt */
  124. #define DIK_PAUSE 0xC5 /* Pause */
  125. #define DIK_HOME 0xC7 /* Home on arrow keypad */
  126. #define DIK_UP 0xC8 /* UpArrow on arrow keypad */
  127. #define DIK_PRIOR 0xC9 /* PgUp on arrow keypad */
  128. #define DIK_LEFT 0xCB /* LeftArrow on arrow keypad */
  129. #define DIK_RIGHT 0xCD /* RightArrow on arrow keypad */
  130. #define DIK_END 0xCF /* End on arrow keypad */
  131. #define DIK_DOWN 0xD0 /* DownArrow on arrow keypad */
  132. #define DIK_NEXT 0xD1 /* PgDn on arrow keypad */
  133. #define DIK_INSERT 0xD2 /* Insert on arrow keypad */
  134. #define DIK_DELETE 0xD3 /* Delete on arrow keypad */
  135. #define DIK_LWIN 0xDB /* Left Windows key */
  136. #define DIK_RWIN 0xDC /* Right Windows key */
  137. #define DIK_APPS 0xDD /* AppMenu key */
  138. #define DIK_POWER 0xDE /* System Power */
  139. #define DIK_SLEEP 0xDF /* System Sleep */
  140. #define DIK_WAKE 0xE3 /* System Wake */
  141. #define DIK_WEBSEARCH 0xE5 /* Web Search */
  142. #define DIK_WEBFAVORITES 0xE6 /* Web Favorites */
  143. #define DIK_WEBREFRESH 0xE7 /* Web Refresh */
  144. #define DIK_WEBSTOP 0xE8 /* Web Stop */
  145. #define DIK_WEBFORWARD 0xE9 /* Web Forward */
  146. #define DIK_WEBBACK 0xEA /* Web Back */
  147. #define DIK_MYCOMPUTER 0xEB /* My Computer */
  148. #define DIK_MAIL 0xEC /* Mail */
  149. #define DIK_MEDIASELECT 0xED /* Media Select */

  150. /*
  151.  * Alternate names for keys, to facilitate transition from DOS.
  152.  */
  153. #define DIK_BACKSPACE DIK_BACK /* backspace */
  154. #define DIK_NUMPADSTAR DIK_MULTIPLY /* * on numeric keypad */
  155. #define DIK_LALT DIK_LMENU /* left Alt */
  156. #define DIK_CAPSLOCK DIK_CAPITAL /* CapsLock */
  157. #define DIK_NUMPADMINUS DIK_SUBTRACT /* - on numeric keypad */
  158. #define DIK_NUMPADPLUS DIK_ADD /* + on numeric keypad */
  159. #define DIK_NUMPADPERIOD DIK_DECIMAL /* . on numeric keypad */
  160. #define DIK_NUMPADSLASH DIK_DIVIDE /* / on numeric keypad */
  161. #define DIK_RALT DIK_RMENU /* right Alt */
  162. #define DIK_UPARROW DIK_UP /* UpArrow on arrow keypad */
  163. #define DIK_PGUP DIK_PRIOR /* PgUp on arrow keypad */
  164. #define DIK_LEFTARROW DIK_LEFT /* LeftArrow on arrow keypad */
  165. #define DIK_RIGHTARROW DIK_RIGHT /* RightArrow on arrow keypad */
  166. #define DIK_DOWNARROW DIK_DOWN /* DownArrow on arrow keypad */
  167. #define DIK_PGDN DIK_NEXT /* PgDn on arrow keypad */
补充:
   在DarkGDK中利用以下函数,也可以完成对某些按键的响应。
  1.  dbUpKey();检测是否按下up键
  2.  dbDownKey();        down键
  3.  dbLeftKey();        left键
  4.  dbRightKey();       right键
  5.  dbControlKey();     ctrl键
  6.  dbShiftKey();       shitf键
  7.  dbReturnKey();      Enter键
  8.  dbEscapeKey();      del键
  9.  dbSpaceKey();       space键

阅读(3480) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~