Key Input Module 1.30 [XP/VX/VXA]

Key Input Module
Author: Glitchfinder
Version: 1.30




Index
    1 - Index
2 - Introduction
3 - Features
4 - Screenshots
5 - Script
6 - Instructions
7 - Method List
8 - Issues
9 - License



Introduction

Glitchfinder's Key Input Module is an advanced scripting tool that provides full scripted access to to check any and all keys on the keyboard, along with the ability to check if a key has been released. It is useful in the sense that it will not break the default Input module, and because it allows far more use of the keyboard than you would get from default RMXP. It also does not need to be updated, aside from the default call to Input.update that is used in most cases. It also does not break default scripts or events that use key input.

(Return to top)


Features
  • Check any key on the keyboard
  • Doesn't break the default Input module
  • Does not break events or scripts that call the default Input module
  • Automatically updated by the default Input module
  • Allows you to check to see if a key has been released
  • Doesn't use a .dll file
  • Works with foreign-language keyboards
  • Doesn't crash on F12
  • Has the unique Anykey function, not found in any other script
  • Can check a key's toggle state. (Not found in any other script)
  • Can check more than one key at a time!

(Return to top)


Screenshots

Since this script has no visual elements, a screenshot cannot be taken.

(Return to top)


Script

  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 hidden]>
  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
Expand to see the code.


(Return to top)


Instructions

  • Place this script above Main, and below the default scripts. (I realize this is obvious to most, but some people don't get it.)
  • This module is automatically updated by the default Input module, which means that the only time you need to call the update method is in a scene that does not update the default Input module.
  • This module does not break the functionality of the default Input module.
  • If you wish to read keys from a gamepad, you must still use the default input module to do so.
  • To use this module, simply use one of the four methods (press?(key), trigger?(key), repeat?(key), or release?(key)), where key is the index of the key you want to check. Key may also be used as Keys::KEYNAME. For a list of acceptable key names, look below the header.
  • There is a key named ANYKEY. This key can be used like any other key, with the exception that, instead of corresponding to any one key, it reacts to them all. If you use it, and any other key would cause the same method to return true, then the ANYKEY will also return true.

(Return to top)


Method List

    Keys.update
Updates keyboard input. Calls to this method are not necessary unless the
default Input module is not being updated.


Keys.press?(key)
Determines whether the button determined by key is currently being
pressed. If the button is being pressed, returns true. If not, returns false.


Keys.trigger?(key)
Determines whether the button determined by key is being pressed again.
"Pressed again" is seen as time having passed between the button being
not pressed and being pressed. If the button is being pressed, returns true.
If not, returns false.


Keys.repeat?(key)
Determines whether the button determined by key is being pressed again.
Unlike trigger?(), this takes into account the repeat input of a button being
held down continuously. If the button is being pressed, returns true. If not,
returns false.


Keys.release?(key)
Determines whether the button determined by key has just been released. If
the button has been released, returns true. If not, returns false.


Keys.toggle?(key)
Determines whether the button determined by key has been toggled. This
functions like Caps Lock, Number Lock, and Scroll Lock, only for all keys.


Keys.array_press?(keys)
Functions in the same manner as Keys.press?(), only it takes an array of
keys as input.


Keys.array_trigger?(keys)
Functions in the same manner as Keys.trigger?(), only it takes an array of
keys as input.


Keys.array_repeat?(keys)
Functions in the same manner as Keys.repeat?(), only it takes an array of
keys as input.


Keys.array_release?(keys)
Functions in the same manner as Keys.release?(), only it takes an array of
keys as input.


Keys.array_toggle?(keys)
Functions in the same manner as Keys.toggle?(), only it takes an array of
keys as input.


Keys.capslock?
Determines whether the caps lock key is toggled on. This will always return
true when the corresponding keyboard light is lit, and false when it is not.


Keys.numlock?
Determines whether the number lock key is toggled on. This will always
return true when the corresponding keyboard light is lit, and false when it is
not.


Keys.scroll?
Determines whether the scroll lock key is toggled on. This will always return
true when the corresponding keyboard light is lit, and false when it is not.


Keys.character_press
Returns the currently pressed key, if it would generate text input. If no such
key would register as pressed, it returns an empty string.


Keys.character_trigger
Returns the currently triggered key, if it would generate text input. If no such
key would register as triggered, it returns an empty string.


Keys.character_repeat
Returns the currently repeated key, if it would generate text input. If no such
key would register as repeated, it returns an empty string.


Keys.get_character(key, scan_code)
Returns the character forresponding to the given key. If no character is
recognized by the system, it returns false instead. This method is intended
for internal use.


Keys.validate_key(key)
Checks the validity of a key. If a key variable can be used by this module, it
will return true. Otherwise, it will return false.


(Return to top)


Known Issues

This script has trouble with certain foreign language keyboards, where dead keys are shift sensitive. Dead keys are used to insert diacritical marks such as an accent. However, the correct character can be generated with creative use of shift and caps lock.

(Return to top)


License

This script is licensed under the MIT License:

The License wrote:
Copyright (c) 2010-2014 Sean Porter (Glitchfinder) <[email hidden]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


(Return to top)

I haven't really had the chance to mess with this yet, but skimming through it, appears you've done an excellent job! What I like about this script is it's compact (compared to Aleworks/AWorks) but is down to the point, and the coding is very clean and easy to read. More importantly, as this is what I've stressed over with other input modules, you say that it doesn't interfere with gamepad functionality or default inputs, just gives you full keyboard support, which I like the most about this!

I plan to deticate a few days to test this script, and evaluate it against others of its kind. When I get a chance, I'll let you know if I've encountered any bugs (with yours) or if there is any functions that others' have which your's doesn't.

While its fresh on my mind... Show
I think AWorks supported multiple simultaneous key presses/triggers/repeats, etc while the default Input module alone doesn't. Since I don't have RM on this particular computer, I haven't been able to test with your's yet, but now I'm curious if yours will allow me to.

ie, with my Run/Jump scripts, I can press [Run] + [Direction] then trigger [Jump] in that sequence with AWorks, but triggering [Jump] without AWorks doesn't respond very well if at all (although Run == Input::C and Jump == Input::A, both native to RM, so thats not the issue)

Until I can get to it, you should see what holding Input::A, any of the directional buttons, and then triggering Input::C do. Perhaps I was just having a bad day or something. Its been awhile since I've used RM, nonetheless without AWorks, that I forget what the default Input system is like compared to it.


Anyways, I'll be sure to check this out when I have a chance, I'm already excited you've went through with finishing and releasing this :thumb:
Thanks for the great comment! Regarding multiple key presses, it should support as many as your keyboard does. This script reads the state of the entire keyboard, instead of simply reading keys one by one. So, in theory, there shouldn't be a problem with more than one key at once.
Nice, but I think your update method is complex. First your are initializing 3 new arrays each frame. You should create a class variable you clear on the update.
No credits for original creators ? Cybersam or NearFantastica I think.
berka wrote:
Nice, but I think your update method is complex. First your are initializing 3 new arrays each frame. You should create a class variable you clear on the update.
No credits for original creators ? Cybersam or NearFantastica I think.

Clearing the arrays would take just as long or longer than simply making a new array. The update method is complex because the Win32API I used doesn't actually give the current key state, but instead will change each key from a 0 to a 1 and vice-versa when it gets pressed. (As opposed to having a 0 when not pressed, and a 1 when currently pressed)

As for credits, why would they be needed? I coded this script from scratch, and the only similarities would be in the key numbers, which are determined by Microsoft, and were taken from the MSDN database. Admittedly, I'm not the first one to come up with the idea for adding the key.released method, but, once again, I coded mine from scratch, instead of relying on somebody else's code.

Edit: I just looked up both CyberSam's and Near Fantastica's Keyboard Input Modules. They are both wildly different from mine, and apparently both have well known problems that I have yet to see in my module. (For example, CyberSam's module has trouble reading the keys that default RMXP Input module can read) The closest module to mine, function-wise, is AWorks, but that uses a .dll file to accomplish all of this, and it breaks the default Input module, requiring fixes to events and other scripts.

Edit: I just tested this in RMVX, and, to the best of my knowledge, it works perfectly in both VX and XP. I have changed the script header and post icon to reflect this.
I just took a more in-depth look at it, and well... I really like what I see ^^ Despite the large introduction block I don't really see a necessity for (not saying it's not helpful, but compared to the additional filesize of your Scripts.rxvdata files, keeping the instructions in the topic or on the webpage would've been enough... that's only an opinion, though), I don't really see much going the wrong way... your documentation is quite exhaustive (maybe a bit too much... talking specifically about the 'return key state' comments - in my opinion, putting the 'return' in there is enough information on what's happening, and whoever reads that and doesn't understand it won't be too happy with the given comment either... too much commenting kills readability again), your coding is clean, the functionality is great (talking about non-DLL-requiring, however I think you also have some keys in there that aren't too usuable; for example the varying-by-keyboard ones... it's very minor though).
The only problem I really see is performance, as with that giant update method and the multiple arrays there, the Interpreter really has to take a punch on each keypress... now for the code I see, that shouldn't be too heavy, as you're not drawing stufff or anything, but I can imagine some input processing errors on an already exhausted Interpreter from processing other scripts... haven't tested this, though.

Keep up the good work! ...while I take a look at your Mouse Input script...
Thanks for the great reply, BlueScope! Regarding the header for my script, I will admit that it's probably too much. (Except the license. That bit's essential, in my opinion) However, it will soon be dwindling in comparison to the size of the script itself, once I get around to adding a few planned methods and releasing the next update.

As for the unusable keys, it's because they do exist on at least some keyboards, and a game will generally be localized to a specific keyboard, which means that the person using the script will be the one to determine what those keys do if they're not using a US 101/102 keyboard. (Note that most of those keys are labeled as OEM keys, because they vary from location to location. The keys specifically designated by name should always have the same ID, regardless of keyboard origin)

And finally, your comment regarding processing time. Believe it or not, pressing a key, ten keys, or no keys will not make a difference to this script. Specifically, this is due to the Win32API method I called. What it does is give me a map of all keys on the keyboard, by ID. I then go through the map, and use it to fill several arrays. And while some values do depend on recent actions, it won't make a difference because it checks the action for every key, regardless of whether it actually happened. I've tested for lag as best I could, however, my computer is far too powerful for me to test properly. (Which is one reason I like it when theory is active. He has several computers, including a couple at just the right power to be good for testing lag)
Your syntax, commenting, and overall neatness is (in my opinion) excellent! I really appreciate commenting such as what can be found in your script... It greatly helps us beginning scripters learn more as we go along with ease. Please, keep it up!
Hi,
I've used your KIM script in my game and it's helpful.. but i still confused in using it. I'm not very good in scripting. When i used it in my game then i created an event to input the player's name (i set the ID is 1). So far so good. but after that when i called the name of player in text command with \n[1] it showed nothing.. just blank text. Can you help me?
Thanks.
My guess is that you didn't save the name properly... it's meant to replace the @name variable in $game_actor, meaning to change the first (database-wise) actor's name, you'd have to set $game_actors[1].name, to set the first (party-wise) actor's name, you' have to use $game_party.actors[0].name or $game_party.members[0].name (depending on your RPG Maker version).

Either way, this won't be affected by the Keyboard module unless you did something unexpected... if you share your code, we can help you further (along with the RPG Maker version you're using).
Ah yes.. i'm using RPG Maker VX. After adding KIM to the script editor i created an event to input character name, i just using 'name input processing' command where i set the actor ID to '001' with max character was 6. Here's the screenshot:
Image
The KIM has completely replace the default keyboard input, but the actor name is still not initialized yet after the command above was called. Is there something i miss?
I'm pretty sure that my script wouldn't be causing the problem, since it never modified Scene_Name, which is the portion of the default scripts that control the actor renaming process. However, I'll edit this post as soon as I've tested to make sure.

All right, I just tested, and the name input processing works like it normally does, so it shouldn't be my script that was keeping it from working properly. Have you tried removing other scripts to see if they were the cause?

I just took a closer look at your event, and realized that you were initializing the actor when you added him to your party. That would reset the actor to the state he's in in your database, including resetting his name to a blank name.
Yeah, you're right. I write the initializing actor command before name input command, and it's working. Thanks for your help.
All right. Gu574v0 forced my hand on this one, but I've had an update for this sitting around waiting for me to finish the header and post it. Has quite a bit in the way of new features, including the ones mostly superseded by Gu574v0's. My favorite is the Anykey. (For reasons obvious to any who hang out with me in IRC)
Glitchfinder wrote:
All right. Gu574v0 forced my hand on this one, but I've had an update for this sitting around waiting for me to finish the header and post it. Has quite a bit in the way of new features, including the ones mostly superseded by Gu574v0's. My favorite is the Anykey. (For reasons obvious to any who hang out with me in IRC)


Hey Glitch, your input module is my favorite. Nice work with the update. Yet, it ignored one key on my keyboard, with the ? and / characters, and it doesn't put together the accents with the letters (pressing ~ and A should type Ã). I should forgive you since English doesn't have any accents, but I have to show people my script isn't completely useless XD
Gu574v0 wrote:
Glitchfinder wrote:
All right. Gu574v0 forced my hand on this one, but I've had an update for this sitting around waiting for me to finish the header and post it. Has quite a bit in the way of new features, including the ones mostly superseded by Gu574v0's. My favorite is the Anykey. (For reasons obvious to any who hang out with me in IRC)


Hey Glitch, your input module is my favorite. Nice work with the update. Yet, it ignored one key on my keyboard, with the ? and / characters, and it doesn't put together the accents with the letters (pressing ~ and A should type Ã). I should forgive you since English doesn't have any accents, but I have to show people my script isn't completely useless XD


Odd. I know it handles the ´, `, and ^ characters in a German keyboard, and I set it to check for dead keys. I'll have to test further. As for the key it ignored, can you tell me what key it is, in my list of constants?
Glitchfinder wrote:
Odd. I know it handles the ´, `, and ^ characters in a German keyboard, and I set it to check for dead keys. I'll have to test further. As for the key it ignored, can you tell me what key it is, in my list of constants?


