[VX/XP] PedroHLC's Input System (usesDLL+VKList+Window_Edit)

Introduction:
Hello, on Thursday(October 11, 2011) I wanted to practice a little more of using ruby variables on C, so I've done this Input System, it's made of four scripts:
  • Simple Input - A Input class that uses C++ to get all the keys pressed, mouse position, and some joystick buttons.
  • Virtual-Key Codes - A conversion of Microsoft Virtual Key List for Ruby.
  • Check Keys - A function that converts one key pressed into a char. (if needed, I convert it to C too)
  • Window_Edit - A Window to input some information, it has: a cursor, functions for the keys home, end, delete, left, right, and ctr+v if you have Aleworks library(this last function wasn't tested yet).

This Input is an Add-On for Enterbrain's Input, it doesn't remove or substitute any Enterbrain's methods, only add news methods.

Why should you use this?: Because it is fast and gets all the needed variables in only one Win32API command.

Scripts (in English):
Simple Input
  1. #==============================================================================
  2. # Name: Simple Input
  3. # Description: Add news methods to get any keyboard key, mouse position, and joysticks buttons
  4. #------------------------------------------------------------------------------
  5. # Developed by: PedroHLC
  6. # Version: 1.0
  7. #==============================================================================
  8.  
  9. Graphics.update
  10. $hWnd = Win32API.new('user32', 'GetActiveWindow', '', 'L').call
  11.  
  12. module Input
  13.   @key = Array.new(256, false)
  14.   @old_key = Array.new(256, false)
  15.   @state = Array.new(3, false)
  16.   @joystick = [0, 0, 0, 0] #Buttons, X, Y, POV
  17.   @mousepos = [0, 0] #x, y
  18.  
  19.   @cursor = Sprite.new
  20.   @cursor.z = 99999
  21.   @cursor.bitmap = RPG::Cache.icon('001-Weapon01')
  22.   @cursor_hide = false
  23.  
  24.   GETINPUT = Win32API.new('Input.dll', 'getInput', 'llllll', 'i')
  25.  
  26.   class << self
  27.     alias enterbrain_update update
  28.   end
  29.  
  30.   module_function
  31.    
  32.   def update
  33.     GETINPUT.call(@key.__id__, @old_key.__id__, @state.__id__,
  34.                   @joystick.__id__, @mousepos.__id__, $hWnd)
  35.     enterbrain_update
  36.    
  37.     if @cursor.visible = (!@cursor_hide)
  38.       @cursor.x = @mousepos[0]
  39.       @cursor.y = @mousepos[1]
  40.     end
  41.   end
  42.  
  43.   def getLockKeyState(key)
  44.     case key
  45.     when 0x14
  46.       return @state[0]
  47.     when 0x90
  48.       return @state[1]
  49.     when 0x91
  50.       return @state[2]
  51.     end
  52.     return false
  53.   end
  54.  
  55.   def getKeyState(key)
  56.     return @key[key]
  57.   end
  58.  
  59.   def getLastKeyState(key)
  60.     return @old_key[key]
  61.   end
  62.  
  63.   def getKeyPressed(key)
  64.     return (@key[key] and !@old_key[key])
  65.   end
  66.  
  67.   def getMousePos
  68.     return @mousepos
  69.   end
  70.  
  71.   def getJoystickButtons
  72.     return (@joystick[0].nil? ? @joystick[0] : 0)
  73.   end
  74.  
  75.   def setCursorHide(value)
  76.     @cursor_hide = value
  77.   end
  78.  
  79.   def setCursorBitmap(value)
  80.     @cursor.bitmap = value
  81.   end
  82.  
  83.   def isUpperingCase
  84.     return ( getKeyState(VK::SHIFT) | getLockKeyState(VK::CAPITAL) )
  85.   end
  86. end
Expand to see the code.

Virtual-Key Codes
  1. #==========================================================================
  2. # Name: Virtual-Key Codes
  3. # Description: List with keyboard keys codes (versão MSDN)
  4. #--------------------------------------------------------------------------
  5. # By: msdn.microsoft.com
  6. # Ripped by: PedroHLC
  7. # Last update: 11/10/10 - 22:00
  8. # Original state:
  9. # http://msdn.microsoft.com/en-us/library ... 85%29.aspx
  10. #==========================================================================
  11. module VK
  12.  
  13.   LBUTTON = 0x01 #Left mouse button
  14.   RBUTTON = 0x02 #Right mouse button
  15.   CANCEL = 0x03 #Control-break processing
  16.   MBUTTON = 0x04 #Middle mouse button (three-button mouse)
  17.   XBUTTON1 = 0x05 #X1 mouse button
  18.   XBUTTON2 = 0x06 #X2 mouse button
  19.   BACK = 0x08 #BACKSPACE key
  20.   TAB = 0x09 #TAB key
  21.   CLEAR = 0x0C #CLEAR key
  22.   RETURN = 0x0D #ENTER key
  23.   SHIFT = 0x10 #SHIFT key
  24.   CONTROL = 0x11 #CTRL key
  25.   MENU = 0x12 #ALT key
  26.   PAUSE = 0x13 #PAUSE key
  27.   CAPITAL = 0x14 #CAPS LOCK key
  28.   KANA = 0x15 #IME Kana mode
  29.   HANGUEL = 0x15 #IME Hanguel mode (maintained for compatibility; use HANGUL)
  30.   HANGUL = 0x15 #IME Hangul mode
  31.   JUNJA = 0x17 #IME Junja mode
  32.   FINAL = 0x18 #IME final mode
  33.   HANJA = 0x19 #IME Hanja mode
  34.   KANJI = 0x19 #IME Kanji mode
  35.   ESCAPE = 0x1B #ESC key
  36.   CONVERT = 0x1C #IME convert
  37.   NONCONVERT = 0x1D #IME nonconvert
  38.   ACCEPT = 0x1E #IME accept
  39.   MODECHANGE = 0x1F #IME mode change request
  40.   SPACE = 0x20 #SPACEBAR
  41.   PRIOR = 0x21 #PAGE UP key
  42.   NEXT = 0x22 #PAGE DOWN key
  43.   KEND = 0x23 #END key
  44.   HOME = 0x24 #HOME key
  45.   LEFT = 0x25 #LEFT ARROW key
  46.   UP = 0x26 #UP ARROW key
  47.   RIGHT = 0x27 #RIGHT ARROW key
  48.   DOWN = 0x28 #DOWN ARROW key
  49.   SELECT = 0x29 #SELECT key
  50.   PRINT = 0x2A #PRINT key
  51.   EXECUTE = 0x2B #EXECUTE key
  52.   SNAPSHOT = 0x2C #PRINT SCREEN key
  53.   INSERT = 0x2D #INS key
  54.   DELETE = 0x2E #DEL key
  55.   HELP = 0x2F #HELP key
  56.   KEY_0 = 0x30 #0 key
  57.   KEY_1 = 0x31 #1 key
  58.   KEY_2 = 0x32 #2 key
  59.   KEY_3 = 0x33 #3 key
  60.   KEY_4 = 0x34 #4 key
  61.   KEY_5 = 0x35 #5 key
  62.   KEY_6 = 0x36 #6 key
  63.   KEY_7 = 0x37 #7 key
  64.   KEY_8 = 0x38 #8 key
  65.   KEY_9 = 0x39 #9 key
  66.   A = 0x41 #A key
  67.   B = 0x42 #B key
  68.   C = 0x43 #C key
  69.   D = 0x44 #D key
  70.   E = 0x45 #E key
  71.   F = 0x46 #F key
  72.   G = 0x47 #G key
  73.   H = 0x48 #H key
  74.   I = 0x49 #I key
  75.   J = 0x4A #J key
  76.   K = 0x4B #K key
  77.   L = 0x4C #L key
  78.   M = 0x4D #M key
  79.   N = 0x4E #N key
  80.   O = 0x4F #O key
  81.   P = 0x50 #P key
  82.   Q = 0x51 #Q key
  83.   R = 0x52 #R key
  84.   S = 0x53 #S key
  85.   T = 0x54 #T key
  86.   U = 0x55 #U key
  87.   V = 0x56 #V key
  88.   W = 0x57 #W key
  89.   X = 0x58 #X key
  90.   Y = 0x59 #Y key
  91.   Z = 0x5A #Z key
  92.   LWIN = 0x5B #Left Windows key (Natural keyboard)
  93.   RWIN = 0x5C #Right Windows key (Natural keyboard)
  94.   APPS = 0x5D #Applications key (Natural keyboard)
  95.   SLEEP = 0x5F #Computer Sleep key
  96.   NUMPAD0 = 0x60 #Numeric keypad 0 key
  97.   NUMPAD1 = 0x61 #Numeric keypad 1 key
  98.   NUMPAD2 = 0x62 #Numeric keypad 2 key
  99.   NUMPAD3 = 0x63 #Numeric keypad 3 key
  100.   NUMPAD4 = 0x64 #Numeric keypad 4 key
  101.   NUMPAD5 = 0x65 #Numeric keypad 5 key
  102.   NUMPAD6 = 0x66 #Numeric keypad 6 key
  103.   NUMPAD7 = 0x67 #Numeric keypad 7 key
  104.   NUMPAD8 = 0x68 #Numeric keypad 8 key
  105.   NUMPAD9 = 0x69 #Numeric keypad 9 key
  106.   MULTIPLY = 0x6A #Multiply key
  107.   ADD = 0x6B #Add key
  108.   SEPARATOR = 0x6C #Separator key
  109.   SUBTRACT = 0x6D #Subtract key
  110.   DECIMAL = 0x6E #Decimal key
  111.   DIVIDE = 0x6F #Divide key
  112.   F1 = 0x70 #F1 key
  113.   F2 = 0x71 #F2 key
  114.   F3 = 0x72 #F3 key
  115.   F4 = 0x73 #F4 key
  116.   F5 = 0x74 #F5 key
  117.   F6 = 0x75 #F6 key
  118.   F7 = 0x76 #F7 key
  119.   F8 = 0x77 #F8 key
  120.   F9 = 0x78 #F9 key
  121.   F10 = 0x79 #F10 key
  122.   F11 = 0x7A #F11 key
  123.   F12 = 0x7B #F12 key
  124.   F13 = 0x7C #F13 key
  125.   F14 = 0x7D #F14 key
  126.   F15 = 0x7E #F15 key
  127.   F16 = 0x7F #F16 key
  128.   F17 = 0x80 #F17 key
  129.   F18 = 0x81 #F18 key
  130.   F19 = 0x82 #F19 key
  131.   F20 = 0x83 #F20 key
  132.   F21 = 0x84 #F21 key
  133.   F22 = 0x85 #F22 key
  134.   F23 = 0x86 #F23 key
  135.   F24 = 0x87 #F24 key
  136.   NUMLOCK = 0x90 #NUM LOCK key
  137.   SCROLL = 0x91 #SCROLL LOCK key
  138.   LSHIFT = 0xA0 #Left SHIFT key
  139.   RSHIFT = 0xA1 #Right SHIFT key
  140.   LCONTROL = 0xA2 #Left CONTROL key
  141.   RCONTROL = 0xA3 #Right CONTROL key
  142.   LMENU = 0xA4 #Left MENU key
  143.   RMENU = 0xA5 #Right MENU key
  144.   BROWSER_BACK = 0xA6 #Browser Back key
  145.   BROWSER_FORWARD = 0xA7 #Browser Forward key
  146.   BROWSER_REFRESH = 0xA8 #Browser Refresh key
  147.   BROWSER_STOP = 0xA9 #Browser Stop key
  148.   BROWSER_SEARCH = 0xAA #Browser Search key
  149.   BROWSER_FAVORITES = 0xAB #Browser Favorites key
  150.   BROWSER_HOME = 0xAC #Browser Start and Home key
  151.   VOLUME_MUTE = 0xAD #Volume Mute key
  152.   VOLUME_DOWN = 0xAE #Volume Down key
  153.   VOLUME_UP = 0xAF #Volume Up key
  154.   MEDIA_NEXT_TRACK = 0xB0 #Next Track key
  155.   MEDIA_PREV_TRACK = 0xB1 #Previous Track key
  156.   MEDIA_STOP = 0xB2 #Stop Media key
  157.   MEDIA_PLAY_PAUSE = 0xB3 #Play/Pause Media key
  158.   LAUNCH_MAIL = 0xB4 #Start Mail key
  159.   LAUNCH_MEDIA_SELECT = 0xB5 #Select Media key
  160.   LAUNCH_APP1 = 0xB6 #Start Application 1 key
  161.   LAUNCH_APP2 = 0xB7 #Start Application 2 key
  162.   OEM_1 = 0xBA #Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key
  163.   OEM_PLUS = 0xBB #For any country/region, the '+' key
  164.   OEM_COMMA = 0xBC #For any country/region, the ',' key
  165.   OEM_MINUS = 0xBD #For any country/region, the '-' key
  166.   OEM_PERIOD = 0xBE #For any country/region, the '.' key
  167.   OEM_2 = 0xBF #Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key
  168.   OEM_3 = 0xC0 #Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key
  169.   OEM_4 = 0xDB #Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key
  170.   OEM_5 = 0xDC #Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\|' key
  171.   OEM_6 = 0xDD #Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key
  172.   OEM_7 = 0xDE #Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the 'single-quote/double-quote' key
  173.   OEM_8 = 0xDF #Used for miscellaneous characters; it can vary by keyboard.
  174.   OEM_102 = 0xE2 #Either the angle bracket key or the backslash key on the RT 102-key keyboard
  175.   PROCESSKEY = 0xE5 #IME PROCESS key
  176.   PACKET = 0xE7 #Used to pass Unicode characters as if they were keystrokes. The PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input method
  177.   ATTN = 0xF6 #Attn key
  178.   CRSEL = 0xF7 #CrSel key
  179.   EXSEL = 0xF8 #ExSel key
  180.   EREOF = 0xF9 #Erase EOF key
  181.   PLAY = 0xFA #Play key
  182.   ZOOM = 0xFB #Zoom key
  183.   NONAME = 0xFC #Reserved
  184.   PA1 = 0xFD #PA1 key
  185.   OEM_CLEAR = 0xFE #Clear key
  186. end
Expand to see the code.

Check Keys
  1. #==============================================================================
  2. # Name: Check Keys
  3. # Description: Converts a pressed key in a char
  4. #------------------------------------------------------------------------------
  5. # Developed by: PedroHLC
  6. # Version: 1.4
  7. #==============================================================================
  8.  
  9. module Input
  10.   module_function
  11.   # Checks all keyboards keys and returns it char if was it pressed
  12.   def check_keys(can_letters=true, can_numbers=true, can_symbol=true)
  13.     return " " if getKeyPressed(VK::SPACE)
  14.    
  15.     up = isUpperingCase
  16.    
  17.     if can_letters == true
  18.       if up
  19.         return "A" if getKeyPressed(VK::A)
  20.         return "B" if getKeyPressed(VK::B)
  21.         return "C" if getKeyPressed(VK::C)
  22.         return "D" if getKeyPressed(VK::D)
  23.         return "E" if getKeyPressed(VK::E)
  24.         return "F" if getKeyPressed(VK::F)
  25.         return "G" if getKeyPressed(VK::G)
  26.         return "H" if getKeyPressed(VK::H)
  27.         return "I" if getKeyPressed(VK::I)
  28.         return "J" if getKeyPressed(VK::J)
  29.         return "K" if getKeyPressed(VK::K)
  30.         return "L" if getKeyPressed(VK::L)
  31.         return "M" if getKeyPressed(VK::M)
  32.         return "N" if getKeyPressed(VK::N)
  33.         return "O" if getKeyPressed(VK::O)
  34.         return "P" if getKeyPressed(VK::P)
  35.         return "Q" if getKeyPressed(VK::Q)
  36.         return "R" if getKeyPressed(VK::R)
  37.         return "S" if getKeyPressed(VK::S)
  38.         return "T" if getKeyPressed(VK::T)
  39.         return "U" if getKeyPressed(VK::U)
  40.         return "V" if getKeyPressed(VK::V)
  41.         return "W" if getKeyPressed(VK::W)
  42.         return "X" if getKeyPressed(VK::X)
  43.         return "Y" if getKeyPressed(VK::Y)
  44.         return "Z" if getKeyPressed(VK::Z)
  45.       else
  46.         return "a" if getKeyPressed(VK::A)
  47.         return "b" if getKeyPressed(VK::B)
  48.         return "c" if getKeyPressed(VK::C)
  49.         return "d" if getKeyPressed(VK::D)
  50.         return "e" if getKeyPressed(VK::E)
  51.         return "f" if getKeyPressed(VK::F)
  52.         return "g" if getKeyPressed(VK::G)
  53.         return "h" if getKeyPressed(VK::H)
  54.         return "i" if getKeyPressed(VK::I)
  55.         return "j" if getKeyPressed(VK::J)
  56.         return "k" if getKeyPressed(VK::K)
  57.         return "l" if getKeyPressed(VK::L)
  58.         return "m" if getKeyPressed(VK::M)
  59.         return "n" if getKeyPressed(VK::N)
  60.         return "o" if getKeyPressed(VK::O)
  61.         return "p" if getKeyPressed(VK::P)
  62.         return "q" if getKeyPressed(VK::Q)
  63.         return "r" if getKeyPressed(VK::R)
  64.         return "s" if getKeyPressed(VK::S)
  65.         return "t" if getKeyPressed(VK::T)
  66.         return "u" if getKeyPressed(VK::U)
  67.         return "v" if getKeyPressed(VK::V)
  68.         return "w" if getKeyPressed(VK::W)
  69.         return "x" if getKeyPressed(VK::X)
  70.         return "y" if getKeyPressed(VK::Y)
  71.         return "z" if getKeyPressed(VK::Z)
  72.       end
  73.     end
  74.    
  75.     if can_numbers == true
  76.       if !up
  77.         return "0" if getKeyPressed(VK::KEY_0)
  78.         return "1" if getKeyPressed(VK::KEY_1)
  79.         return "2" if getKeyPressed(VK::KEY_2)
  80.         return "3" if getKeyPressed(VK::KEY_3)
  81.         return "4" if getKeyPressed(VK::KEY_4)
  82.         return "5" if getKeyPressed(VK::KEY_5)
  83.         return "6" if getKeyPressed(VK::KEY_6)
  84.         return "7" if getKeyPressed(VK::KEY_7)
  85.         return "8" if getKeyPressed(VK::KEY_8)
  86.         return "9" if getKeyPressed(VK::KEY_9)
  87.       end
  88.       if getLockKeyState(VK::NUMLOCK)
  89.         return "0" if getKeyPressed(VK::NUMPAD0)
  90.         return "1" if getKeyPressed(VK::NUMPAD1)
  91.         return "2" if getKeyPressed(VK::NUMPAD2)
  92.         return "3" if getKeyPressed(VK::NUMPAD3)
  93.         return "4" if getKeyPressed(VK::NUMPAD4)
  94.         return "5" if getKeyPressed(VK::NUMPAD5)
  95.         return "6" if getKeyPressed(VK::NUMPAD6)
  96.         return "7" if getKeyPressed(VK::NUMPAD7)
  97.         return "8" if getKeyPressed(VK::NUMPAD8)
  98.         return "9" if getKeyPressed(VK::NUMPAD9)
  99.       end
  100.     end
  101.    
  102.     if can_symbol == true
  103.       if !up
  104.         return ";" if getKeyPressed(VK::OEM_1)
  105.         return "'" if getKeyPressed(VK::OEM_7)
  106.       else
  107.         return ":" if getKeyPressed(VK::OEM_1)
  108.         return "\"" if getKeyPressed(VK::OEM_7)
  109.       end
  110.       return "!" if getKeyPressed(VK::KEY_1)
  111.       return "@" if getKeyPressed(VK::KEY_2)
  112.       return "#" if getKeyPressed(VK::KEY_3)
  113.       return "$" if getKeyPressed(VK::KEY_4)
  114.       return "%" if getKeyPressed(VK::KEY_5)
  115.       return "¨" if getKeyPressed(VK::KEY_6)
  116.       return "&" if getKeyPressed(VK::KEY_7)
  117.       return "*" if getKeyPressed(VK::KEY_8)
  118.       return "(" if getKeyPressed(VK::KEY_9)
  119.       return ")" if getKeyPressed(VK::KEY_0)
  120.       return "[" if getKeyPressed(VK::OEM_4)
  121.       return "]" if getKeyPressed(VK::OEM_6)
  122.       return "\/" if getKeyPressed(VK::OEM_5)
  123.       return "*" if getKeyPressed(VK::MULTIPLY)
  124.       return "+" if getKeyPressed(VK::ADD)
  125.       return "," if getKeyPressed(VK::SEPARATOR)
  126.       return "-" if getKeyPressed(VK::SUBTRACT)
  127.       return "." if getKeyPressed(VK::DECIMAL)
  128.       return "/" if getKeyPressed(VK::DIVIDE)
  129.     end
  130.    
  131.     return ""
  132.   end
  133. end
Expand to see the code.

Window_Edit
  1. #===============================================================================
  2. # Name: Window_Edit
  3. # Description: A window to type text/informations
  4. #------------------------------------------------------------------------------
  5. # Developed by: PedroHLC
  6. # Version: 1.2
  7. #===============================================================================
  8. class Window_Edit < Window_Base
  9.   attr_accessor :index
  10.   def initialize(x, y, width, height, max, password=false)
  11.     @text_width = (width - 32)
  12.     @text_height = (height - 32)
  13.     super(x, y, width, height)
  14.     self.contents = Bitmap.new(width - 32, height - 32)
  15.     self.contents.font.size = 16
  16.     self.back_opacity = 160
  17.     @max = max
  18.     @text = []
  19.     @index = 0
  20.     @blink_time = 0
  21.     @password = password
  22.     refresh
  23.   end
  24.   def text
  25.     final_text = ""
  26.     for i in 0..(@text.size-1)
  27.       if @text[i] != nil
  28.         final_text += @text[i].to_s
  29.       end
  30.     end
  31.     return final_text
  32.   end
  33.   def text=(value)
  34.     @text = value.split
  35.   end
  36.   def refresh
  37.     if @blink_time <= -20
  38.       @blink_time = 10
  39.     else
  40.       @blink_time -= 1
  41.     end
  42.     @cursor_char = "|"
  43.     cursorx = 0
  44.     if @password
  45.       see_text = Array.new(@text.size, '*')
  46.     else
  47.       see_text = @text
  48.     end
  49.     if @index > 0
  50.       for i in 0..(see_text.size-1)
  51.         if i == (@index-1)
  52.           cursorx += contents.text_size(see_text[i]).width
  53.           break
  54.         else
  55.           cursorx += contents.text_size(see_text[i]).width
  56.         end
  57.       end
  58.     end
  59.     self.contents.clear
  60.     self.contents.draw_text(1, 0, @text_width, @text_height, see_text.to_s)
  61.     if @blink_time <= 0
  62.       self.contents.draw_text(cursorx, 0, 4, @text_height, '|')
  63.     end
  64.   end
  65.   def add(char)
  66.     return if !char.is_a?(String)
  67.     return if char.to_s == nil.to_s
  68.     temp = []
  69.     for i in 0..(@text.size - 1)
  70.       if i >= @index
  71.         temp[i+1] = @text[i] if @text[i].to_s != nil.to_s
  72.       elsif i < @index
  73.         temp[i] = @text[i] if @text[i].to_s != nil.to_s
  74.       end
  75.     end
  76.     temp[(@index)] = (char.to_s)
  77.     @text = temp
  78.     @index += 1
  79.   end
  80.   def add_array(array)
  81.     return if !array.is_a?(Array)
  82.     if @index >= @text.size
  83.       @text.push(array)
  84.       @index = @text.size
  85.     else
  86.       temp1 = []
  87.       temp2 = []
  88.       for i in 0..(@text.size - 1)
  89.         if i < @index
  90.           temp1.push(@text[i]) if @text[i].to_s != nil.to_s
  91.         elsif i >= @index
  92.           temp2.push(@text[i]) if @text[i].to_s != nil.to_s
  93.         end
  94.       end
  95.       for i in 0..(array.size - 1)
  96.         temp1.push(array[i]) if array[i].to_s != nil.to_s
  97.       end
  98.       for i in 0..(temp2.size - 1)
  99.         temp1.push(temp2[i]) if temp2[i].to_s != nil.to_s
  100.       end
  101.       @index += array.size
  102.       @text = temp1
  103.     end
  104.   end
  105.   def clear
  106.     @text = []
  107.     @index = 0
  108.   end
  109.   def onReturn
  110.     @text.clear
  111.   end
  112.   def update
  113.     if Input.getKeyPressed(VK::RETURN)
  114.       onReturn
  115.     end
  116.     if Input.getKeyPressed(VK::BACK)
  117.       if @text.size.to_i <= 0 or @index <= 0
  118.         $game_system.se_play($data_system.buzzer_se)
  119.       else
  120.         @text.delete_at(@index-1)
  121.         @index -= 1 if @index >= 1
  122.       end
  123.     end
  124.     if Input.getKeyPressed(VK::DELETE)
  125.       if @text.size.to_i <= 0 or @text[@index] == nil
  126.         $game_system.se_play($data_system.buzzer_se)
  127.       else
  128.         @text.delete_at(@index)
  129.       end
  130.     end
  131.     #if Input.getKeyState(VK::CONTROL) and Input.getKeyPressed(VK::V)
  132.       #clipboard = Aleworks::Clipboard.read.to_s.strip.split(//)
  133.       #if ([email hidden]_s.size) < @max
  134.         #add_array(clipboard)
  135.         #return
  136.       #else
  137.         #$game_system.se_play($data_system.buzzer_se)
  138.       #end
  139.     #end
  140.     char = Input.check_keys(true,true,true)
  141.     if @text.to_s.size < @max
  142.       add(char) if char != nil
  143.     else
  144.       $game_system.se_play($data_system.buzzer_se)
  145.     end
  146.     if Input.getKeyPressed(VK::LEFT)
  147.       if @index <= 0
  148.         $game_system.se_play($data_system.buzzer_se)
  149.       else
  150.         $game_system.se_play($data_system.cursor_se)
  151.         @index -= 1
  152.       end
  153.     end
  154.     if Input.getKeyPressed(VK::RIGHT)
  155.       if @index >= @text.size
  156.         $game_system.se_play($data_system.buzzer_se)
  157.       else
  158.         $game_system.se_play($data_system.cursor_se)
  159.         @index += 1
  160.       end
  161.     end
  162.     #print @text if Input.getKeyPressed(VK::UP)
  163.     if Input.getKeyPressed(VK::KEND)
  164.       $game_system.se_play($data_system.cursor_se)
  165.       @index = @text.size
  166.     end
  167.     if Input.getKeyPressed(VK::HOME)
  168.       $game_system.se_play($data_system.cursor_se)
  169.       @index = 0
  170.     end
  171.     if @index <= 0
  172.       @index = 0
  173.     elsif @index >= @text.size
  174.       @index = @text.size
  175.     end
  176.     refresh
  177.   end
  178. end
Expand to see the code.


DLL Source:
  1. //Developed by PedroHLC
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <Mmsystem.h>
  6. #include <windows.h>
  7.  
  8. #define DLLEXTERN extern "C" __declspec (dllexport)
  9.  
  10. #define Qfalse ((VALUE)0)
  11. #define Qtrue  ((VALUE)2)
  12. #define Qnil   4
  13. //#define NIL_P(v) ((VALUE)(v) == Qnil)
  14. #define FIXNUM_FLAG 0x01
  15. #define INT2FIX(i) ((VALUE)(((long)(i))<<1 | FIXNUM_FLAG))
  16.  
  17.  
  18. typedef unsigned long VALUE;
  19.  
  20. typedef struct{
  21.     unsigned long flags;
  22.     VALUE klass;
  23. } RBasic;
  24.  
  25. typedef struct{
  26.     RBasic basic;
  27.     long len;
  28.     union {
  29.            long capa;
  30.            VALUE shared;
  31.     } aux;
  32.     VALUE *ptr;
  33. } RArray;
  34.  
  35. DLLEXTERN int getInput (long rbkeys_id,
  36.                         long rboldkeys_id,
  37.                         long rbstates_id,
  38.                         long rbjoystick_id,
  39.                         long rbmousepos_id,
  40.                      long windowid){
  41.                
  42.         VALUE *rbkeys_arr = ((RArray*)(rbkeys_id << 1))->ptr;
  43.         VALUE *rboldkeys_arr = ((RArray*)(rboldkeys_id << 1))->ptr;
  44.         VALUE *rbstates_arr = ((RArray*)(rbstates_id << 1))->ptr;
  45.         VALUE *rbjoysticks_arr = ((RArray*)(rbjoystick_id << 1))->ptr;
  46.         VALUE *rbmousepos_arr = ((RArray*)(rbmousepos_id << 1))->ptr;
  47.  
  48.         //Old Keys
  49.         for (int keycode = 0; keycode < 256; keycode++){
  50.             if (rbkeys_arr[keycode] == Qtrue)
  51.                 rboldkeys_arr[keycode] = Qtrue;
  52.             else
  53.                 rboldkeys_arr[keycode] = Qfalse;
  54.         }
  55.  
  56.         //Keys
  57.         for (int keycode = 0; keycode < 256; keycode++){
  58.             if (GetAsyncKeyState(keycode) >> 8)
  59.                 rbkeys_arr[keycode] = Qtrue;
  60.             else
  61.                 rbkeys_arr[keycode] = Qfalse;
  62.         }
  63.        
  64.         //States
  65.         rbstates_arr[0] = (GetKeyState(0x14)? Qtrue : Qfalse);
  66.         rbstates_arr[1] = (GetKeyState(0x90)? Qtrue : Qfalse);
  67.         rbstates_arr[2] = (GetKeyState(0x91)? Qtrue : Qfalse);
  68.  
  69.         //Mouse
  70.         POINT input_MousePos;
  71.         GetCursorPos(&input_MousePos);
  72.         HWND hWnd = (HWND)windowid;
  73.         ScreenToClient(hWnd, &input_MousePos);
  74.        
  75.         rbmousepos_arr[0] = INT2FIX(input_MousePos.x);
  76.         rbmousepos_arr[1] = INT2FIX(input_MousePos.y);
  77.  
  78.         //Joystick
  79.         if (joyGetNumDevs() > 0){
  80.             JOYINFOEX input_JoypadInfo;
  81.             input_JoypadInfo.dwSize = 52;
  82.             input_JoypadInfo.dwFlags = 255;
  83.             if (joyGetPosEx(0, &input_JoypadInfo) != JOYERR_UNPLUGGED){
  84.                 rbjoysticks_arr[0] = INT2FIX(input_JoypadInfo.dwButtons);
  85.                 rbjoysticks_arr[1] = INT2FIX(input_JoypadInfo.dwXpos);
  86.                 rbjoysticks_arr[2] = INT2FIX(input_JoypadInfo.dwYpos);
  87.                 rbjoysticks_arr[3] = INT2FIX(input_JoypadInfo.dwPOV);
  88.             }else{
  89.                 rbjoysticks_arr[0] = Qnil;
  90.                 rbjoysticks_arr[1] = Qnil;
  91.                 rbjoysticks_arr[2] = Qnil;
  92.                 rbjoysticks_arr[3] = Qnil;
  93.             }
  94.         }
  95.        
  96.         return 0;
  97. }
Expand to see the code.


Demo (in Portuguese):
Download using DROPBOX.COM

Compiled DLL included in Demo. Not tested yet on RMVX.
As you seen this is a simple and common script, that's why I'll only makes update when it's necessary; anyway thanks for the interesting on it. 😀
While I applaud the work that went into this, I do have a complaint about it as well. This is primarily an English speaking forum, with many members in both the United States and Great Britain. The settings you have for the key checks are not the same as what these two groups would expect. For example, I'll list the differences between a USA 101 keyboard and what you have there.

The first one I noticed was your OEM_7 key. The unshifted (effectively, lowercase) character was "'", which is correct for this 101 keyboard. However, the shifted character was "\", which is another key entirely for us. (For locations on the keyboard, I believe OEM_7 is directly to the left of the return key, while the key that 101 keyboards use for "\" is OEM_5, which is directly above the return key, and features "\" as the unshifted character and "|" as the shifted character. The OEM_7 key features '"' as the shifted character for users of a USA 101 keyboard) The next thing I noted was the shifted character for KEY_6, which was "¨" for you, but is actually "^" for us. Next up, is the fact that you did not feature shifted characters for OEM_4, OEM_6, or OEM_5, ("{", "}", and "|", respectively), and that you failed to include OEM_2 ("/" unshifted, "?" shifted), OEM_3 ("`" unshifted, "~" shifted, although the shifted value for standard UK keyboards is actually different), OEM_PLUS ("=" unshifted, "+" shifted), OEM_COMMA ("," unshifted, "<" shifted), OEM_MINUS ("-" unshifted, "_" shifted), or OEM_PERIOD ("." unshifted, ">" shifted).

Now, I'm not saying this to be mean, rude, or anything like that. In the case of the missing keys I listed, I'm pointing them out because I would find it difficult to perform some input tasks without them, even in a game. And, even with a limited input system like many games offer for naming characters, you typically see several of those missing keys as available characters, especially the "-" and "_" characters. I'm also pointing out the differences because, as I said, this is a primarily English forum, and the format you have is not a primarily English format, or at the least, not a particularly common one here.

Now, before you take this the wrong way, I've gone through this with my own input script. I still have transcripts of conversations with BlueScope, a resident scripter who lives in Germany and uses a German format keyboard. My final conclusion with those conversations was that either I could either perfectly simulate a single keyboard, or I could do my best to simulate all keyboards by using an alternative method. I ended up choosing the alternative method, which was using native Windows calls to convert keycodes to their unicode representation according to the currently set system keyboard. And even then, I still had issues with dead keys (which are used in certain languages for things like adding accents to certain characters) and I also had issues where it could not properly read input from any keyboard format that used multiple keystrokes to define a single character. (Like Kana input, for example)

Anyway, that's about all I can say about it, aside from the fact that this appears to only read currently pressed keys when you check, which is terribly limited compared to most input systems I have seen for RMXP and RMVX. As it is, take what I said with a grain of salt, since I've moved on from RM* scripting and currently do most of my programming in C, C++, Java, and PHP, depending on what's currently needed.

Edit: One more thing that I would like to mention. You can actually simulate input from keyboards you don't own by setting the system keyboard to something else using the control panel, and then bringing up the On-Screen Keyboard, which can be found in the accessibility menu (under accessories) in your start menu.
Sure this is a forum with generally English population of people, and it is true that those keys are missing, but UNLESS you are making an online game which is the only kind of game where I would find those keys actually necessary, they are not actually needed. If I had to guess I think Pedro wanted to do a simple input system to allow people to make custom name input scripts or something like that.

In the end it all boils down to whether people are gonna use it or not. And as I said, this script is ok, without those missing keys, but not for online games or something that actually requires you to input those characters. :/
No, you're not quite getting the point of what Glitch was trying to say. For me, several remotely important characters such as -, _, + and whatnot were non-functional with his initial version. Now, you might of course say they're not needed, but I think Glitchs point is (if not it's still mine) that if you make an input script, don't make one that supports only 60% of the keyboards out there, and everyone else "better not use those keys". I mean, if those special keys are only needed for special games, than I guess normal games only need normal keys. Well, normal keys are supported by default...

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