StenHisDirt

Input System

Nov 1st, 2021 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. --// All made by Sten :)
  2.  
  3. --// Services
  4. local UserInputService = game:GetService("UserInputService")
  5.  
  6. --// Blocks inputs that will break the input system if enabled
  7. local blocked_input = {
  8. Enum.UserInputType.MouseMovement,
  9. Enum.UserInputType.Focus,
  10. }
  11.  
  12. --// Combines multiple arrays into one Array
  13. local function CombineArray(...)
  14. local table = {}
  15.  
  16. for _, array in next, {...} do
  17. for i, v in next, array do
  18. table[i] = v
  19. end
  20. end
  21.  
  22. return table
  23. end
  24.  
  25. --// Module
  26. local keybinds = {} do
  27. keybinds.keys_down = {}
  28. keybinds.keybinded = {}
  29.  
  30. --// Keybind remover
  31. function keybinds.AddKeybind(_keycodes, _callback, _args, _settings)
  32. local bind = {}
  33.  
  34. _args = _args or {}
  35. _settings = _settings or {}
  36.  
  37. function bind.Remove()
  38. keybinds.keybinded[bind.index] = nil
  39. end
  40.  
  41. bind.index = #keybinds.keybinded + 1
  42. bind.settgins = _settings
  43. bind.keycodes = _keycodes
  44. bind.callback = _callback
  45. bind.args = _args
  46.  
  47. keybinds.keybinded[bind.index] = {
  48. _keycodes,
  49. _callback,
  50. _args,
  51. _settings
  52. }
  53.  
  54. return bind
  55. end
  56.  
  57. --// Activates on input
  58. function Input(_bool, _input, _processedevent)
  59. local current_key = (_input.UserInputType == Enum.UserInputType.Keyboard and _input.KeyCode) or (not table.find(blocked_input, _input.UserInputType) and _input.UserInputType) or nil
  60.  
  61. if _processedevent or current_key == nil then
  62. return
  63. end
  64.  
  65. current_key = tostring(current_key):split(".")[3]
  66.  
  67. keybinds.keys_down[current_key] = _bool
  68.  
  69. if #keybinds.keybinded == 0 then
  70. return
  71. end
  72.  
  73. for _, bind in next, keybinds.keybinded do
  74. local can_activate = true
  75.  
  76. for key, value in next, keybinds.keys_down do
  77. if (value == true and bind[4].singular and not table.find(bind[1], key)) or (value == false and table.find(bind[1], key)) then
  78. can_activate = false
  79. break
  80. end
  81. end
  82.  
  83. if not can_activate or not table.find(bind[1], current_key) then
  84. continue
  85. end
  86.  
  87. --// Activates callback
  88. task.spawn(bind[2], unpack(bind[3]))
  89. end
  90. end
  91.  
  92. UserInputService.InputBegan:Connect(function(_input, _processedevent)
  93. Input(true, _input, _processedevent)
  94. end)
  95.  
  96. UserInputService.InputEnded:Connect(function(_input, _processedevent)
  97. Input(false, _input, _processedevent)
  98. end)
  99.  
  100. do -- Setup
  101. local inputs = CombineArray(Enum.KeyCode:GetEnumItems(), Enum.UserInputType:GetEnumItems())
  102.  
  103. for _, key in next, inputs do
  104. key = tostring(key):split(".")[3]
  105. keybinds.keys_down[key] = false
  106. end
  107. end
  108. end
  109.  
  110. return keybinds
Add Comment
Please, Sign In to add comment