Skip to content

Instantly share code, notes, and snippets.

@Maher4Ever
Created February 19, 2012 21:25
Show Gist options
  • Save Maher4Ever/1865875 to your computer and use it in GitHub Desktop.
Save Maher4Ever/1865875 to your computer and use it in GitHub Desktop.
Make the cursor on Windows go crazy!
require 'win32/api'
class Cursor
include Win32
DLL = 'user32'
def initialize
@set_cursor_pos = API.new('SetCursorPos', 'II', 'B', DLL)
@get_cursor_pos = API.new('GetCursorPos', 'P', 'V', DLL)
end
def position
points = new_buffer(8)
@get_cursor_pos.call(points)
pos = points.unpack("LL")
{ :x => pos[0], :y => pos[1] }
end
def position=(pos)
raise "The parameter must be a hash with x and y coordinates" unless pos.is_a?(Hash) and pos[:x] and pos[:y]
@set_cursor_pos.call(pos[:x],pos[:y])
end
private
def new_buffer(size_in_bytes)
' ' * size_in_bytes
end
end
machine_info = Win32::API.new('GetSystemMetrics', 'I', 'I', 'user32')
screen_width = machine_info.call(78)
screen_height = machine_info.call(79)
signs = %w{+ -}
# Create a cursor
cur = Cursor.new
puts "I'll screw up your mouse!"
loop do
old_pos = cur.position
cur.position = {
:x => eval("#{old_pos[:x]} #{signs.sample} #{rand(screen_width)}"),
:y => eval("#{old_pos[:y]} #{signs.sample} #{rand(screen_height)}")
}
sleep 0.1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment