Project1

标题: 实现了WASD操作,通过菜单退回标题没问题,一旦F12就报错 [打印本页]

作者: sxjkjly8010    时间: 2021-1-30 13:01
标题: 实现了WASD操作,通过菜单退回标题没问题,一旦F12就报错
本帖最后由 sxjkjly8010 于 2021-1-30 13:42 编辑

如图 搞不懂为什么就发生堆栈错误了,求大佬指教,,,
RUBY 代码复制
  1. #==============================================================================
  2. # ** Glitchfinder's Key Input Module
  3. #    Version 1.30
  4. #------------------------------------------------------------------------------
  5. #  [RPG Maker XP] [RPG Maker VX] [RPG Maker VX Ace]
  6. #------------------------------------------------------------------------------
  7. #  This script helps scripters to use the full range of keys on any keyboard,
  8. #  without being limited by the default Input Module.
  9. #==============================================================================
  10. # * Version History
  11. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  12. #   Version 1.00 ------------------------------------------------- (2010-03-18)
  13. #     - Initial version
  14. #     - Author: Glitchfinder
  15. #    Version 1.10 ------------------------------------------------ (2010-08-20)
  16. #      - Added the Keys.toggle? method
  17. #      - Added the Keys.capslock? method
  18. #      - Added the Keys.numlock? method
  19. #      - Added the Keys.scroll? method
  20. #      - Added the Keys.character_press method
  21. #      - Added the Keys.character_trigger method
  22. #      - Added the Keys.character_repeat method
  23. #      - Added the Keys.get_character method
  24. #      - Added the ANYKEY key constant
  25. #      - Streamlined the script
  26. #      - Patched various errors
  27. #      - Author: Glitchfinder
  28. #    Version 1.20 ------------------------------------------------ (2011-03-17)
  29. #      - Added the Keys.array_press? method
  30. #      - Added the Keys.array_trigger? method
  31. #      - Added the Keys.array_repeat? method
  32. #      - Added the Keys.array_release? method
  33. #      - Added the Keys.array_toggle? method
  34. #      - Modified the Keys.press? method for better error hendling
  35. #      - Modified the Keys.trigger? method for better error hendling
  36. #      - Modified the Keys.repeat? method for better error hendling
  37. #      - Modified the Keys.release? method for better error hendling
  38. #      - Modified the Keys.toggle? method for better error hendling
  39. #      - Author: Glitchfinder
  40. #    Version 1.30 ------------------------------------------------ (2014-02-07)
  41. #      - Added the Keys.validate_key? method
  42. #      - Modified the Keys.array_press? method for efficiency
  43. #      - Modified the Keys.array_trigger? method for efficiency
  44. #      - Modified the Keys.array_repeat? method for efficiency
  45. #      - Modified the Keys.array_release? method for efficiency
  46. #      - Modified the Keys.array_toggle? method for efficiency
  47. #      - Modified the Keys.press? method to reduce unnecessary code
  48. #      - Modified the Keys.trigger? method to reduce unnecessary code
  49. #      - Modified the Keys.repeat? method to reduce unnecessary code
  50. #      - Modified the Keys.release? method to reduce unnecessary code
  51. #      - Modified the Keys.toggle? method to reduce unnecessary code
  52. #      - Changed the license to be more open
  53. #      - Author: Glitchfinder
  54. #==============================================================================
  55. # * Instructions
  56. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  57. #  Place this script above Main, and below the default scripts. (I realize this
  58. #  is obvious to most, but some people don't get it.)
  59. #
  60. #  This module is automatically updated by the default Input module, which
  61. #  means that the only time you need to call the update method is in a scene
  62. #  that does not update the default Input module.
  63. #
  64. #  This module does not break the functionality of the default Input module.
  65. #
  66. #  If you wish to read keys from a gamepad, you must still use the default
  67. #  input module to do so.
  68. #
  69. #  To use this module, simply use one of the four methods (press?(key),
  70. #  trigger?(key), repeat?(key), or release?(key)), where key is the index of
  71. #  the key you want to check. Key may also be used as Keys::KEYNAME. For a list
  72. #  of acceptable key names, look below the header.
  73. #
  74. #  There is a key named ANYKEY. This key can be used like any other key, with
  75. #  the exception that, instead of corresponding to any one key, it reacts to
  76. #  them all. If you use it, and any other key would cause the same method to
  77. #  return true, then the ANYKEY will also return true.
  78. #==============================================================================
  79. # * Method List
  80. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  81. #  Keys.update
  82. #    Updates keyboard input. Calls to this method are not necessary unless the
  83. #    default Input module is not being updated.
  84. #
  85. #  Keys.press?(key)
  86. #    Determines whether the button determined by key is currently being
  87. #    pressed. If the button is being pressed, returns true. If not, returns
  88. #    false.
  89. #
  90. #  Keys.trigger?(key)
  91. #    Determines whether the button determined by key is being pressed again.
  92. #    "Pressed again" is seen as time having passed between the button being not
  93. #    pressed and being pressed. If the button is being pressed, returns true.
  94. #    If not, returns false.
  95. #
  96. #  Keys.repeat?(key)
  97. #    Determines whether the button determined by key is being pressed again.
  98. #    Unlike trigger?(), this takes into account the repeat input of a button
  99. #    being held down continuously. If the button is being pressed, returns
  100. #    true. If not, returns false.
  101. #
  102. #  Keys.release?(key)
  103. #    Determines whether the button determined by key has just been released. If
  104. #    the button has been released, returns true. If not, returns false.
  105. #
  106. #  Keys.toggle?(key)
  107. #    Determines whether the button determined by key has been toggled. This
  108. #    functions like Caps Lock, Number Lock, and Scroll Lock, only for all keys.
  109. #
  110. #  Keys.array_press?(keys)
  111. #    Functions in the same manner as Keys.press?(), only it takes an array of
  112. #    keys as input.
  113. #
  114. #  Keys.array_trigger?(keys)
  115. #    Functions in the same manner as Keys.trigger?(), only it takes an array of
  116. #    keys as input.
  117. #
  118. #  Keys.array_repeat?(keys)
  119. #    Functions in the same manner as Keys.repeat?(), only it takes an array of
  120. #    keys as input.
  121. #
  122. #  Keys.array_release?(keys)
  123. #    Functions in the same manner as Keys.release?(), only it takes an array of
  124. #    keys as input.
  125. #
  126. #  Keys.array_toggle?(keys)
  127. #    Functions in the same manner as Keys.toggle?(), only it takes an array of
  128. #    keys as input.
  129. #
  130. #  Keys.capslock?
  131. #    Determines whether the caps lock key is toggled on. This will always
  132. #    return true when the corresponding keyboard light is lit, and false when
  133. #    it is not.
  134. #
  135. #  Keys.numlock?
  136. #    Determines whether the number lock key is toggled on. This will always
  137. #    return true when the corresponding keyboard light is lit, and false when
  138. #    it is not.
  139. #
  140. #  Keys.scroll?
  141. #    Determines whether the scroll lock key is toggled on. This will always
  142. #    return true when the corresponding keyboard light is lit, and false when
  143. #    it is not.
  144. #
  145. #  Keys.character_press
  146. #    Returns the currently pressed key, if it would generate text input. If no
  147. #    such key would register as pressed, it returns an empty string.
  148. #
  149. #  Keys.character_trigger
  150. #    Returns the currently triggered key, if it would generate text input. If
  151. #    no such key would register as triggered, it returns an empty string.
  152. #
  153. #  Keys.character_repeat
  154. #    Returns the currently repeated key, if it would generate text input. If no
  155. #    such key would register as repeated, it returns an empty string.
  156. #
  157. #  Keys.get_character(key, scan_code)
  158. #    Returns the character corresponding to the given key. If no character
  159. #    is recognized by the system, it returns false instead. This method is
  160. #    intended for internal use.
  161. #
  162. #  Keys.validate_key(key)
  163. #    Checks the validity of a key. If a key variable can be used by this
  164. #    module, it will return true. Otherwise, it will return false.
  165. #==============================================================================
  166. # * Known Issues
  167. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  168. #  This script has trouble with certain foreign language keyboards, where dead
  169. #  keys are shift sensitive. Dead keys are used to insert diacritical marks
  170. #  such as an accent. However, the correct character can be generated with
  171. #  creative use of shift and caps lock.
  172. #==============================================================================
  173. # * Glitchfinder's Advice
  174. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  175. #  This script is meant for people with a medium or advanced level of scripting
  176. #  knowledge and ability, or for those using scripts that require this module.
  177. #==============================================================================
  178. # * License
  179. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  180. #  This script is licensed under the MIT License:
  181. #
  182. #  Copyright (c) 2010-2014 Sean Porter (Glitchfinder) <[email protected]>
  183. #
  184. #  Permission is hereby granted, free of charge, to any person obtaining a copy
  185. #  of this software and associated documentation files (the "Software"), to deal
  186. #  in the Software without restriction, including without limitation the rights
  187. #  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  188. #  copies of the Software, and to permit persons to whom the Software is
  189. #  furnished to do so, subject to the following conditions:
  190. #
  191. #  The above copyright notice and this permission notice shall be included in
  192. #  all copies or substantial portions of the Software.
  193. #
  194. #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  195. #  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  196. #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  197. #  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  198. #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  199. #  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  200. #  THE SOFTWARE.
  201. #==============================================================================
  202.  
  203. #==============================================================================
  204. # ** Keys
  205. #------------------------------------------------------------------------------
  206. #  This module performs key input processing
  207. #==============================================================================
  208.  
  209. module Keys
  210.   #--------------------------------------------------------------------------
  211.   # * Miscellaneous Keys
  212.   #--------------------------------------------------------------------------
  213.   CANCEL              = 0x03 # Control-Break Processing
  214.   BACKSPACE           = 0x08 # Backspace Key
  215.   TAB                 = 0x09 # Tab Key
  216.   CLEAR               = 0x0C # Clear Key
  217.   RETURN              = 0x0D # Enter Key
  218.   SHIFT               = 0x10 # Shift Key
  219.   CONTROL             = 0x11 # Ctrl Key
  220.   MENU                = 0x12 # Alt Key
  221.   PAUSE               = 0x13 # Pause Key
  222.   ESCAPE              = 0x1B # Esc Key
  223.   CONVERT             = 0x1C # IME Convert Key
  224.   NONCONVERT          = 0x1D # IME Nonconvert Key
  225.   ACCEPT              = 0x1E # IME Accept Key
  226.   SPACE               = 0x20 # Space Bar Key (Space, usually blank)
  227.   PRIOR               = 0x21 # Page Up Key
  228.   NEXT                = 0x22 # Page Down Key
  229.   ENDS                = 0x23 # End Key
  230.   HOME                = 0x24 # Home Key
  231.   LEFT                = 0x25 # Left Arrow Key
  232.   UP                  = 0x26 # Up Arrow Key
  233.   RIGHT               = 0x27 # Right Arrow Key
  234.   DOWN                = 0x28 # Down Arrow Key
  235.   SELECT              = 0x29 # Select Key
  236.   PRINT               = 0x2A # Print Key
  237.   EXECUTE             = 0x2B # Execute Key
  238.   SNAPSHOT            = 0x2C # Print Screen Key
  239.   DELETE              = 0x2E # Delete Key
  240.   HELP                = 0x2F # Help Key
  241.   LSHIFT              = 0xA0 # Left Shift Key
  242.   RSHIFT              = 0xA1 # Right Shift Key
  243.   LCONTROL            = 0xA2 # Left Control Key (Ctrl)
  244.   RCONTROL            = 0xA3 # Right Control Key (Ctrl)
  245.   LMENU               = 0xA4 # Left Menu Key (Alt)
  246.   RMENU               = 0xA5 # Right Menu Key (Alt)
  247.   PACKET              = 0xE7 # Used to Pass Unicode Characters as Keystrokes
  248.   #--------------------------------------------------------------------------
  249.   # * Number Keys
  250.   #--------------------------------------------------------------------------
  251.   N0                  = 0x30 # 0 Key
  252.   N1                  = 0x31 # 1 Key
  253.   N2                  = 0x32 # 2 Key
  254.   N3                  = 0x33 # 3 Key
  255.   N4                  = 0x34 # 4 Key
  256.   N5                  = 0x35 # 5 Key
  257.   N6                  = 0x36 # 6 Key
  258.   N7                  = 0x37 # 7 Key
  259.   N8                  = 0x38 # 8 Key
  260.   N9                  = 0x39 # 9 Key
  261.   #--------------------------------------------------------------------------
  262.   # * Letter Keys
  263.   #--------------------------------------------------------------------------
  264.   A                   = 0x41 # A Key
  265.   B                   = 0x42 # B Key
  266.   C                   = 0x43 # C Key
  267.   D                   = 0x44 # D Key
  268.   E                   = 0x45 # E Key
  269.   F                   = 0x46 # F Key
  270.   G                   = 0x47 # G Key
  271.   H                   = 0x48 # H Key
  272.   I                   = 0x49 # I Key
  273.   J                   = 0x4A # J Key
  274.   K                   = 0x4B # K Key
  275.   L                   = 0x4C # L Key
  276.   M                   = 0x4D # M Key
  277.   N                   = 0x4E # N Key
  278.   O                   = 0x4F # O Key
  279.   P                   = 0x50 # P Key
  280.   Q                   = 0x51 # Q Key
  281.   R                   = 0x52 # R Key
  282.   S                   = 0x53 # S Key
  283.   T                   = 0x54 # T Key
  284.   U                   = 0x55 # U Key
  285.   V                   = 0x56 # V Key
  286.   W                   = 0x57 # W Key
  287.   X                   = 0x58 # X Key
  288.   Y                   = 0x59 # Y Key
  289.   Z                   = 0x5A # Z Key
  290.   #--------------------------------------------------------------------------
  291.   # * Windows Keys
  292.   #--------------------------------------------------------------------------
  293.   LWIN                = 0x5B # Left Windows Key (Natural keyboard)
  294.   RWIN                = 0x5C # Right Windows Key (Natural Keyboard)
  295.   APPS                = 0x5D # Applications Key (Natural keyboard)
  296.   SLEEP               = 0x5F # Computer Sleep Key
  297.   BROWSER_BACK        = 0xA6 # Browser Back Key
  298.   BROWSER_FORWARD     = 0xA7 # Browser Forward Key
  299.   BROWSER_REFRESH     = 0xA8 # Browser Refresh Key
  300.   BROWSER_STOP        = 0xA9 # Browser Stop Key
  301.   BROWSER_SEARCH      = 0xAA # Browser Search Key
  302.   BROWSER_FAVORITES   = 0xAB # Browser Favorites Key
  303.   BROWSER_HOME        = 0xAC # Browser Start and Home Key
  304.   VOLUME_MUTE         = 0xAD # Volume Mute Key
  305.   VOLUME_DOWN         = 0xAE # Volume Down Key
  306.   VOLUME_UP           = 0xAF # Volume Up Key
  307.   MEDIA_NEXT_TRACK    = 0xB0 # Next Track Key
  308.   MEDIA_PREV_TRACK    = 0xB1 # Previous Track Key
  309.   MEDIA_STOP          = 0xB2 # Stop Media Key
  310.   MEDIA_PLAY_PAUSE    = 0xB3 # Play/Pause Media Key
  311.   LAUNCH_MAIL         = 0xB4 # Start Mail Key
  312.   LAUNCH_MEDIA_SELECT = 0xB5 # Select Media Key
  313.   LAUNCH_APP1         = 0xB6 # Start Application 1 Key
  314.   LAUNCH_APP2         = 0xB7 # Start Application 2 Key
  315.   PROCESSKEY          = 0xE5 # IME Process Key
  316.   ATTN                = 0xF6 # Attn Key
  317.   CRSEL               = 0xF7 # CrSel Key
  318.   EXSEL               = 0xF8 # ExSel Key
  319.   EREOF               = 0xF9 # Erase EOF Key
  320.   PLAY                = 0xFA # Play Key
  321.   ZOOM                = 0xFB # Zoom Key
  322.   PA1                 = 0xFD # PA1 Key
  323.   #--------------------------------------------------------------------------
  324.   # * Number Pad Keys
  325.   #--------------------------------------------------------------------------
  326.   NUMPAD0             = 0x60 # Numeric Keypad 0 Key
  327.   NUMPAD1             = 0x61 # Numeric Keypad 1 Key
  328.   NUMPAD2             = 0x62 # Numeric Keypad 2 Key
  329.   NUMPAD3             = 0x63 # Numeric Keypad 3 Key
  330.   NUMPAD4             = 0x64 # Numeric Keypad 4 Key
  331.   NUMPAD5             = 0x65 # Numeric Keypad 5 Key
  332.   NUMPAD6             = 0x66 # Numeric Keypad 6 Key
  333.   NUMPAD7             = 0x67 # Numeric Keypad 7 Key
  334.   NUMPAD8             = 0x68 # Numeric Keypad 8 Key
  335.   NUMPAD9             = 0x69 # Numeric Keypad 9 Key
  336.   MULTIPLY            = 0x6A # Multiply Key (*)
  337.   ADD                 = 0x6B # Add Key (+)
  338.   SEPARATOR           = 0x6C # Separator Key
  339.   SUBTRACT            = 0x6D # Subtract Key (-)
  340.   DECIMAL             = 0x6E # Decimal Key (.)
  341.   DIVIDE              = 0x6F # Divide Key (/)
  342.   #--------------------------------------------------------------------------
  343.   # * Function Keys
  344.   #--------------------------------------------------------------------------
  345.   F1                  = 0x70 # F1 Key
  346.   F2                  = 0x71 # F2 Key
  347.   F3                  = 0x72 # F3 Key
  348.   F4                  = 0x73 # F4 Key
  349.   F5                  = 0x74 # F5 Key
  350.   F6                  = 0x75 # F6 Key
  351.   F7                  = 0x76 # F7 Key
  352.   F8                  = 0x77 # F8 Key
  353.   F9                  = 0x78 # F9 Key
  354.   F10                 = 0x79 # F10 Key
  355.   F11                 = 0x7A # F11 Key
  356.   F12                 = 0x7B # F12 Key
  357.   F13                 = 0x7C # F13 Key
  358.   F14                 = 0x7D # F14 Key
  359.   F15                 = 0x7E # F15 Key
  360.   F16                 = 0x7F # F16 Key
  361.   F17                 = 0x80 # F17 Key
  362.   F18                 = 0x81 # F18 Key
  363.   F19                 = 0x82 # F19 Key
  364.   F20                 = 0x83 # F20 Key
  365.   F21                 = 0x84 # F21 Key
  366.   F22                 = 0x85 # F22 Key
  367.   F23                 = 0x86 # F23 Key
  368.   F24                 = 0x87 # F24 Key
  369.   #--------------------------------------------------------------------------
  370.   # * Toggle Keys
  371.   #--------------------------------------------------------------------------
  372.   CAPITAL             = 0x14 # Caps Lock Key
  373.   KANA                = 0x15 # IME Kana Mode Key
  374.   HANGUL              = 0x15 # IME Hangul Mode Key
  375.   JUNJA               = 0x17 # IME Junja Mode Key
  376.   FINAL               = 0x18 # IME Final Mode Key
  377.   HANJA               = 0x19 # IME Hanja Mode Key
  378.   KANJI               = 0x19 # IME Kanji Mode Key
  379.   MODECHANGE          = 0x1F # IME Mode Change Request Key
  380.   INSERT              = 0x2D # Insert Key
  381.   NUMLOCK             = 0x90 # Num Lock Key
  382.   SCROLL              = 0x91 # Scroll Lock Key
  383.   #--------------------------------------------------------------------------
  384.   # * OEM Keys (Vary by keyboard)
  385.   #--------------------------------------------------------------------------
  386.   OEM_1               = 0xBA # Misc Characters (; : in USA 101/102 Keyboards)
  387.   OEM_PLUS            = 0xBB # + = Key
  388.   OEM_COMMA           = 0xBC # , < Key
  389.   OEM_MINUS           = 0xBD # - _ Key
  390.   OEM_PERIOD          = 0xBE # . > Key
  391.   OEM_2               = 0xBF # Misc Characters (/ ? in USA 101/102 Keyboards)
  392.   OEM_3               = 0xC0 # Misc Characters (` ~ in USA 101/102 Keyboards)
  393.   OEM_4               = 0xDB # Misc Characters ([ { in USA 101/102 Keyboards)
  394.   OEM_5               = 0xDC # Misc Characters (\ | in USA 101/102 Keyboards)
  395.   OEM_6               = 0xDD # Misc Characters (] } in USA 101/102 Keyboards)
  396.   OEM_7               = 0xDE # Misc Characters (' " in USA 101/102 Keyboards)
  397.   OEM_8               = 0xDF # Misc Characters (Varies by Keyboard)
  398.   OEM_9               = 0xE1 # OEM Specific
  399.   OEM_10              = 0x92 # OEM Specific
  400.   OEM_11              = 0x93 # OEM Specific
  401.   OEM_12              = 0x94 # OEM Specific
  402.   OEM_13              = 0x95 # OEM Specific
  403.   OEM_14              = 0x96 # OEM Specific
  404.   OEM_15              = 0xE3 # OEM Specific
  405.   OEM_16              = 0xE4 # OEM Specific
  406.   OEM_17              = 0xE6 # OEM Specific
  407.   OEM_18              = 0xE9 # OEM Specific
  408.   OEM_19              = 0xEA # OEM Specific
  409.   OEM_20              = 0xEB # OEM Specific
  410.   OEM_21              = 0xEC # OEM Specific
  411.   OEM_22              = 0xED # OEM Specific
  412.   OEM_23              = 0xEE # OEM Specific
  413.   OEM_24              = 0xEF # OEM Specific
  414.   OEM_25              = 0xF1 # OEM Specific
  415.   OEM_26              = 0xF2 # OEM Specific
  416.   OEM_27              = 0xF3 # OEM Specific
  417.   OEM_28              = 0xF4 # OEM Specific
  418.   OEM_29              = 0xF5 # OEM Specific
  419.   OEM_102             = 0xE2 # Angle Bracket or Backslash on RT-102 Keyboards
  420.   OEM_CLEAR           = 0xFE # Clear Key
  421.   #--------------------------------------------------------------------------
  422.   # * Special Keys
  423.   #--------------------------------------------------------------------------
  424.   ANYKEY              = 0x100 # Any Key
  425.   #--------------------------------------------------------------------------
  426.   # * Declare Module Variables
  427.   #--------------------------------------------------------------------------
  428.   begin
  429.     # Create strings for unpacking input
  430.     @unpack_string = 'B' * 256
  431.     @toggle_unpack_string = 'b' * 256
  432.     # Generate blank input arrays
  433.     @last_array = '0' * 256
  434.     @press   = Array.new(256, false) # Key currently pressed
  435.     @trigger = Array.new(256, false) # Key initially pressed
  436.     @repeat  = Array.new(256, false) # Key being held
  437.     @release = Array.new(256, false) # Key being released
  438.     @toggle  = Array.new(256, false) # Key currently toggled
  439.     # Generate blank counter array
  440.     @repeat_counter = Array.new(256, 0)
  441.     # Declare keyboard API
  442.     @getKeyboardState = Win32API.new('user32.dll', 'GetKeyboardState', 'P', 'V')
  443.     @getAsyncKeyState = Win32API.new('user32.dll', 'GetAsyncKeyState', 'I', 'I')
  444.     @getKeyState = Win32API.new('user32.dll', 'GetKeyState', 'I', 'I')
  445.     @getKeyboardLayoutName = Win32API.new('user32.dll', 'GetKeyboardLayoutName',
  446.       'P', 'I')
  447.     @loadKeyboardLayout = Win32API.new('user32.dll', 'LoadKeyboardLayout', 'PI',
  448.       'I')
  449.     @mapVirtualKeyEx = Win32API.new('user32.dll', 'MapVirtualKeyEx', 'III', 'I')
  450.     @toUnicodeEx = Win32API.new('user32.dll', 'ToUnicodeEx', 'IIPPIII', 'I')
  451.     # Call current keyboard state
  452.     @getKeyboardState.call(@last_array)
  453.     # Set previous keyboard state
  454.     @last_array = @last_array.unpack(@unpack_string)
  455.     # Set a blank keyboard state
  456.     @keyboard_state = '0'*256
  457.     # Set array to input character list
  458.     @input_characters = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S,
  459.       T, U, V, W, X, Y, Z, N0, N1, N2, N3, N4, N5, N6, N7, N8, N9, OEM_MINUS,
  460.       OEM_PLUS, OEM_COMMA, OEM_PERIOD, OEM_1, OEM_2, OEM_3, OEM_4, OEM_5, OEM_6,
  461.       OEM_7, OEM_8, OEM_9, OEM_10, OEM_11, OEM_12, OEM_13, OEM_14, OEM_15,
  462.       OEM_16, OEM_17, OEM_18, OEM_19, OEM_20, OEM_21, OEM_22, OEM_23, OEM_24,
  463.       OEM_25, OEM_26, OEM_27, OEM_28, OEM_29, OEM_102, NUMPAD0, NUMPAD1, NUMPAD2,
  464.       NUMPAD3, NUMPAD4, NUMPAD5, NUMPAD6, NUMPAD7, NUMPAD8, NUMPAD9, DECIMAL,
  465.       MULTIPLY, ADD, SEPARATOR, SUBTRACT, DIVIDE, SPACE]
  466.     # Create a keyboard layout buffer
  467.     layout_name = '0' * 8
  468.     # Get the keyboard name
  469.     success = @getKeyboardLayoutName.call(layout_name)
  470.     # Set the layout to US 101 if no name was obtained
  471.     layout_name = '00000409' if success == 0
  472.     # Save the keyboard layout
  473.     @layout = @loadKeyboardLayout.call(layout_name, 0x00000001)
  474.     # Create an empty array to store mapped virtual keys
  475.     @mapped_keys = []
  476.     # Iterate through the input character list
  477.     for key in @input_characters
  478.       # Insert the mapped virtual key into the array
  479.       @mapped_keys.push(@mapVirtualKeyEx.call(key, 0, @layout))
  480.     end
  481.     # create blank input array
  482.     @keyboard_state = '0'*256
  483.     # Call current keyboard state
  484.     @getKeyboardState.call(@keyboard_state)
  485.     # Create an array to detect dead keys
  486.     @dead_keys = []
  487.     # Iterate through the input character array
  488.     for key in 0...256
  489.       # Find the key's current scan code
  490.       scan_code = @mapVirtualKeyEx.call(key, 0, @layout)
  491.       # Skip if there is no scan code for the current layout
  492.       next if scan_code == 0
  493.       # Create a character buffer
  494.       buffer = '0' * 10
  495.       # Test the key
  496.       dead_key = @toUnicodeEx.call(key, scan_code, @keyboard_state, buffer, 5, 0,
  497.         @layout)
  498.       # if the key is a dead key
  499.       if dead_key == -1
  500.         # Add the key to the dead key array
  501.         @dead_keys.push(key)
  502.         # Create a character buffer
  503.         buffer = '0' * 10
  504.         # Clear the input queue
  505.         dead_key = @toUnicodeEx.call(@input_characters[-1], @mapped_keys[-1],
  506.           @keyboard_state, buffer, 5, 0, @layout)
  507.       end
  508.    end
  509.    # Set the dead keys to off
  510.    @dead_key_on = false
  511.   end
  512.   #--------------------------------------------------------------------------
  513.   # * Frame Update
  514.   #--------------------------------------------------------------------------
  515.   def self.update
  516.     # Clear input arrays
  517.     @trigger = Array.new(256, false)
  518.     @repeat = Array.new(256, false)
  519.     @release = Array.new(256, false)
  520.     # create blank input array
  521.     @keyboard_state = '0' * 256
  522.     # Call current keyboard state
  523.     @getKeyboardState.call(@keyboard_state)
  524.     # unpack toggle array
  525.     toggle = @keyboard_state.unpack(@toggle_unpack_string)
  526.     # Unpack key array
  527.     array = @keyboard_state.unpack(@unpack_string)
  528.     # Cycle through all keys
  529.     for i in 0...array.size
  530.       # Set the current key state
  531.       @press[i] = (array[i].to_i != 0)
  532.       # Set the current toggle unpack state
  533.       @toggle[i] = (toggle[i].to_i != 0)
  534.       # If the current key state does not match the previous state
  535.       if array[i] != @last_array[i]
  536.         # If the repeat counter is at 0
  537.         if @repeat_counter[i] <= 0 && @press[i]
  538.           # Set the key to repeat
  539.           @repeat[i] = true
  540.           # Set the repeat counter to 15 frames
  541.           @repeat_counter[i] = 15
  542.         end
  543.         # If the key is not being pressed
  544.         if !@press[i]
  545.           # Set the key to released
  546.           @release[i] = true
  547.         # If the key is being pressed
  548.         else
  549.           # Set the key to triggered
  550.           @trigger[i] = true
  551.         end
  552.       # If the key state is the same
  553.       else
  554.         # If the repeat counter is greater than 0 and the key is pressed
  555.         if @repeat_counter[i] > 0 && @press[i]
  556.           # Cycle the repeat counter down one frame
  557.           @repeat_counter[i] -= 1
  558.         # If the repeat counter is 0 or less and the key is pressed
  559.         elsif @repeat_counter[i] <= 0 && @press[i]
  560.           # Set the key to repeat
  561.           @repeat[i] = true
  562.           # Set the repeat counter to 15 frames
  563.           @repeat_counter[i] = 3
  564.         # If the repeat counter does not equal 0
  565.         elsif @repeat_counter[i] != 0 && !@press[i]
  566.           # Set the repeat counter to 0
  567.           @repeat_counter[i] = 0
  568.         end
  569.       end
  570.     end
  571.     # Set the previous keyboard state
  572.     @last_array = array
  573.   end
  574.   #--------------------------------------------------------------------------
  575.   # * Get Key Pressed State
  576.   #     key : key index
  577.   #--------------------------------------------------------------------------
  578.   def self.press?(key)
  579.     # Return the array state if key is an array
  580.     return array_press?(key) if (Array == key.class)
  581.     # Return false if key is invalid
  582.     return false if !validate_key(key)
  583.     # Return for any key
  584.     return @press.include?(true) if key == 0x100
  585.     # Return key pressed state
  586.     return @press[key]
  587.   end
  588.   #--------------------------------------------------------------------------
  589.   # * Get Key Triggered State
  590.   #     key : key index
  591.   #--------------------------------------------------------------------------
  592.   def self.trigger?(key)
  593.     # Return the array state if key is an array
  594.     return array_trigger?(key) if (Array == key.class)
  595.     # Return false if key is invalid
  596.     return false if !validate_key(key)
  597.     # Return for any key
  598.     return @trigger.include?(true) if key == 0x100
  599.     # Return key triggered state
  600.     return @trigger[key]
  601.   end
  602.   #--------------------------------------------------------------------------
  603.   # * Get Key Repeated State
  604.   #     key : key index
  605.   #--------------------------------------------------------------------------
  606.   def self.repeat?(key)
  607.     # Return the array state if key is an array
  608.     return array_repeat?(key) if (Array == key.class)
  609.     # Return false if key is invalid
  610.     return false if !validate_key(key)
  611.     # Return for any key
  612.     return @repeat.include?(true) if key == 0x100
  613.     # Return key repeated state
  614.     return @repeat[key]
  615.   end
  616.   #--------------------------------------------------------------------------
  617.   # * Get Key Released State
  618.   #     key : key index
  619.   #--------------------------------------------------------------------------
  620.   def self.release?(key)
  621.     # Return the array state if key is an array
  622.     return array_release?(key) if (Array == key.class)
  623.     # Return false if key is invalid
  624.     return false if !validate_key(key)
  625.     # Return for any key
  626.     return @release.include?(true) if key == 0x100
  627.     # Return key released state
  628.     return @release[key]
  629.   end
  630.   #--------------------------------------------------------------------------
  631.   # * Get Key Toggled State
  632.   #     key : key index
  633.   #--------------------------------------------------------------------------
  634.   def self.toggle?(key)
  635.     # Return the array state if key is an array
  636.     return array_toggle?(key) if (Array == key.class)
  637.     # Return false if key is invalid
  638.     return false if !validate_key(key)
  639.     # Return for any key
  640.     return @toggle.include?(true) if key == 0x100
  641.     # Return key toggled state
  642.     return @toggle[key]
  643.   end
  644.   #--------------------------------------------------------------------------
  645.   # * Get Key Pressed State (array)
  646.   #     keys : array of key indices
  647.   #--------------------------------------------------------------------------
  648.   def self.array_press?(keys)
  649.     # Return as a key if the input is not an array
  650.     return press?(keys) if (Array != keys.class)
  651.     # Create a temporary array to store key states
  652.     states = [false]
  653.     # Iterate through the keys array
  654.     for key in keys
  655.       # Add the key state to the array
  656.       states << press?(key)
  657.     end
  658.     # Return true if any of the keys were pressed
  659.     return states.include?(true)
  660.   end
  661.   #--------------------------------------------------------------------------
  662.   # * Get Key Triggered State (array)
  663.   #     keys : array of key indices
  664.   #--------------------------------------------------------------------------
  665.   def self.array_trigger?(keys)
  666.     # Return as a key if the input is not an array
  667.     return trigger?(keys.to_i) if (Array != keys.class)
  668.     # Create a temporary array to store key states
  669.     states = [false]
  670.     # Iterate through the keys array
  671.     for key in keys
  672.       # Add the key state to the array
  673.       states << trigger?(key)
  674.     end
  675.     # Return true if any of the keys were pressed
  676.     return states.include?(true)
  677.   end
  678.   #--------------------------------------------------------------------------
  679.   # * Get Key Repeated State (array)
  680.   #     keys : array of key indices
  681.   #--------------------------------------------------------------------------
  682.   def self.array_repeat?(keys)
  683.     # Return as a key if the input is not an array
  684.     return repeat?(keys.to_i) if (Array != keys.class)
  685.     # Create a temporary array to store key states
  686.     states = [false]
  687.     # Iterate through the keys array
  688.     for key in keys
  689.       # Add the key state to the array
  690.       states << repeat?(key)
  691.     end
  692.     # Return true if any of the keys were pressed
  693.     return states.include?(true)
  694.   end
  695.   #--------------------------------------------------------------------------
  696.   # * Get Key Released State (array)
  697.   #     keys : array of key indices
  698.   #--------------------------------------------------------------------------
  699.   def self.array_release?(keys)
  700.     # Return as a key if the input is not an array
  701.     return release?(keys.to_i) if (Array != keys.class)
  702.     # Create a temporary array to store key states
  703.     states = [false]
  704.     # Iterate through the keys array
  705.     for key in keys
  706.       # Add the key state to the array
  707.       states << release?(key)
  708.     end
  709.     # Return true if any of the keys were pressed
  710.     return states.include?(true)
  711.   end
  712.   #--------------------------------------------------------------------------
  713.   # * Get Key Toggled State (array)
  714.   #     keys : array of key indices
  715.   #--------------------------------------------------------------------------
  716.   def self.array_toggle?(keys)
  717.     # Return as a key if the input is not an array
  718.     return toggle?(keys.to_i) if (Array != keys.class)
  719.     # Create a temporary array to store key states
  720.     states = [false]
  721.     # Iterate through the keys array
  722.     for key in keys
  723.       # Add the key state to the array
  724.       states << toggle?(key)
  725.     end
  726.     # Return true if any of the keys were pressed
  727.     return states.include?(true)
  728.   end
  729.   #--------------------------------------------------------------------------
  730.   # * Get Caps Lock State
  731.   #--------------------------------------------------------------------------
  732.   def self.capslock?
  733.     # Return the current toggle state of Caps Lock
  734.     return @toggle[CAPITAL]
  735.   end
  736.   #--------------------------------------------------------------------------
  737.   # * Get Number Lock State
  738.   #--------------------------------------------------------------------------
  739.   def self.numlock?
  740.     # Return the current toggle state of Num Lock
  741.     return @toggle[NUMLOCK]
  742.   end
  743.   #--------------------------------------------------------------------------
  744.   # * Get Scroll Lock State
  745.   #--------------------------------------------------------------------------
  746.   def self.scroll?
  747.     # Return the current toggle state of Scroll Lock
  748.     return @toggle[SCROLL]
  749.   end
  750.   #--------------------------------------------------------------------------
  751.   # * Return Current Key Character Pressed
  752.   #--------------------------------------------------------------------------
  753.   def self.character_press
  754.     # Iterate through the list of input characters
  755.     for i in 0...@input_characters.size
  756.       # Set a temporary variable to the value of the current key
  757.       key = @input_characters[i]
  758.       # Skip if the character is not currently pressed
  759.       next unless self.press?(key)
  760.       # Get the current key's scan code
  761.       scan_code = @mapped_keys[i]
  762.       # Get the specified character
  763.       character = get_character(key, scan_code)
  764.       # Skip if there was no translation for the character
  765.       next if !character || character == -1
  766.       # Return the translated character
  767.       return character
  768.     end
  769.     # Return blank if no characters were returned
  770.     return ""
  771.   end
  772.   #--------------------------------------------------------------------------
  773.   # * Return Current Key Character Triggered
  774.   #--------------------------------------------------------------------------
  775.   def self.character_trigger
  776.     # Iterate through the list of input characters
  777.     for i in 0...@input_characters.size
  778.       # Set a temporary variable to the value of the current key
  779.       key = @input_characters[i]
  780.       # Skip if the character is not currently triggered
  781.       next unless self.trigger?(key)
  782.       # Get the current key's scan code
  783.       scan_code = @mapped_keys[i]
  784.       # Get the specified character
  785.       character = get_character(key, scan_code)
  786.       # Skip if there was no translation for the character
  787.       next if !character || character == -1
  788.       # Return the translated character
  789.       return character
  790.     end
  791.     # Return blank if no characters were returned
  792.     return ""
  793.   end
  794.   #--------------------------------------------------------------------------
  795.   # * Return Current Key Character Repeated
  796.   #--------------------------------------------------------------------------
  797.   def self.character_repeat
  798.     # Iterate through the list of input characters
  799.     for i in 0...@input_characters.size
  800.       # Set a temporary variable to the value of the current key
  801.       key = @input_characters[i]
  802.       # Skip if the character is not currently repeated
  803.       next unless self.repeat?(key)
  804.       # Get the current key's scan code
  805.       scan_code = @mapped_keys[i]
  806.       # Get the specified character
  807.       character = get_character(key, scan_code)
  808.       # Skip if there was no translation for the character
  809.       next if !character || character == -1
  810.       # Return the translated character
  811.       return character
  812.     end
  813.     # Return blank if no characters were returned
  814.     return ""
  815.   end
  816.   #--------------------------------------------------------------------------
  817.   # * Get Character from Key
  818.   #--------------------------------------------------------------------------
  819.   def self.get_character(key, scan_code)
  820.     # Create a character buffer
  821.     buffer = '0' * 10
  822.     # Translate the current key to Unicode
  823.     success = @toUnicodeEx.call(key, scan_code, @keyboard_state, buffer, 5, 0,
  824.       @layout)
  825.     # Return false there was no translation for the character
  826.     return false if success == 0
  827.     # Iterate through the dead keys
  828.     for dead_key in @dead_keys
  829.       # If the dead key was pressed
  830.       if @getAsyncKeyState.call(dead_key) != 0
  831.         # Map the dead key scan code
  832.         dead_scan =  @mapVirtualKeyEx.call(dead_key, 0, @layout)
  833.         # Recreate a character buffer
  834.         buffer = '0' * 10
  835.         # Translate the current key to Unicode
  836.         success = @toUnicodeEx.call(dead_key, dead_scan, @keyboard_state,
  837.           buffer, 5, 0, @layout)
  838.         # If it returns as a dead key
  839.         if success == -1
  840.           # Recreate a character buffer
  841.           buffer = '0' * 10
  842.           # Translate the current key to Unicode
  843.           success = @toUnicodeEx.call(key, scan_code, @keyboard_state, buffer,
  844.           5, 0, @layout)
  845.         end
  846.         # Return false there was no translation for the character
  847.         return false if success == 0
  848.       end
  849.     end
  850.     # Translate the characters to Ruby encoding
  851.     characters = buffer.unpack('C*').pack('U*')
  852.     # Create the return character
  853.     array = []
  854.     # Iterate through the character buffer
  855.     for i in 0...(success * 2)
  856.       # Add characters to the character array
  857.       array.push(characters[i]) unless array.include?(characters[i])
  858.     end
  859.     # If a dead key was pressed
  860.     if success == -1
  861.       # Iterate through the character buffer
  862.       for i in 0...2
  863.         # Add characters to the character array
  864.         array.push(characters[i]) unless array.include?(characters[i])
  865.       end
  866.     end
  867.     # Delete null characters
  868.     array.delete_if {|byte| byte == 0}
  869.     # Create an empty string for the return character
  870.     character = ' ' * array.size
  871.     # Iterate through the character array
  872.     for i in 0...array.size
  873.       # Add the character to the string
  874.       character[i] = array[i]
  875.     end
  876.     # Return the translated character
  877.     return character
  878.   end
  879.   #--------------------------------------------------------------------------
  880.   # * Validate Key
  881.   #--------------------------------------------------------------------------
  882.   def self.validate_key(key)
  883.     # Return false if key is not a number
  884.     return false if !(key.is_a?(Numeric))
  885.     # Force key into the integer class
  886.     key = key.to_i
  887.     # Return false if the key does not exist or is not an integer
  888.     return false if ((key < 0x01) || (key > 0x100) || !(key == key.to_i))
  889.     # Return that the key is valid
  890.     return true
  891.   end
  892. end
  893.  
  894. #==============================================================================
  895. # ** Input
  896. #------------------------------------------------------------------------------
  897. #  This module performs key input processing
  898. #==============================================================================
  899.  
  900. module Input
  901.   # Add class data
  902.   class << self
  903.     #------------------------------------------------------------------------
  904.     # * Alias Methods
  905.     #------------------------------------------------------------------------
  906.     # If the update method has not been aliased
  907.     unless method_defined?(:keyinputmodule_input_update)
  908.       # Alias the update method
  909.       alias keyinputmodule_input_update update
  910.     end
  911.     #-------------------------------------------------------------------------
  912.     # * Frame Update
  913.     #-------------------------------------------------------------------------
  914.     def update
  915.       # Call original method
  916.       keyinputmodule_input_update
  917.       # Update Keys module
  918.       Keys.update
  919.     end
  920.   end
  921. end

RUBY 代码复制
  1. class << Input
  2.   Key_UP = [Keys::UP,Keys::W]
  3.   Key_DOWN = [Keys::DOWN,Keys::S]
  4.   Key_LEFT = [Keys::LEFT,Keys::A]
  5.   Key_RIGHT = [Keys::RIGHT,Keys::D]
  6.   alias :o_press :press?
  7.   def press?(num)
  8.     return true if num==Input::UP && Key_UP.any?{ |key| Keys.press?(key) }
  9.     return true if num==Input::DOWN && Key_DOWN.any?{ |key| Keys.press?(key) }
  10.     return true if num==Input::LEFT && Key_LEFT.any?{ |key| Keys.press?(key) }
  11.     return true if num==Input::RIGHT && Key_RIGHT.any?{ |key| Keys.press?(key) }
  12.     o_press(num)
  13.   end
  14.   alias :o_repeat :repeat?
  15.   def repeat?(num)
  16.     return true if num==Input::UP && Key_UP.any?{ |key| Keys.repeat?(key) }
  17.     return true if num==Input::DOWN && Key_DOWN.any?{ |key| Keys.repeat?(key) }
  18.     return true if num==Input::LEFT && Key_LEFT.any?{ |key| Keys.repeat?(key) }
  19.     return true if num==Input::RIGHT && Key_RIGHT.any?{ |key| Keys.repeat?(key) }
  20.     o_repeat(num)
  21.   end
  22.   alias :o_trigger :trigger?
  23.   def trigger?(num)
  24.     return true if num==Input::UP && Key_UP.any?{ |key| Keys.trigger?(key) }
  25.     return true if num==Input::DOWN && Key_DOWN.any?{ |key| Keys.trigger?(key) }
  26.     return true if num==Input::LEFT && Key_LEFT.any?{ |key| Keys.trigger?(key) }
  27.     return true if num==Input::RIGHT && Key_RIGHT.any?{ |key| Keys.trigger?(key) }
  28.     o_trigger(num)
  29.   end
  30.   def dir4()
  31.     return 8 if press?(Input::UP)
  32.     return 4 if press?(Input::LEFT)
  33.     return 6 if press?(Input::RIGHT)
  34.     return 2 if press?(Input::DOWN)
  35.     0
  36.   end
  37.   def dir8()
  38.     dir = press?(Input::UP) ? 8 : 0
  39.     dir = press?(Input::LEFT) ? dir==0 ? 4 : 7 : dir
  40.     return dir if dir == 7
  41.     dir = press?(Input::RIGHT) ? dir==4 ? 0 : dir==8 ? 9 : 6 : dir
  42.     return dir if dir == 9
  43.     press?(Input::DOWN) ? dir==8 ? 0 : dir==4 ? 1 : dir==6 ? 3 : 2 : dir
  44.   end
  45. end


我解决了,此贴终结

QQ截图20210130125834.png (11.8 KB, 下载次数: 62)

QQ截图20210130125834.png

作者: HATSUNEMIMU    时间: 2021-10-8 02:03
虽然看不懂代码,但是依据基本逻辑建议排查一下按键冲突问题、
作者: RPGzh500223    时间: 2021-10-8 20:15
第二页脚本
alias :o_press :press? unless method_defined?(:o_press)
alias :o_repeat :repeat? unless method_defined?(:o_repeat)
alias :o_trigger :trigger? unless method_defined?(:o_trigger)





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1