Disable gamepad

By Zeriab, 1 Month ago, written in ruby.
  1. module Utility
  2.   #############
  3.   # DLL STUFF #
  4.   #############
  5.   READ_REGISTRY    = Win32API.new('advapi32',  'RegGetValue',
  6.                                   %w(l p p l p p p), 'l')
  7.  
  8.   HKEY_CURRENT_USER = 1-0x80000000
  9.  
  10.   ##
  11.   # Read controls from registry
  12.   #
  13.   def self.read_controls
  14.     hkey = HKEY_CURRENT_USER
  15.     subkey = 'Software\Enterbrain\RGSS'
  16.     value = 'ButtonAssign'
  17.     type = 0x00000008 # Binary
  18.     buftype = "\0" * 5
  19.     buffer_size = 256
  20.     buffer = "\0" * buffer_size
  21.     size = [buffer_size-1].pack("L")
  22.     res = READ_REGISTRY.call(hkey, subkey, value, type, buftype, buffer, size)
  23.     size = size.unpack("L")
  24.     return buffer[0...size[0]].unpack("c*")
  25.   end
  26.  
  27.   def self.read_smoothmode
  28.     read_dword('SmoothMode') == 1
  29.   end
  30.  
  31.   def self.read_waitforvsync
  32.     read_dword('WaitForVsync') == 1
  33.   end
  34.  
  35.   def self.read_musicplaying
  36.     read_dword('PlayMusic') == 1
  37.   end
  38.  
  39.   def self.read_soundplaying
  40.     read_dword('PlaySound') == 1
  41.   end
  42.  
  43.   ##
  44.   # Reading a given dword
  45.   #
  46.   def self.read_dword(value)
  47.     hkey = HKEY_CURRENT_USER
  48.     subkey = 'Software\Enterbrain\RGSS'
  49.     type = 0x00000008 # Binary
  50.     buftype = "\0" * 5
  51.     buffer_size = 256
  52.     buffer = "\0" * buffer_size
  53.     size = [buffer_size-1].pack("L")
  54.     res = READ_REGISTRY.call(hkey, subkey, value, type, buftype, buffer, size)
  55.     size = size.unpack("L")
  56.     return buffer[0...size[0]].unpack("L")[0]
  57.   end
  58. end
  59.  
  60. module Input
  61.   VK_RETURN   = 0x0D # ENTER
  62.   VK_SHIFT    = 0x10
  63.   VK_CONTROL  = 0x11 # CTRL
  64.   VK_MENU     = 0x12 # ALT
  65.   VK_ESCAPE   = 0x1B # ESC
  66.   VK_SPACE    = 0x20 # SPACEBAR
  67.   VK_PRIOR    = 0x21 # PAGE UP
  68.   VK_NEXT     = 0x22 # PAGE DOWN
  69.   VK_LEFT     = 0x25 # LEFT ARROW
  70.   VK_UP       = 0x26 # UP ARROW
  71.   VK_RIGHT    = 0x27 # RIGHT ARROW
  72.   VK_DOWN     = 0x28 # DOWN ARROW
  73.   VK_A        = 0x41
  74.   VK_B        = 0x42
  75.   VK_C        = 0x43
  76.   VK_D        = 0x44
  77.   VK_Q        = 0x51
  78.   VK_S        = 0x53
  79.   VK_V        = 0x56
  80.   VK_W        = 0x57
  81.   VK_X        = 0x58
  82.   VK_Z        = 0x5A
  83.   VK_NUMPAD0  = 0x60 # Num0
  84.   VK_F5       = 0x74
  85.   VK_F6       = 0x75
  86.   VK_F7       = 0x76
  87.   VK_F8       = 0x77
  88.   VK_F9       = 0x78
  89.  
  90.   KEYBOARD_TRIGGERS = [VK_RETURN, VK_SHIFT, VK_CONTROL, VK_MENU, VK_ESCAPE,
  91.       VK_SPACE, VK_PRIOR, VK_NEXT, VK_LEFT, VK_UP, VK_DOWN, VK_RIGHT,
  92.       VK_A, VK_B, VK_C, VK_D, VK_Q, VK_S, VK_V, VK_W, VK_X, VK_Z,
  93.       VK_NUMPAD0, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9]
  94.      
  95.   BUTTONS = Utility.read_controls
  96.   BUTTON_MAPPING = {
  97.       VK_RETURN => BUTTONS[11],
  98.       VK_SHIFT => [BUTTONS[14], SHIFT],
  99.       VK_CONTROL => CTRL,
  100.       VK_MENU => ALT,
  101.       VK_ESCAPE => BUTTONS[12],
  102.       VK_SPACE => BUTTONS[10],
  103.       VK_PRIOR => L,
  104.       VK_NEXT => R,
  105.       VK_LEFT => LEFT,
  106.       VK_UP => UP,
  107.       VK_DOWN => DOWN,
  108.       VK_RIGHT => RIGHT,
  109.       VK_A => BUTTONS[20],
  110.       VK_B => BUTTONS[19],
  111.       VK_C => BUTTONS[17],
  112.       VK_D => BUTTONS[22],
  113.       VK_Q => BUTTONS[23],
  114.       VK_S => BUTTONS[21],
  115.       VK_V => BUTTONS[18],
  116.       VK_W => BUTTONS[24],
  117.       VK_X => BUTTONS[16],
  118.       VK_Z => BUTTONS[15],
  119.       VK_NUMPAD0 => BUTTONS[13],
  120.       VK_F5 => F5,
  121.       VK_F6 => F6,
  122.       VK_F7 => F7,
  123.       VK_F8 => F8,
  124.       VK_F9 => F9
  125.   }
  126.      
  127.   ASYNC_KEY = Win32API.new('user32', 'GetAsyncKeyState', 'i', 'i') # Trigger
  128.   KEY_STATE = Win32API.new('user32', 'GetKeyState',      'i', 'i') # Press
  129.  
  130.   # Number of frames to wait between each scroll
  131.   SCROLL_WAIT = 5
  132.   # Frames to wait between each repeat
  133.   REPEAT_WAIT = 4
  134.   # Frames to wait from the first repeat to the following repeats
  135.   REPEAT_START_WAIT = 16
  136.  
  137.   # Initialize button presses and triggers
  138.   class << self
  139.     @@button_presses = {}
  140.     @@button_presses.default = 0
  141.     @@button_trigger = {}
  142.     @@button_trigger.default = false
  143.   end
  144.  
  145.   # module functions
  146.   module_function
  147.  
  148.   def update_trigger(state, trigger)
  149.     @@button_trigger[trigger] |= (state == 1)
  150.   end
  151.  
  152.   def update_triggers
  153.     @@button_trigger.clear
  154.     # Check for clicks
  155.     for trigger in KEYBOARD_TRIGGERS
  156.       # Gets key state for presses
  157.       state = KEY_STATE.call(trigger)
  158.       async = ASYNC_KEY.call(trigger) & 1
  159.       if BUTTON_MAPPING[trigger].is_a?(Array)
  160.         for button in BUTTON_MAPPING[trigger]
  161.           next if button == 0
  162.           update_press(state, button)
  163.           update_trigger(async, button)
  164.         end
  165.       else
  166.         next if BUTTON_MAPPING[trigger] == 0
  167.         update_press(state, BUTTON_MAPPING[trigger])
  168.         update_trigger(async, BUTTON_MAPPING[trigger])
  169.       end
  170.     end
  171.   end
  172.  
  173.   def update_press(state, trigger)
  174.     # If 0 or 1
  175.     if state == 0 || state == 1
  176.       if @@button_presses[trigger] > 0
  177.         @@button_presses[trigger] *= -1
  178.       else
  179.         @@button_presses[trigger] = 0
  180.       end
  181.     else
  182.       if @@button_presses[trigger] > 0
  183.         @@button_presses[trigger] += 1
  184.       else
  185.         @@button_presses[trigger] = 1
  186.       end
  187.     end
  188.   end
  189.  
  190.  
  191.   ##
  192.   # Overrides
  193.   #
  194.   def update
  195.     update_triggers
  196.   end
  197.  
  198.   def trigger?(num)
  199.     return @@button_trigger[num]
  200.   end
  201.   def press?(num)
  202.     return @@button_presses[num] >= 1
  203.   end
  204.   def repeat?(num)
  205.     if @@button_presses[num] <= 0
  206.       return false
  207.     elsif @@button_presses[num] == 1
  208.       return true
  209.     elsif @@button_presses[num] > REPEAT_START_WAIT
  210.       return @@button_presses[num] % REPEAT_WAIT == 1
  211.     end
  212.   end
  213.  
  214.   def dir4
  215.     if press?(LEFT)
  216.       return LEFT
  217.     elsif press?(RIGHT)
  218.       return RIGHT
  219.     elsif press?(DOWN)
  220.       return DOWN
  221.     elsif press?(UP)
  222.       return UP
  223.     else
  224.       return 0
  225.     end
  226.   end
  227. end