Advertisement
LunaeStellsr

Ruby clicker example

Nov 21st, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.68 KB | None | 0 0
  1. require 'win32/api'
  2. require 'win32con'
  3. require 'win32con/keymap'
  4.  
  5. # Include Win32 Constants for more convenient access
  6. include Win32CON
  7.  
  8. # Win32 APIs
  9. GetAsyncKeyState = Win32::API.new('GetAsyncKeyState', 'L', 'L', 'user32')
  10. MouseEvent = Win32::API.new('mouse_event', 'LLLLP', 'L', 'user32')
  11.  
  12. # Input module
  13. module Input
  14.  
  15.   # initialize keystates
  16.   @keystate = Array.new(0xff){0}
  17.  
  18.   module_function
  19.  
  20.   # keystate getter
  21.   def keystate; @keystate; end
  22.  
  23.   # Input main update
  24.   def update
  25.     0xff.times do |i|
  26.       # SHORT to BOOL
  27.       pressed = (GetAsyncKeyState.call(i) & 0x8000) > 0
  28.       @keystate[i] = pressed ? (@keystate[i] + 1) : 0
  29.     end
  30.   end
  31.  
  32.   # mouse right down
  33.   def right_down
  34.     MouseEvent.call(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
  35.   end
  36.  
  37.   # mouse right up
  38.   def right_up
  39.     MouseEvent.call(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
  40.   end
  41.  
  42.   # mouse right click
  43.   def right_click
  44.     right_down
  45.     sleep(0.01)
  46.     right_up
  47.   end
  48.  
  49.   # Whether the key is just pressed down
  50.   def trigger?(key)
  51.     @keystate[key] == 1
  52.   end
  53.  
  54.   # Whether the key is pressed
  55.   def pressed?(key)
  56.     @keystate[key] >= 1
  57.   end
  58.  
  59.   # Get length of the key being pressed
  60.   def repeat?(key)
  61.     @keystate[key]
  62.   end
  63. end
  64.  
  65. # Update per second
  66. FPS = 120
  67.  
  68. # Sleep time between updates
  69. TickSleepTime = (1 / 120)
  70.  
  71. begin
  72.   loop do
  73.     sleep(TickSleepTime)
  74.     Input.update
  75.     Input.right_down if Input.trigger?(Keymap[:vk_F7])
  76.     Input.right_up   if Input.trigger?(Keymap[:vk_F8])
  77.     exit             if Input.trigger?(Keymap[:vk_F9])
  78.   end
  79. rescue SystemExit, Interrupt
  80.   puts "Bye!"
  81. ensure
  82.   # ensure the mouse right key is released
  83.   Input.right_up
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement