Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'win32/api'
- require 'win32con'
- require 'win32con/keymap'
- # Include Win32 Constants for more convenient access
- include Win32CON
- # Win32 APIs
- GetAsyncKeyState = Win32::API.new('GetAsyncKeyState', 'L', 'L', 'user32')
- MouseEvent = Win32::API.new('mouse_event', 'LLLLP', 'L', 'user32')
- # Input module
- module Input
- # initialize keystates
- @keystate = Array.new(0xff){0}
- module_function
- # keystate getter
- def keystate; @keystate; end
- # Input main update
- def update
- 0xff.times do |i|
- # SHORT to BOOL
- pressed = (GetAsyncKeyState.call(i) & 0x8000) > 0
- @keystate[i] = pressed ? (@keystate[i] + 1) : 0
- end
- end
- # mouse right down
- def right_down
- MouseEvent.call(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
- end
- # mouse right up
- def right_up
- MouseEvent.call(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
- end
- # mouse right click
- def right_click
- right_down
- sleep(0.01)
- right_up
- end
- # Whether the key is just pressed down
- def trigger?(key)
- @keystate[key] == 1
- end
- # Whether the key is pressed
- def pressed?(key)
- @keystate[key] >= 1
- end
- # Get length of the key being pressed
- def repeat?(key)
- @keystate[key]
- end
- end
- # Update per second
- FPS = 120
- # Sleep time between updates
- TickSleepTime = (1 / 120)
- begin
- loop do
- sleep(TickSleepTime)
- Input.update
- Input.right_down if Input.trigger?(Keymap[:vk_F7])
- Input.right_up if Input.trigger?(Keymap[:vk_F8])
- exit if Input.trigger?(Keymap[:vk_F9])
- end
- rescue SystemExit, Interrupt
- puts "Bye!"
- ensure
- # ensure the mouse right key is released
- Input.right_up
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement