win 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/win/gui/input.rb CHANGED
@@ -7,6 +7,11 @@ module Win
7
7
  module Input
8
8
  include Win::Library
9
9
 
10
+ # Internal constants:
11
+
12
+ # Key event delay
13
+ KEY_DELAY = 0.00001
14
+
10
15
  # Windows keyboard-related Constants:
11
16
 
12
17
  # Key down keyboard event (the key is being depressed)
@@ -132,115 +137,123 @@ module Win
132
137
  # The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate
133
138
  # a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
134
139
  #
135
- # !!!! Windows NT/2000/XP/Vista:This function has been superseded. Use SendInput instead.
140
+ # <b> !! Windows NT/2000/XP/Vista:This function has been superseded. Use SendInput instead. </b>
136
141
  #
137
- # Syntax: VOID keybd_event( BYTE bVk, BYTE bScan, DWORD dwFlags, PTR dwExtraInfo);
142
+ # [*Syntax*] VOID keybd_event( BYTE bVk, BYTE bScan, DWORD dwFlags, PTR dwExtraInfo);
138
143
  #
139
- # Parameters:
140
- # bVk [C] - [in] Specifies a virtual-key code. The code must be a value in the range 1 to 254.
141
- # For a complete list, see Virtual-Key Codes.
142
- # bScan [C] - [in] Specifies a hardware scan code for the key.
143
- # dwFlags [L] - [in] Specifies various aspects of function operation. This parameter can be
144
- # one or more of the following values:
145
- # KEYEVENTF_EXTENDEDKEY, KEYEVENTF_KEYUP, KEYEVENTF_KEYDOWN
146
- # dwExtraInfo [L] -[in] Specifies an additional value associated with the key stroke.
144
+ # bVk:: [in] Specifies a virtual-key code. The code must be a value in the range 1 to 254.
145
+ # For a complete list, see Virtual-Key Codes.
146
+ # bScan:: [in] Specifies a hardware scan code for the key.
147
+ # dwFlags:: [in] Specifies various aspects of function operation. This parameter can be
148
+ # one or more of the following values:
149
+ # KEYEVENTF_EXTENDEDKEY, KEYEVENTF_KEYUP, KEYEVENTF_KEYDOWN
150
+ # dwExtraInfo:: [in] Specifies an additional value associated with the key stroke.
147
151
  #
148
- # Return Value: none
152
+ # *Returns*:: nothing
153
+ # ---
154
+ # *Remarks*:
155
+ # - An application can simulate a press of the PRINTSCRN key in order to obtain a screen snapshot
156
+ # and save it to the clipboard. To do this, call keybd_event with the bVk parameter set to VK_SNAPSHOT.
149
157
  #
150
- # Remarks: An application can simulate a press of the PRINTSCRN key in order to obtain a screen snapshot and save
151
- # it to the clipboard. To do this, call keybd_event with the bVk parameter set to VK_SNAPSHOT.
158
+ # - Windows NT/2000/XP: The keybd_event function can toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK keys.
159
+ # - Windows 95/98/Me: The keybd_event function can toggle only the CAPS LOCK and SCROLL LOCK keys.
152
160
  #
153
- # Windows NT/2000/XP: The keybd_event function can toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK keys.
154
- # Windows 95/98/Me: The keybd_event function can toggle only the CAPS LOCK and SCROLL LOCK keys.
161
+ # :call-seq:
162
+ # keybd_event( virtual_key, scan_code, flags, extra_info )
155
163
  #
156
- function 'keybd_event', 'IILL', 'V'
164
+ function :keybd_event, [:char, :char, :ulong, :ulong], :void
157
165
 
158
166
  ##
159
167
  # The mouse_event function synthesizes mouse motion and button clicks.