It's the 0xC1 key. It's not on your list of constants. The trigger/press/repeat methods all react to it, though (I found the value of the key by making ANYKEY return the index of the pressed key).
My Keyboard layout is "Portuguese (Brazilian ABNT)".
Gu574v0 wrote:
It's the 0xC1 key. It's not on your list of constants. The trigger/press/repeat methods all react to it, though (I found the value of the key by making ANYKEY return the index of the pressed key).
My Keyboard layout is "Portuguese (Brazilian ABNT)".


Interesting. The reason it's not on my list of constants is because it's in a series of keys that Microsoft marked as Reserved. I'll add those keys to the list of constants, as well as the array to check for input. As for the ~ key, what does that respond to? (Note that you have to press a dead key twice to get it to print anything.) Also, is that key shift sensitive, or simply unresponsive?
Glitchfinder wrote:
Gu574v0 wrote:
It's the 0xC1 key. It's not on your list of constants. The trigger/press/repeat methods all react to it, though (I found the value of the key by making ANYKEY return the index of the pressed key).
My Keyboard layout is "Portuguese (Brazilian ABNT)".


Interesting. The reason it's not on my list of constants is because it's in a series of keys that Microsoft marked as Reserved. I'll add those keys to the list of constants, as well as the array to check for input. As for the ~ key, what does that respond to? (Note that you have to press a dead key twice to get it to print anything.) Also, is that key shift sensitive, or simply unresponsive?


The ?/ key doesn't respond to the "character" methods any way, with or without shift and Alt-gr.
The all the accents key [~ ^], key [´ `] and key [6 ¨] give me the accent character (the shift status is taken into accout correctly), when pressed once, as if I have pressed the accent and then space.
Gu574v0 wrote:
The all the accents (...) give me the accent character (the shift status is taken into accout correctly), when pressed once, as if I have pressed the accent and then space.
Same for German keyboard mapping, however it does work when you press them at the very same frame (or very shortly after, resulting in `È, for example) for me. As you have different keys on your keyboard, would you mind testing that?
^, ~, ´ and ` work if I hold the accent key and then press the character. ¨ doesn't.
I'm using the character_trigger method. It returns the accent character once (when I press it), and then if I keep holding it and press the letter I want, it returns the correct character.
This works for ^, ~, ´ and ` but not for ¨ :huh:
Hi.
I'am new to rgss and i've got a question:
Can somebody give me a codeexample how to open with the letter "W" a common event?
I tried a

if keys.press?(0x57) then
print("Hallo Welt")
end

as a test but it gives me a error... :(
Toshi wrote:
Hi.
I'am new to rgss and i've got a question:
Can somebody give me a codeexample how to open with the letter "W" a common event?
I tried a

if keys.press?(0x57) then
print("Hallo Welt")
end

as a test but it gives me a error... :(


What error did it give?

Anyway, what you would want to do is set it up with a conditional branch, go to page four, and place the following bit of code into the script box.

  1. Keys.press?(Keys::W)
Expand to see the code.


Then you can proceed like it's a normal event.

Also, looking closer at what you typed, I can see why that would give an error. In Ruby, names are case sensitive, meaning that keys.press? would give an error while Keys.press? would not. If that wasn't your issue, I'm sure I'd be much more capable of helping once I know what the error message is.
Hey thanks a lot! It's working... Diden't know that it's case sensitive... I normaly code in vb.net...^^
All right guys, I have a new version! Enjoy!
I highly suggest that you make it easy to customize the repeat delay and repeat rate which will be very helpful for people changing the framerate.
It would be more awesome if you read the user specified repeat delay and rate and then use that to figure out how many frames that corresponds to relative to the framerate.

*hugs*
Zeriab wrote:
I highly suggest that you make it easy to customize the repeat delay and repeat rate which will be very helpful for people changing the framerate.
It would be more awesome if you read the user specified repeat delay and rate and then use that to figure out how many frames that corresponds to relative to the framerate.

*hugs*

Actually, that's a really good idea! I'll put that in my little book of things to do.
is it possible to change the picture of the mouse pointer?
instinct04 wrote:
is it possible to change the picture of the mouse pointer?


search for Amanda's (Lambchop, Amaranth) Commercial Game Kit,
or Shaz' Super Simple Mouse script
I've just updated this script to a new version. The condensed changelog is below.

Version 1.30:
  • Removed redundant type-checks originally intended for safety.
  • Moved some repeated code into a new function: Keys.validate_key
  • Changed the license to the MIT license for better ease of use.
  • Tested to ensure functionality in RPG Maker XP, RPG Maker VX, and RPG Maker VX Ace.
  • Heavily updated the format of the initial post.

The most important update here is that this script is now under the MIT license, which means that you're free to use it as you see fit so long as you include the license somewhere. It also means I can't see any legal repercussions for what you do with the script, since that's not under my control.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=71132