Ruby调用WindowsAPI模拟按键

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

require 'win32api'

class KeyBoardHelper
    attr_reader :ctrl, :shift, :alt, :win

    def initialize
        @ctrl, @shift, @alt, @win = 0x11, 0x10, 0x12, 0x5b
        @bScan, @downFlag, @upFlag, @extraInfo = 0x45, 1, 3, 0

        @ke = Win32API.new('User32.dll', 'keybd_event', 'IIII')
    end

    def press(key, &block)
        key = key.upcase.bytes[0] if key.instance_of? String

        @ke.call(key, @bScan, @downFlag, @extraInfo)
        yield if block
        @ke.call(key, @bScan, @upFlag, @extraInfo)
    end

    def test_paste
        press(@ctrl) {
            press('v')
        }
    end
end

=begin
h = KeyBoardHelper.new
sleep 3
h.test_paste
=end