Module: MouseControl

Extended by:
FFI::Library
Defined in:
lib/simple_gui_creator/mouse_control.rb

Defined Under Namespace

Classes: Input, InputEvent, MouseInput

Constant Summary collapse

MouseInfo =
java.awt.MouseInfo
MOUSEEVENTF_MOVE =
1
INPUT_MOUSE =
0
MOUSEEVENTF_ABSOLUTE =
0x8000
MOUSEEVENTF_LEFTDOWN =
0x0002
MOUSEEVENTF_LEFTUP =
0x0004
VK_LBUTTON =

mouse left button for GetAsyncKeyState (seeing if mouse down currently or not)

0x01

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.total_movementsObject

Returns the value of attribute total_movements.



132
133
134
# File 'lib/simple_gui_creator/mouse_control.rb', line 132

def total_movements
  @total_movements
end

Class Method Details

.get_mouse_locationObject

x, y


127
128
129
130
# File 'lib/simple_gui_creator/mouse_control.rb', line 127

def get_mouse_location
  loc = MouseInfo.getPointerInfo.getLocation # pure java!
  [loc.x, loc.y]
end

.jitter_forever_in_own_threadObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/simple_gui_creator/mouse_control.rb', line 62

def jitter_forever_in_own_thread
  
  old_x, old_y = get_mouse_location
  Thread.new {
    loop {
      move_y = 8 # just enough for VLC when full screened...
      cur_x, cur_y = get_mouse_location
      if(cur_x == old_x && cur_y == old_y)
        @total_movements += 1
        # blit it up
        move_mouse_relative(0, move_y)
        move_mouse_relative(0, move_y * -1)
        # let it move it back
        sleep 0.05
        old_x, old_y = get_mouse_location
        sleep 0.75
      else
        # user has been moving the mouse around, so we don't need to, to not annoy them
        old_x, old_y = get_mouse_location
        sleep 3
      end
    }
  }
  
end

.left_mouse_button_stateObject



117
118
119
120
121
122
123
124
# File 'lib/simple_gui_creator/mouse_control.rb', line 117

def left_mouse_button_state
  GetAsyncKeyState(VK_LBUTTON) # ignore a first response, which also tells us if it has changed at all since last call
  if GetAsyncKeyState(VK_LBUTTON) == 0 # zero means up
    :up
  else
    :down
  end
end

.left_mouse_down!Object



107
108
109
# File 'lib/simple_gui_creator/mouse_control.rb', line 107

def left_mouse_down!
  send_left_mouse_button MOUSEEVENTF_LEFTDOWN
end

.left_mouse_up!Object



111
112
113
# File 'lib/simple_gui_creator/mouse_control.rb', line 111

def left_mouse_up!
  send_left_mouse_button MOUSEEVENTF_LEFTUP
end

.move_mouse_relative(dx, dy) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/simple_gui_creator/mouse_control.rb', line 88

def move_mouse_relative dx, dy 
  myinput = MouseControl::Input.new
  myinput[:type] = MouseControl::INPUT_MOUSE
  in_evt = myinput[:evt][:mi]
  in_evt[:mouse_data] = 0 # null it out
  in_evt[:flags] = MouseControl::MOUSEEVENTF_MOVE
  in_evt[:time] = 0
  in_evt[:extra] = 0
  in_evt[:dx] = dx
  in_evt[:dy] = dy
  SendInput(1, myinput, MouseControl::Input.size)
end

.single_click_left_mouse_buttonObject



101
102
103
104
105
# File 'lib/simple_gui_creator/mouse_control.rb', line 101

def single_click_left_mouse_button
  left_mouse_down!
  left_mouse_up!
  p "CLICKED LEFT MOUSE BUTTON"
end