How to send keystrokes to windows from a ruby app?
I´ve been trying to interact with another app on windows, which doesn´t have any data exchange protocol implement开发者_运维技巧ed. So i figured the best way to do this is by fetching data from an app and send it to the other one by sending keystrokes, simulating human interaction.
But i am having such a hard time trying to implement this kind of behavior! Do you know how to do this using Ruby?
You could install the Ruby-FFI gem: [sudo] gem install ffi
, use it to load user32.dll
, then bind and call the keybd_event
method.
Here's an example from the FFI Github wiki:
require 'ffi'
module Win
VK_VOLUME_DOWN = 0xAE; VK_VOLUME_UP = 0xAF; VK_VOLUME_MUTE = 0xAD;
KEYEVENTF_KEYUP = 2
extend FFI::Library
ffi_lib 'user32'
ffi_convention :stdcall
attach_function :keybd_event, [ :uchar, :uchar, :int, :pointer ], :void
# simulate pressing the mute key on the keyboard
keybd_event(VK_VOLUME_MUTE, 0, 0, nil);
keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_KEYUP, nil);
end
keybd_event would work, you could also use jruby to script the java Robot class
精彩评论