#!/usr/bin/env ruby

require "socket"
require "dl/import"

module Win32
 extend DL::Importable
 
 typealias "DWORD", "unsigned long"
 typealias "BYTE", "unsigned char"
 typealias "ULONG", "unsigned long"
 
 KEYEVENTF_KEYUP = 0x02
 VK_SHIFT = 0x10
 VK_A = 0x41
 VK_SPACE = 0x20
 VK_BACKSPACE = 0x08
 
 dlload "user32.dll"
 extern "void keybd_event(BYTE, BYTE, DWORD, ULONG)"
 
 def send_keybd_event(bVk, dwFlags = 0)
  keybd_event(bVk, 0, dwFlags,0)
 end

 module_function :send_keybd_event
end

serverhost = 'localhost'
port = 4321

socket = TCPSocket.new(serverhost, port)

prevval0 = 0
prevval2 = 0
while line = socket.gets
 (io,device,ad,portno,val) = line.chomp.split(/,/)
 if io == 'In' && device == 'InterfaceKit' && 
   ad = 'Analog' && portno.to_i == 0 then
   if val.to_i > 100 && prevval0 < 100 then
   Win32.send_keybd_event(Win32::VK_SPACE)
   Win32.send_keybd_event(Win32::VK_SPACE,
           Win32::KEYEVENTF_KEYUP)
  end
  prevval0 = val.to_i
 end
...