160
- # !!!! Windows NT/2000/XP: This function has been superseded. Use SendInput instead.
161
- #
162
- # Syntax: VOID mouse_event( DWORD dwFlags, DWORD dx, DWORD dy, DWORD dwData, ULONG_PTR dwExtraInfo );
163
- #
164
- # Parameters:
165
- # flags (I) - [in] Specifies various aspects of mouse motion and button clicking. This parameter can be
166
- # certain combinations of the following values. The values that specify mouse button status are set to
167
- # indicate changes in status, not ongoing conditions. For example, if the left mouse button is pressed
168
- # and held down, MOUSEEVENTF_LEFTDOWN is set when the left button is first pressed, but not for subsequent
169
- # motions. Similarly, MOUSEEVENTF_LEFTUP is set only when the button is first released. You cannot specify
170
- # both MOUSEEVENTF_WHEEL and either MOUSEEVENTF_XDOWN or MOUSEEVENTF_XUP simultaneously, because they
171
- # both require use of the dwData field:
172
- # MOUSEEVENTF_ABSOLUTE, MOUSEEVENTF_MOVE, MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP, MOUSEEVENTF_RIGHTDOWN,
173
- # MOUSEEVENTF_RIGHTUP, MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP, MOUSEEVENTF_WHEEL, MOUSEEVENTF_XDOWN,
174
- # MOUSEEVENTF_XUP
175
- # dx (I) - [in] Specifies the mouse's absolute position along the x-axis or its amount of motion since the
176
- # last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is
177
- # specified as the mouse's actual x-coordinate; relative data is specified as the number of mickeys moved.
178
- # A mickey is the amount that a mouse has to move for it to report that it has moved.
179
- # dy (I) - [in] Specifies the mouse's absolute position along the y-axis or its amount of motion since the
180
- # last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is
181
- # specified as the mouse's actual y-coordinate; relative data is specified as the number of mickeys moved.
182
- # data (I) - [in] If flags contains MOUSEEVENTF_WHEEL, then data specifies the amount of wheel movement.
183
- # A positive value indicates that the wheel was rotated forward, away from the user; a negative value
184
- # indicates that the wheel was rotated backward, toward the user. One wheel click is defined as
185
- # WHEEL_DELTA, which is 120. If flags contains MOUSEEVENTF_WHHEEL, then data specifies the amount of
186
- # wheel movement. A positive value indicates that the wheel was rotated to the right; a negative value
187
- # indicates that the wheel was rotated to the left. One wheel click is defined as WHEEL_DELTA, which is 120.
188
- # Windows 2000/XP: If flags contains MOUSEEVENTF_XDOWN or MOUSEEVENTF_XUP, then data specifies which X
189
- # buttons were pressed or released. This value may be any combination of the following flags.
190
- # If flags is not MOUSEEVENTF_WHEEL, MOUSEEVENTF_XDOWN, or MOUSEEVENTF_XUP, then data should be zero.
191
- # XBUTTON1 - Set if the first X button was pressed or released.
192
- # XBUTTON2 - Set if the second X button was pressed or released.
193
- # extra_info (P) - [in] Specifies an additional value associated with the mouse event. An application
194
- # calls GetMessageExtraInfo to obtain this extra information.
195
- #
196
- # NO Return Value
197
- #
198
- # Remarks: If the mouse has moved, indicated by MOUSEEVENTF_MOVE being set, dx and dy hold information about
199
- # that motion. The information is specified as absolute or relative integer values. If MOUSEEVENTF_ABSOLUTE
200
- # value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. The event
201
- # procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner
202
- # of the display surface, (65535,65535) maps onto the lower-right corner. If the MOUSEEVENTF_ABSOLUTE value
203
- # is not specified, dx and dy specify relative motions from when the last mouse event was generated (the last
204
- # reported position). Positive values mean the mouse moved right (or down); negative values mean the mouse
205
- # moved left (or up). Relative mouse motion is subject to the settings for mouse speed and acceleration level.
206
- # An end user sets these values using the Mouse application in Control Panel. An application obtains and sets
207
- # these values with the SystemParametersInfo function. The system applies two tests to the specified relative
208
- # mouse motion when applying acceleration. If the specified distance along either the x or y axis is greater
209
- # than the first mouse threshold value, and the mouse acceleration level is not zero, the operating system
210
- # doubles the distance. If the specified distance along either the x- or y-axis is greater than the second
211
- # mouse threshold value, and the mouse acceleration level is equal to two, the operating system doubles the
212
- # distance that resulted from applying the first threshold test. It is thus possible for the operating system
213
- # to multiply relatively-specified mouse motion along the x- or y-axis by up to four times. Once acceleration
214
- # has been applied, the system scales the resultant value by the desired mouse speed. Mouse speed can range
215
- # from 1 (slowest) to 20 (fastest) and represents how much the pointer moves based on the distance the mouse
216
- # moves. The default value is 10, which results in no additional modification to the mouse motion. The
217
- # mouse_event function is used to synthesize mouse events by applications that need to do so. It is also used
218
- # by applications that need to obtain more information from the mouse than its position and button state.
219
- # For example, if a tablet manufacturer wants to pass pen-based information to its own applications, it can
220
- # write a DLL that communicates directly to the tablet hardware, obtains the extra information, and saves it
221
- # in a queue. The DLL then calls mouse_event with the standard button and x/y position data, along with,
222
- # in the dwExtraInfo parameter, some pointer or index to the queued extra information. When the application
223
- # needs the extra information, it calls the DLL with the pointer or index stored in dwExtraInfo, and the DLL
224
- # returns the extra information.
225
- #
226
- #
227
- function 'mouse_event', 'IIIIP', 'V'
168
+ #
169
+ # <b> !! Windows NT/2000/XP/Vista:This function has been superseded. Use SendInput instead. </b>
170
+ #
171
+ # [*Syntax*] VOID mouse_event( DWORD dwFlags, DWORD dx, DWORD dy, DWORD dwData, ULONG_PTR dwExtraInfo );
172
+ #
173
+ # dwFlags:: [in] Specifies various aspects of mouse motion and button clicking. This parameter can be certain
174
+ # combinations of the following values. The values that specify mouse button status are set to indicate
175
+ # changes in status, not ongoing conditions. For example, if the left mouse button is pressed and
176
+ # held down, MOUSEEVENTF_LEFTDOWN is set when the left button is first pressed, but not for subsequent
177
+ # motions. Similarly, MOUSEEVENTF_LEFTUP is set only when the button is first released. You cannot
178
+ # specify both MOUSEEVENTF_WHEEL and either MOUSEEVENTF_XDOWN or MOUSEEVENTF_XUP simultaneously,
179
+ # because they both require use of the dwData field:
180
+ # MOUSEEVENTF_ABSOLUTE, MOUSEEVENTF_MOVE, MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP,
181
+ # MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP, MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP,
182
+ # MOUSEEVENTF_WHEEL, MOUSEEVENTF_XDOWN, MOUSEEVENTF_XUP
183
+ # dx:: [in] Specifies the mouse's absolute position along the x-axis or its amount of motion since the
184
+ # last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is
185
+ # specified as the mouse's actual x-coordinate; relative data is specified as the number of mickeys moved.
186
+ # A mickey is the amount that a mouse has to move for it to report that it has moved.
187
+ # dy:: [in] Specifies the mouse's absolute position along the y-axis or its amount of motion since the
188
+ # last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is
189
+ # specified as the mouse's actual y-coordinate; relative data is specified as the number of mickeys moved.
190
+ # dwData:: [in]
191
+ # - If dwFlags contains MOUSEEVENTF_WHEEL, then data specifies the amount of wheel movement.
192
+ # A positive value indicates that the wheel was rotated forward, away from the user; a negative value
193
+ # indicates that the wheel was rotated backward, toward the user. One wheel click is defined as
194
+ # WHEEL_DELTA, which is 120.
195
+ # - If dwFlags contains MOUSEEVENTF_WHHEEL, then data specifies the amount of
196
+ # wheel movement. A positive value indicates that the wheel was rotated to the right; a negative value
197
+ # indicates that the wheel was rotated to the left. One wheel click is defined as WHEEL_DELTA (= 120).
198
+ # - Windows 2000/XP: If flags contains MOUSEEVENTF_XDOWN or MOUSEEVENTF_XUP, then data specifies which
199
+ # X buttons were pressed or released. This value may be any combination of the following flags.
200
+ # - If flags is not MOUSEEVENTF_WHEEL, MOUSEEVENTF_XDOWN, or MOUSEEVENTF_XUP, then data should be zero.
201
+ # XBUTTON1 - Set if the first X button was pressed or released.
202
+ # XBUTTON2 - Set if the second X button was pressed or released.
203
+ # dwExtraInfo:: [in] Specifies an additional value associated with the mouse event. An application
204
+ # calls GetMessageExtraInfo to obtain this extra information.
205
+ # <b>NO Return Value</b>
206
+ # ---
207
+ # *Remarks*:
208
+ # - If the mouse has moved, indicated by MOUSEEVENTF_MOVE being set, dx and dy hold information about
209
+ # that motion. The information is specified as absolute or relative integer values.
210
+ # - If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between
211
+ # 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps
212
+ # onto the upper-left corner of the display surface, (65535,65535) maps onto the lower-right corner.
213
+ # - If the MOUSEEVENTF_ABSOLUTE value is not specified, dx and dy specify relative motions from when the
214
+ # last mouse event was generated (the last reported position). Positive values mean the mouse moved right
215
+ # (or down); negative values mean the mouse moved left (or up). Relative mouse motion is subject to the
216
+ # settings for mouse speed and acceleration level. An end user sets these values using the Mouse application
217
+ # in Control Panel. An application obtains and sets these values with the SystemParametersInfo function.
218
+ # - The system applies two tests to the specified relative mouse motion when applying acceleration. If the
219
+ # specified distance along either the x or y axis is greater than the first mouse threshold value, and the
220
+ # mouse acceleration level is not zero, the operating system doubles the distance. If the specified distance
221
+ # along either the x- or y-axis is greater than the second mouse threshold value, and the mouse acceleration
222
+ # level is equal to two, the operating system doubles the distance that resulted from applying the first
223
+ # threshold test. It is thus possible for the operating system to multiply relatively-specified mouse motion
224
+ # along the x- or y-axis by up to four times.
225
+ # - Once acceleration has been applied, the system scales the resultant value by the desired mouse speed.
226
+ # Mouse speed can range from 1 (slowest) to 20 (fastest) and represents how much the pointer moves based
227
+ # on the distance the mouse moves. The default value is 10, which results in no additional modification
228
+ # to the mouse motion.
229
+ # - The mouse_event function is used to synthesize mouse events by applications that need to do so. It is also
230
+ # used by applications that need to obtain more information from the mouse than its position and button state.
231
+ # For example, if a tablet manufacturer wants to pass pen-based information to its own applications, it can
232
+ # write a DLL that communicates directly to the tablet hardware, obtains the extra information, and saves it
233
+ # in a queue. The DLL then calls mouse_event with the standard button and x/y position data, along with,
234
+ # in the dwExtraInfo parameter, some pointer or index to the queued extra information. When the application
235
+ # needs the extra information, it calls the DLL with the pointer or index stored in dwExtraInfo, and the DLL
236
+ # returns the extra information.
237
+ #
238
+ # :call-seq:
239
+ # mouse_event( flags, dx, dy, data, extra_info )
240
+ #
241
+ function :mouse_event, [:ulong, :ulong, :ulong, :ulong, :ulong, ], :void
228
242
 
229
243
  ##
230
244
  # SetCursorPos Function moves the cursor to the specified screen coordinates. If the new coordinates are not
231
245
  # within the screen rectangle set by the most recent ClipCursor function call, the system automatically adjusts
232
246
  # the coordinates so that the cursor stays within the rectangle.
233
247
  #
234
- # Syntax: BOOL SetCursorPos( int X, int Y );
248
+ # [*Syntax*] BOOL SetCursorPos( int X, int Y );
235
249
  #
236
- # Parameters:
237
- # X (i) - [in] Specifies the new x-coordinate of the cursor, in screen coordinates.
238
- # Y (i) - [in] Specifies the new y-coordinate of the cursor, in screen coordinates.
250
+ # X:: [in] Specifies the new x-coordinate of the cursor, in screen coordinates.
251
+ # Y:: [in] Specifies the new y-coordinate of the cursor, in screen coordinates.
239
252
  #
240
- # Return Value: Nonzero if successful or zero otherwise. To get extended error information, call GetLastError.
241
- # Enhanced to return true/false instead of nonzero/zero
242
- #
243
- # Remarks: The cursor is a shared resource. A window should move the cursor only when the cursor is in the
253
+ # *Returns*:: Nonzero(*true*) if successful or zero(*false*) otherwise. To get extended error information,
254
+ # call GetLastError. Enhanced to return true/false instead of nonzero/zero
255
+ # ---
256
+ # *Remarks*: The cursor is a shared resource. A window should move the cursor only when the cursor is in the
244
257
  # window's client area. The calling process must have WINSTA_WRITEATTRIBUTES access to the window station.
