Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

key emulate

For general discussion related FlowStone

key emulate

Postby fixstuff555 » Mon Jul 29, 2013 9:37 pm

Hi all,

Anyone done any keyboard emulation with Ruby, and the Win32 api?

In particular, I'd like to send keystrokes to the windows keyboard stream using SendInput in API if possible.

Any ideas on how to do this?

I found this:

http://batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput/, but its for C++. so I found this:

http://www.velocityreviews.com/forums/t821631-ruby-dl-sendinput.html

But its not functional the way its shown.. Any ideas on how to do this? I have attached the code I was testing, but I can't get it to work. I'm sure its something stupid.
Attachments
key_emulate2.fsm
(617 Bytes) Downloaded 900 times
fixstuff555
 
Posts: 151
Joined: Thu Oct 21, 2010 3:24 pm

Re: key emulate

Postby Tronic » Tue Jul 30, 2013 4:27 am

this is the code that I use.
:arrow:
Code: Select all
###########################
# Ruby Key Sender by Tronic
###########################

require 'Win32API'
# http://msdn.microsoft.com/en-us/library/windows/desktop/ms646306(v=vs.85).aspx
MapVirtualKey = Win32API.new('user32.dll', 'MapVirtualKey', %w(i i), 'i')

# http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
SendInput = Win32API.new("user32", "SendInput", %w(i p i), 'i')

# http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
# http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271(v=vs.85).aspx
INPUT_KEYBOARD = 1
KEYEVENTF_UNICODE = 0x0004
KEYEVENTF_KEYUP = 2
# Defined costant for Shift & Ctrl
# Add here the other special key you need
VK_SHIFT = 0x10
VK_CONTROL = 0x11

# Perform the operation hit for the specified key on the keyboard.
# Vkey Specifies an array of key code of the key that hit at the same time.
# For example, if you want to enter the t (lower case)
# keys_send (['T'])
# If you want to enter the T (upper case)
# keys_send ([VK_SHIFT, 'T'])

def keys_send(keys)
# convert key to integer unicode
key_conv= keys.map{|ikeys|
         case ikeys
           when String
           ikeys.unpack('U')[0]
           else
           ikeys
           end   }

wscans = key_conv.map{|vkey| MapVirtualKey.call(vkey, 0)}      
vkeywscan = key_conv.zip(wscans)
 
keydowns = vkeywscan.map{|vkey, wscan|
         [INPUT_KEYBOARD, vkey, wscan, 0, 0, 0, 0, 0].pack('ISSIIIII')
         }
keyups = vkeywscan.map{|vkey, wscan|
         [INPUT_KEYBOARD, vkey, wscan, KEYEVENTF_UNICODE|KEYEVENTF_KEYUP, 0, 0, 0, 0].pack('ISSIIIII')
         }.reverse
 
SendInput.call(keys.length*2, keydowns.join('')+keyups.join(''), keydowns[0].length)
end


def event i
   keys_send([VK_SHIFT,'K'])
end
Tronic
 
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: key emulate

Postby MyCo » Tue Jul 30, 2013 11:04 am

User avatar
MyCo
 
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany

Re: key emulate

Postby kortezzzz » Tue Jul 30, 2013 12:15 pm

THANKS,
and MyCo, please check your massage box.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: key emulate

Postby fixstuff555 » Tue Jul 30, 2013 3:56 pm

Those are awesome guys. Thanks a bunch :)
fixstuff555
 
Posts: 151
Joined: Thu Oct 21, 2010 3:24 pm


Return to General

Who is online

Users browsing this forum: No registered users and 47 guests