245
258
  # The input desktop must be the current desktop when you call SetCursorPos. Call OpenInputDesktop to determine
246
259
  # whether the current desktop is the input desktop. If it is not, call SetThreadDesktop with the HDESK returned
@@ -249,7 +262,29 @@ module Win
249
262
  # :call-seq:
250
263
  # success = set_cursor_pos(x,y)
251
264
  #
252
- function :SetCursorPos, [:int, :int], :bool
265
+ function :SetCursorPos, [:int, :int], :int, boolean: true
266
+
267
+ # Convenience methods
268
+
269
+ ##
270
+ # Emulates combinations of (any amount of) keys pressed one after another (Ctrl+Alt+P) and then released
271
+ # *keys should be a sequence of a virtual-key codes. The codes must be a value in the range 1 to 254.
272
+ # For a complete list, see msdn:Virtual Key Codes.
273
+ def keystroke(*keys)
274
+ return if keys.empty?
275
+ keybd_event keys.first, 0, KEYEVENTF_KEYDOWN, 0
276
+ sleep KEY_DELAY
277
+ keystroke *keys[1..-1]
278
+ sleep KEY_DELAY
279
+ keybd_event keys.first, 0, KEYEVENTF_KEYUP, 0
280
+ end
281
+
282
+ # types text message into window holding the focus
283
+ def type_in(message)
284
+ message.scan(/./m) do |char|
285
+ keystroke(*char.to_vkeys)
286
+ end
287
+ end
253
288
  end
254
289
  end
255
290
  end